Spring Boot(5)一个极简且完整的后台框架

一个完整的极简后台框架,方便做小项目的时候可以快速开发。
这里面多贴图片和代码,做个参考吧,代码可以下载下来自己看看,里面这套后台模板不错,喜欢的拿去。

先放几张图

Spring Boot(5)一个极简且完整的后台框架_第1张图片
后台登录页面
Spring Boot(5)一个极简且完整的后台框架_第2张图片
后台主页面
Spring Boot(5)一个极简且完整的后台框架_第3张图片
列表页

项目介绍

SpringBoot,我也是第一次用,实现了一个极简单的后台框架,希望有不太合理的地方大家给个建议。


Spring Boot(5)一个极简且完整的后台框架_第4张图片
基础架构
Spring Boot(5)一个极简且完整的后台框架_第5张图片
项目结构

项目配置

maven配置pox.xml



    4.0.0

    com.moxi
    moxi
    0.0.1-SNAPSHOT
    jar

    moxi
    mox

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.2.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.springframework.boot
            spring-boot-devtools
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.2.0
        
        
        
        
            mysql
            mysql-connector-java
            runtime
        
        
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
        
        
            commons-io
            commons-io
            2.4
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


项目配置文件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 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";
    }
    
    
}

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 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);
    
}

View层,使用的thymeleaf的标签,挺好用的,本来打算全站用ajax,不过开发效率稍微慢了些。









MOXI












    

文章分类

搜索
文章列表
ID 名称 描述 添加时间 操作
Onions Onions Onions Onions

    分页封装,一切为了极简,做了个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("
  • "+(i+1)+"
  • "); } html.append("
  • "); html.append("
  • »
  • "); return html.toString().replaceAll("null", ""); } }

    项目下载地址

    https://github.com/daleiwang/moxi

    就这些,足够简单。包含登录、列表、分页、新增、修改、上传文件等……接下来会不断进行完善。
    sql语句放到项目里面了。

    Spring Boot(1)工具安装:

    http://www.jianshu.com/p/fb6ed37c90eb

    Spring Boot(2)新建Spring Boot工程

    http://www.jianshu.com/p/00fd73f515f6

    Spring Boot(3)整合Mybatis

    http://www.jianshu.com/p/8401e9304fa0

    Spring Boot(4)整合thymeleaf

    http://www.jianshu.com/p/8d2cc7207fb2

    Spring Boot(5)一个极简且完整的后台框架

    http://www.jianshu.com/p/923d26d705ed

    Spring Boot(6)jar方式打包发布

    http://www.jianshu.com/p/9cf6faa8595e

    Spring Boot(7)war方式打包发布

    http://www.jianshu.com/p/ae170a58f88c

    SQL语句

    -- MySQL dump 10.13  Distrib 5.7.12, for Win64 (x86_64)
    --
    -- Host: localhost    Database: moxi
    -- ------------------------------------------------------
    -- Server version   5.7.17-log
    
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
    /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    /*!40103 SET TIME_ZONE='+00:00' */;
    /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
    --
    -- Table structure for table `admin`
    --
    
    DROP TABLE IF EXISTS `admin`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `admin` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `userName` varchar(45) NOT NULL,
      `password` varchar(45) NOT NULL,
      `realName` varchar(45) DEFAULT NULL,
      `age` int(11) DEFAULT NULL,
      `phoneNumber` varchar(45) DEFAULT NULL,
      `headPicture` varchar(45) DEFAULT NULL,
      `addDate` date DEFAULT NULL,
      `updateDate` date DEFAULT NULL,
      `state` int(11) DEFAULT '0' COMMENT '1:正常\n2:冻结\n3:删除',
      PRIMARY KEY (`id`),
      UNIQUE KEY `name_UNIQUE` (`userName`)
    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `admin`
    --
    
    LOCK TABLES `admin` WRITE;
    /*!40000 ALTER TABLE `admin` DISABLE KEYS */;
    INSERT INTO `admin` VALUES (1,'admin','admin','面皮大师',28,'17788886666','','2017-06-15','2017-06-15',1);
    /*!40000 ALTER TABLE `admin` ENABLE KEYS */;
    UNLOCK TABLES;
    
    --
    -- Table structure for table `news`
    --
    
    DROP TABLE IF EXISTS `news`;
    /*!40101 SET @saved_cs_client     = @@character_set_client */;
    /*!40101 SET character_set_client = utf8 */;
    CREATE TABLE `news` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `title` varchar(45) NOT NULL,
      `description` varchar(45) DEFAULT NULL,
      `category` int(11) DEFAULT NULL,
      `image` varchar(45) DEFAULT NULL,
      `content` mediumtext,
      `addDate` datetime DEFAULT NULL,
      `updateDate` datetime DEFAULT NULL,
      `state` int(11) DEFAULT NULL COMMENT '1,正常\n2,删除',
      `commendState` int(11) DEFAULT NULL COMMENT '1,正常\n2,推荐',
      `browses` int(11) DEFAULT NULL COMMENT '浏览数',
      `likes` int(11) DEFAULT NULL COMMENT '喜欢数',
      `comments` int(11) DEFAULT NULL COMMENT '回复数',
      `score` int(11) DEFAULT NULL COMMENT '分数,用于排序',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
    /*!40101 SET character_set_client = @saved_cs_client */;
    
    --
    -- Dumping data for table `news`
    --
    
    LOCK TABLES `news` WRITE;
    /*!40000 ALTER TABLE `news` DISABLE KEYS */;
    INSERT INTO `news` VALUES (1,'科技是第一生产力1','科技是第一生产力',2,NULL,'

    科技是第一生产力

    ','2017-06-15 19:15:04','2017-06-15 19:15:04',1,1,0,0,0,0),(2,'科技是第一生产力2','科技是第一生产力2',2,'','科技是第一生产力2','2017-06-15 19:17:10','2017-06-15 19:17:10',1,1,0,0,0,0),(3,'科技是第一生产力3','科技是第一生产力3',2,'','科技是第一生产力3','2017-06-15 19:17:31','2017-06-15 19:17:31',1,1,0,0,0,0),(4,'科技是第一生产力4','科技是第一生产力4',2,'','科技是第一生产力4','2017-06-15 19:17:50','2017-06-15 19:17:50',1,1,0,0,0,0),(5,'科技是第一生产力5','科技是第一生产力5',2,'','科技是第一生产力5','2017-06-15 19:17:57','2017-06-15 19:17:57',1,1,0,0,0,0),(6,'科技是第一生产力6','科技是第一生产力6',2,'','科技是第一生产力6','2017-06-15 19:18:02','2017-06-15 19:18:02',1,1,0,0,0,0),(7,'科技是第一生产力7','科技是第一生产力7',2,'','科技是第一生产力7','2017-06-15 19:18:06','2017-06-15 19:18:06',1,1,0,0,0,0),(8,'科技是第一生产力8','科技是第一生产力8',2,'','科技是第一生产力8','2017-06-15 19:18:10','2017-06-15 19:18:10',1,1,0,0,0,0),(9,'科技是第一生产力9','科技是第一生产力9',2,'','科技是第一生产力9','2017-06-15 19:18:21','2017-06-15 19:18:21',1,1,0,0,0,0),(10,'科技是第一生产力10','科技是第一生产力10',2,'','科技是第一生产力10','2017-06-15 19:18:30','2017-06-15 19:18:30',1,1,0,0,0,0),(11,'财经天下1','财经天下1',1,'','财经天下1','2017-06-15 19:19:13','2017-06-15 19:19:13',1,1,0,0,0,0),(12,'财经天下2','财经天下2',1,'','财经天下2','2017-06-15 19:19:19','2017-06-15 19:19:19',1,1,0,0,0,0),(13,'财经天下3','财经天下3',1,'','财经天下3','2017-06-15 19:19:23','2017-06-15 19:19:23',1,2,0,0,0,0),(14,'财经天下4','财经天下4',1,'','财经天下4','2017-06-15 19:19:27','2017-06-15 19:19:27',1,1,0,0,0,0),(15,'财经天下5','财经天下5',1,'','财经天下5','2017-06-15 19:19:31','2017-06-15 19:19:31',1,1,0,0,0,0),(16,'财经天下6','财经天下6',1,'','财经天下6','2017-06-15 19:19:34','2017-06-15 19:19:34',1,1,0,0,0,0),(17,'财经天下7','财经天下7',1,'','财经天下7','2017-06-15 19:19:38','2017-06-15 19:19:38',2,2,0,0,0,0),(18,'财经天下8','财经天下8',1,'','财经天下8','2017-06-15 19:19:42','2017-06-15 19:19:42',1,1,0,0,0,0),(19,'财经天下9','财经天下9',1,'','财经天下9','2017-06-15 19:19:45','2017-06-15 19:19:45',1,1,0,0,0,0),(20,'财经天下10','财经天下10',1,'','财经天下10','2017-06-15 19:19:53','2017-06-15 19:19:53',1,2,0,0,0,0),(21,'体育人生1','体育人生1',3,'','体育人生1','2017-06-15 19:20:55','2017-06-15 19:20:55',1,2,0,0,0,0),(22,'体育人生2','体育人生2',3,'','体育人生2','2017-06-15 19:21:03','2017-06-15 19:21:03',2,1,0,0,0,0),(23,'体育人生3','体育人生3',3,'','体育人生3','2017-06-15 19:21:07','2017-06-15 19:21:07',2,2,0,0,0,0),(24,'体育人生4','体育人生4',3,'','体育人生4','2017-06-15 19:21:11','2017-06-15 19:21:11',2,1,0,0,0,0),(25,'体育人生5','体育人生5',3,'','体育人生5','2017-06-15 19:21:14','2017-06-15 19:21:14',2,2,0,0,0,0),(26,'特泰尼克号update','一个美好的故事update',3,'/userfiles/20170704160534_351.png','

    一个美好的故事update

    ','2017-07-04 11:11:18','2017-07-04 16:05:34',2,2,11,12,15,111),(27,'张学友芜湖演唱会','张学友芜湖演唱会一票难求',5,'/userfiles/20170704143344_119.png','

    云堆微软文分发介绍

    自媒体真实流量分发 · 品效合一

    云堆投放优势

    ','2017-07-04 14:33:44','2017-07-04 14:33:44',2,1,0,0,0,0),(28,'','',0,'','','2017-07-05 11:35:19','2017-07-05 11:35:19',2,1,0,0,0,0),(29,'标题','',1,'','


    ','2017-07-05 11:46:14','2017-07-05 14:03:27',1,1,0,0,0,0); /*!40000 ALTER TABLE `news` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `news_category` -- DROP TABLE IF EXISTS `news_category`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `news_category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `description` varchar(45) DEFAULT NULL, `image` varchar(45) DEFAULT NULL, `addDate` datetime DEFAULT NULL, `state` int(11) DEFAULT NULL COMMENT '1,正常\n2,删除', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `news_category` -- LOCK TABLES `news_category` WRITE; /*!40000 ALTER TABLE `news_category` DISABLE KEYS */; INSERT INTO `news_category` VALUES (1,'财经','财经','','2017-06-15 18:59:37',1),(2,'科技','科技','','2017-06-15 18:59:46',1),(3,'体育','体育','','2017-06-15 18:59:55',1),(4,'人文','人文','','2017-06-15 19:00:06',2),(5,'娱乐','娱乐八卦','/userfiles/20170704110957.png','2017-07-04 11:09:57',2),(6,'历史','历史文学','/userfiles/20170705113505.png','2017-07-05 11:34:31',1); /*!40000 ALTER TABLE `news_category` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2017-07-05 14:23:39

    你可能感兴趣的:(Spring Boot(5)一个极简且完整的后台框架)