Spring Boot 一個極簡且完整的後臺框架

鏈接:https://www.jianshu.com/p/871e70b3ad7c

一個完整的極簡後臺框架,方便做小項目的時候可以快速開發。

這裡面多貼圖片和代碼,做個參考吧,代碼可以下載下來自己看看,裡面這套後臺模板不錯,喜歡的拿去。

先放幾張圖

Spring Boot 一個極簡且完整的後臺框架

image

Spring Boot 一個極簡且完整的後臺框架

image

Spring Boot 一個極簡且完整的後臺框架

image

項目介紹

SpringBoot,實現了一個極簡單的後臺框架

Spring Boot 一個極簡且完整的後臺框架

image

項目配置

maven配置pox.xml


<project> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelversion>4.0.0/<modelversion>
<groupid>com.moxi/<groupid>
<artifactid>moxi/<artifactid>
<version>0.0.1-SNAPSHOT/<version>
<packaging>jar/<packaging>
<name>moxi/<name>
<description>mox/<description>
<parent>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-parent/<artifactid>
<version>1.5.2.RELEASE/<version>
<relativepath>
/<parent>
<properties>
<project.build.sourceencoding>UTF-8/<project.build.sourceencoding>
<project.reporting.outputencoding>UTF-8/<project.reporting.outputencoding>
<java.version>1.8/<java.version>
/<properties>
<dependencies>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-web/<artifactid>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-test/<artifactid>
<scope>test/<scope>
/<dependency>
<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-devtools/<artifactid>
/<dependency>

<dependency>
<groupid>org.mybatis.spring.boot/<groupid>
<artifactid>mybatis-spring-boot-starter/<artifactid>
<version>1.2.0/<version>

/<dependency>

<dependency>
<groupid>mysql/<groupid>
<artifactid>mysql-connector-java/<artifactid>
<scope>runtime/<scope>
/<dependency>

<dependency>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-starter-thymeleaf/<artifactid>
/<dependency>

<dependency>
<groupid>commons-io/<groupid>
<artifactid>commons-io/<artifactid>
<version>2.4/<version>
/<dependency>
/<dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot/<groupid>
<artifactid>spring-boot-maven-plugin/<artifactid>
/<plugin>
/<plugins>
/<build>
/<project>

項目配置文件application.properties

#DataBase start

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/moxi?useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=Shu1shu2

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#DataBase end

#thymeleaf start

spring.thymeleaf.mode=HTML5

spring.thymeleaf.encoding=UTF-8

spring.thymeleaf.content-type=text/html

#開發時關閉緩存,不然沒法看到實時頁面

spring.thymeleaf.cache=false

#thymeleaf end

#uploadFileSize start

spring.http.multipart.maxFileSize=10Mb

spring.http.multipart.maxRequestSize=100Mb

#uploadFileSize end

項目分層

Controller層,追求極簡,分頁自己進行了一個簡單封裝

package com.moxi.controller;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.moxi.model.NewsCategory;
import com.moxi.service.NewsCategoryService;
import com.moxi.util.PageUtil;
@Controller
@RequestMapping("/admin")
public class NewsController {

@Autowired
private NewsCategoryService newsCategoryService;
@RequestMapping("/newsManage_{pageCurrent}_{pageSize}_{pageCount}")
public String newsManage(@PathVariable Integer pageCurrent,@PathVariable Integer pageSize,@PathVariable Integer pageCount, Model model) {
return "/news/newsManage";
}
/**
* 文章分類列表
* @param newsCategory
* @param pageCurrent
* @param pageSize
* @param pageCount
* @param model
* @return
*/
@RequestMapping("/newsCategoryManage_{pageCurrent}_{pageSize}_{pageCount}")
public String newsCategoryManage(NewsCategory newsCategory,@PathVariable Integer pageCurrent,@PathVariable Integer pageSize,@PathVariable Integer pageCount, Model model) {
//判斷
if(pageSize == 0) pageSize = 10;
if(pageCurrent == 0) pageCurrent = 1;
int rows = newsCategoryService.count(newsCategory);
if(pageCount == 0) pageCount = rows%pageSize == 0 ? (rows/pageSize) : (rows/pageSize) + 1;
//查詢
newsCategory.setStart((pageCurrent - 1)*pageSize);
newsCategory.setEnd(pageSize);
List<newscategory> list = newsCategoryService.list(newsCategory);
//輸出
model.addAttribute("list", list);
String pageHTML = PageUtil.getPageContent("newsCategoryManage_{pageCurrent}_{pageSize}_{pageCount}?name="+newsCategory.getName(), pageCurrent, pageSize, pageCount);
model.addAttribute("pageHTML",pageHTML);
model.addAttribute("newsCategory",newsCategory);
return "/news/newsCategoryManage";
}
/**
* 文章分類新增、修改跳轉
* @param model
* @param newsCategory
* @return
*/
@GetMapping("newsCategoryEdit")
public String newsCategoryEditGet(Model model,NewsCategory newsCategory) {
if(newsCategory.getId()!=0){
NewsCategory newsCategoryT = newsCategoryService.findById(newsCategory);
model.addAttribute("newsCategory",newsCategoryT);
}
return "/news/newsCategoryEdit";
}
/**

* 文章分類新增、修改提交
* @param model
* @param newsCategory
* @param imageFile
* @param httpSession
* @return
*/
@PostMapping("newsCategoryEdit")
public String newsCategoryEditPost(Model model,NewsCategory newsCategory, @RequestParam MultipartFile[] imageFile,HttpSession httpSession) {
for (MultipartFile file : imageFile) {
if (file.isEmpty()) {
System.out.println("文件未上傳");
} else {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new java.util.Date();
String strDate = sdf.format(date);
String fileName = strDate + file.getOriginalFilename().substring(
file.getOriginalFilename().indexOf("."),
file.getOriginalFilename().length());
String realPath = httpSession.getServletContext().getRealPath("/userfiles");
System.out.println("realPath : "+realPath);
try {
FileUtils.copyInputStreamToFile(file.getInputStream(),new File(realPath, fileName));
newsCategory.setImage("/userfiles/"+fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
if(newsCategory.getId()!=0){
newsCategoryService.update(newsCategory);
} else {
newsCategoryService.insert(newsCategory);
}
return "redirect:newsCategoryManage_0_0_0";
}
}
/<newscategory>

Model層(pure類)

package com.moxi.model;

import java.sql.Date;

public class NewsCategory extends BaseObject {

private long id;

private String name;

private String description;

private String image;

private Date addDate;

private int state;

public long getId() {

return id;

}

public void setId(long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public String getImage() {

return image;

}

public void setImage(String image) {

this.image = image;

}

public Date getAddDate() {

return addDate;

}

public void setAddDate(Date addDate) {

this.addDate = addDate;

}

public int getState() {

return state;

}

public void setState(int state) {

this.state = state;

}

}

Service層,一切追求極簡,所以這裡Mybatis用得是註解,我覺得註解也挺好用的。

package com.moxi.service;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.moxi.model.NewsCategory;
@Mapper
public interface NewsCategoryService {
@Select("SELECT * FROM `moxi`.`news_category` where id = #{id};")

NewsCategory findById(NewsCategory newsCategory);
@Select({
""
})
List<newscategory> list(NewsCategory newsCategory);
@Select({
""
})
int count(NewsCategory newsCategory);
@Insert("INSERT INTO `moxi`.`news_category` (`id`, `name`, `description`, `image`, `addDate`, `state`) VALUES (null, #{name}, #{description}, #{image}, now(), 0);")
int insert(NewsCategory newsCategory);
@Update("UPDATE `moxi`.`news_category`SET `name` = #{name}, `description` = #{description}, `image` = #{image}, `state` = #{state} WHERE `id` = #{id};")
int update(NewsCategory newsCategory);
}
/<newscategory>

View層,使用的thymeleaf的標籤,挺好用的,本來打算全站用ajax,不過開發效率稍微慢了些。






<title>MOXI/<title>
<link>
<link>
<link>
<link>
<link>



分頁封裝,一切為了極簡,做了個util類

package com.moxi.util;

public class PageUtil {

public static String getPageContent(String url,int pageCurrent,int pageSize,int pageCount){

if (pageCount == 0) {

return "";

}

String urlNew = url.replace("{pageSize}", pageSize+"").replace("{pageCount}", pageCount+"");

String first = urlNew.replace("{pageCurrent}", 1+"");

String prev = urlNew.replace("{pageCurrent}", (pageCurrent - 1)+"");

String next = urlNew.replace("{pageCurrent}", (pageCurrent + 1)+"");

String last = urlNew.replace("{pageCurrent}", pageCount+"");

StringBuffer html = new StringBuffer();

html.append("

  • ");

    html.append("

  • ");

    for(int i = 0 ;i < pageCount; i++){

    String urlItem = urlNew.replace("{pageCurrent}", (i+1)+"");

    html.append("

  • ");

    }

    html.append("

  • ");

    html.append("

  • ");

    return html.toString().replaceAll("null", "");

    }

    }

    就這些,足夠簡單。包含登錄、列表、分頁、新增、修改、上傳文件等……接下來會不斷進行完善。

    sql語句放到項目裡面了。


  • 分享到:


    相關文章: