SpringBoot + MyBatis-Plus +MySQL8 +Thymeleaf +LayUI通用业务模块增删改查

Posted 在奋斗的大道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot + MyBatis-Plus +MySQL8 +Thymeleaf +LayUI通用业务模块增删改查相关的知识,希望对你有一定的参考价值。

目录

1、基础技术框架

2、完整pom.xml  

3、初始化脚本

4、功能效果截图

5、项目结构截图: 

6、业务模块说明

 7、通用模块说明

 8、系统初始化配置和程序入口

 9:源码下载地址


1、基础技术框架

技术名称版本
SpringBoot2.1.0.RELEASE
Mybatis-Plus3.4.1
Lombok1.18.2
mysql88.0.13
Alibaba Druid1.1.10
Thymelaf3.0.11
Alibaba Fastjson1.2.4
Hutool5.5.2

2、完整pom.xml  

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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.zzg</groupId>
	<artifactId>cas-thymeleaf</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<encoding>UTF-8</encoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--starter -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!-- test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--validation -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
		<!-- MyBatis-Plus 拓展 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.1</version>
		</dependency>
		<!--lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

		<!--mysql-connector -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<!-- druid-spring-boot-starter -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>
		<!-- 集成thymelaf -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!-- commons-lang -->
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<!-- apache common 模块 -->
		<!--commons-lang3 工具包 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.9</version>
		</dependency>
		<!--commons-codec 加密工具包 -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.10</version>
		</dependency>
		<!--commons-net 网络工具包 -->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.6</version>
		</dependency>
		<!--common-io 工具包 -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.6</version>
		</dependency>
		<!--common-collection 工具包 -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<!-- 集成fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.4</version>
		</dependency>
		<!-- 集成hutool-all 工具包 -->
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.5.2</version>
		</dependency>


	</dependencies>
</project>

3、初始化脚本

CREATE TABLE IF NOT EXISTS `employee` (
  `id` int(32) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `code` varchar(32) NOT NULL COMMENT '员工编号',
  `name` varchar(64) NOT NULL COMMENT '员工名称',
  `sex` int(2) NOT NULL DEFAULT '1' COMMENT '性别:1:男(默认) 2:女',
  `birthday` datetime NOT NULL COMMENT '出生日期',
  `passwd` varchar(32) NOT NULL COMMENT '员工密码',
  `telephone` varchar(32) NOT NULL COMMENT '电话',
  `note` varchar(128) DEFAULT NULL COMMENT '备注',
  `work_year` int(2) NOT NULL COMMENT '工作年限',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='员工表';

4、功能效果截图

5、项目结构截图: 

6、业务模块说明

com.zzg.entity = 实体对象定义

com.zzg.mapper= 接口映射

com.zzg.service= 服务定义

com.zzg.service.impl= 服务实列化

com.zzg.controller=业务控制层

package com.zzg.entity;                                                                                                         
                                                                                                                                
import java.util.Date;                                                                                                          
                                                                                                                                
import org.springframework.format.annotation.DateTimeFormat;                                                                    
                                                                                                                                
import com.baomidou.mybatisplus.annotation.FieldFill;                                                                           
import com.baomidou.mybatisplus.annotation.IdType;                                                                              
import com.baomidou.mybatisplus.annotation.TableField;                                                                          
import com.baomidou.mybatisplus.annotation.TableId;                                                                             
import com.baomidou.mybatisplus.annotation.TableName;                                                                           
                                                                                                                                
import lombok.Data;                                                                                                             
                                                                                                                                
@Data                                                                                                                           
@TableName                                                                                                                      
public class Employee                                                                                                          
	@TableId(type=IdType.AUTO)                                                                                                  
    private Integer id;                                                                                                         
                                                                                                                                
    private String code;                                                                                                        
                                                                                                                                
    private String name;                                                                                                        
                                                                                                                                
    private Integer sex;                                                                                                        
                                                                                                                                
    @DateTimeFormat(pattern = "yyyy-MM-dd")                                                                                     
    private Date birthday;                                                                                                      
                                                                                                                                
    private String passwd;                                                                                                      
                                                                                                                                
    private String telephone;                                                                                                   
                                                                                                                                
    @TableField(fill = FieldFill.DEFAULT)                                                                                       
    private String note;                                                                                                        
                                                                                                                                
    private Integer workYear;                                                                                                   
                                                                                                                                
                                                                                                                                
                                                                                                                               
package com.zzg.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzg.entity.Employee;

public interface EmployeeMapper extends BaseMapper<Employee> 
package com.zzg.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.zzg.entity.Employee;


public interface EmployeeService extends IService<Employee> 


package com.zzg.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzg.entity.Employee;
import com.zzg.mapper.EmployeeMapper;
import com.zzg.service.EmployeeService;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService 


package com.zzg.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzg.common.AbstractCommonController;
import com.zzg.common.Response;
import com.zzg.entity.Employee;
import com.zzg.service.EmployeeService;

@Controller
@RequestMapping("/employee")
public class EmployeeController extends AbstractCommonController<Employee> 
	@Autowired
	private EmployeeService service;
	
	@GetMapping(value = "/list")
	public String view() 
		return "page/sys/employeeList";
	
	@GetMapping(value = "/add")
	public String add() 
		return "page/sys/employeeAdd";
	
	
	
	 // 增
	@RequestMapping(value = "save")
	@ResponseBody
    public Response<Boolean> save(Employee employe) 
    	Boolean target = service.save(employe);
        return Response.success(target);
    
    
    @RequestMapping(value = "edit")
	public String edit(Employee employee, Model model) 
		model.addAttribute("employee", service.getById(employee.getId()));
		return "page/sys/employeeEdit";
	

    // 改
    @RequestMapping(value = "update")
	@ResponseBody
    public Response<Boolean> update(Employee employee ) 
        return Response.success(service.updateById(employee));
    

    // 删
    @RequestMapping(value = "delete")
	@ResponseBody
    public Response<Boolean> delete( Employee employee ) 
        return Response.success(service.removeById(employee.getId()));
    

    // 查
    @GetMapping( value = "/getUserByName")
    public Object getUserByName( @RequestParam String userName ) 
    	QueryWrapper<Employee> wrapper =new QueryWrapper<Employee>() ;
    	wrapper.like("name", userName);
        return service.getOne(wrapper);
    
    
    // 查
    @GetMapping( value = "/page", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object page( @RequestParam(required = false) Map paramter ) 
    	Page<Employee> page = super.getPage(paramter);
    	QueryWrapper<Employee> wrapper =new QueryWrapper<Employee>() ;
    	
    	if(paramter.get("id") != null && StringUtils.isNotEmpty(paramter.get("id").toString())) 
    		wrapper.eq(true, "id", paramter.get("id"));
    	
    	if(paramter.get("name") != null && StringUtils.isNotEmpty(paramter.get("name").toString())) 
    		wrapper.like(true, "name", paramter.get("name"));
    	
    	if(paramter.get("code") != null && StringUtils.isNotEmpty(paramter.get("code").toString())) 
    		wrapper.like(true, "code", paramter.get("code"));
    	
    	IPage<Employee> result = service.page(page, wrapper);
        return result;
    
    
    


 

package com.zzg.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 系统首页Controller
 * 
 * @author zzg
 *
 */
@Controller
public class IndexController 
	/**
	 * 首页
	 * 
	 * @return
	 */
	@GetMapping(value = "index")
	public String index(Model model) 
		model.addAttribute("user", "周志刚");
		return "index";
	
	
	/**
	 * 控制台
	 * 
	 * @return
	 */
	@GetMapping(value = "view")
	public String view() 
		return "page/sys/view";
	

 7、通用模块说明

package com.zzg.common.converter;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.util.NumberUtils;

public class ObjectToIntegerConverterFactory implements ConverterFactory<Integer, Integer> 

	public <T extends Integer> Converter<Integer, T> getConverter(Class<T> targetType) 
		// TODO Auto-generated method stub
		return new ObjectToInteger(targetType);
	
	
	// 私有内部类:实现Converter接口。用泛型边界约束一类类型
		private static final class ObjectToInteger<T extends Integer> implements Converter<T, Integer> 

			private final Class<T> targetType;
			public ObjectToInteger(Class<T> targetType) 
				this.targetType = targetType;
			


			public Integer convert(T source) 
				// TODO Auto-generated method stub
				if(source == null) 
					return 0;
				
				return NumberUtils.parseNumber(String.valueOf(source), this.targetType);
			
		


package com.zzg.common;

import java.util.Map;

import org.springframework.core.convert.converter.ConverterFactory;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzg.common.converter.ObjectToIntegerConverterFactory;

public abstract class AbstractCommonController<T> 
	public static final String pageNo = "pageNo";
	public static final String pageSize = "pageSize";
	public Page<T> getPage(Map map)
		ConverterFactory<Integer, Integer> converterFactory = new ObjectToIntegerConverterFactory();
		Integer no = converterFactory.getConverter(Integer.class).convert(Integer.valueOf(String.valueOf(map.get(pageNo))));
		Integer size = converterFactory.getConverter(Integer.class).convert(Integer.valueOf(String.valueOf(map.get(pageSize))));
		return new Page<T>(no,size);
	
	
	

package com.zzg.common;

import java.io.PrintWriter;

import javax.servlet.ServletResponse;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Response<T> 
	/**
	 * 响应码
	 */
	private int code;
 
	/**
	 * 响应消息体
	 */
	private String msg;
 
	/**
	 * 响应数据
	 */
	private T data;
 
	/**
	 * 失败响应
	 * 
	 * @param code 响应码
	 * @param msg  响应消息体
	 * @param data 响应数据
	 * @return
	 */
	public static <T> Response<T> error(int code, String msg, T data) 
		return new Response<T>(code, msg, data);
	
 
	/**
	 * 失败响应
	 * 
	 * @param msg 响应消息体
	 * @return
	 */
	public static <T> Response<T> error(String msg) 
		return new Response<T>(500, msg, null);
	
 
	/**
	 * 成功响应
	 * 
	 * @param data 响应数据
	 * @return
	 */
	public static <T> Response<T> success(T data) 
		return new Response<T>(200, null, data);
	
 
	/**
	 * 成功响应
	 * 
	 * @param msg 响应消息体
	 * @return
	 */
	public static <T> Response<T> success(String msg) 
		return new Response<T>(200, msg, null);
	
 
	/**
	 * 成功响应
	 * 
	 * @param msg  响应消息体
	 * @param data 响应数据
	 * @return
	 */
	public static <T> Response<T> success(String msg, T data) 
		return new Response<T>(200, msg, data);
	
 
	/**
	 * Response输出Json格式
	 * 
	 * @param response
	 * @param data     返回数据
	 */
	public static void responseJson(ServletResponse response, Object data) 
		PrintWriter out = null;
		try 
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json");
			out = response.getWriter();
			out.println(JSONUtil.toJsonStr(data));
			out.flush();
		 catch (Exception e) 
		 finally 
			if (out != null) 
				out.close();
			
		
	


 8、系统初始化配置和程序入口

package com.zzg.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
/**
 * MyBatisPlus 配置类
 * @author Administrator
 *
 */
@Configuration
public class MyBatisPlusConfig 
	 /**
     * 分页插件
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() 
        return new PaginationInterceptor();
    



package com.zzg;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.zzg.mapper")
public class DemoApplication 

	public static void main(String[] args) 
		// TODO Auto-generated method stub
		SpringApplication.run(DemoApplication.class, args);
	


 9:源码下载地址

链接:https://pan.baidu.com/s/1WN0k3oaMRMP1xV3r2nbIXg 
提取码:zcxy 

以上是关于SpringBoot + MyBatis-Plus +MySQL8 +Thymeleaf +LayUI通用业务模块增删改查的主要内容,如果未能解决你的问题,请参考以下文章

如何整合 springboot + mybatis-plus(系列一)

springboot整合mybatis-plus

SpringBoot使用·下篇(SpringBoot集成MyBatis+日志打印+MyBatis-plus)

SpringBoot使用·下篇(SpringBoot集成MyBatis+日志打印+MyBatis-plus)

mybatis-plus整合springboot入门

mybatis-plus整合springboot入门