参考:
https://blog.csdn.net/weixin_45947759/article/details/116805582

1.引入依赖

  1. 1.引入依赖
  2. <!--Mybatis-plus-SpringBoot集成-->
  3. <dependency>
  4. <groupId>com.baomidou</groupId>
  5. <artifactId>mybatis-plus-boot-starter</artifactId>
  6. <version>3.5.1</version>
  7. </dependency>
  8. <!--hutool-->
  9. <dependency>
  10. <groupId>cn.hutool</groupId>
  11. <artifactId>hutool-all</artifactId>
  12. <version>5.7.22</version>
  13. </dependency>
  14. <!--knife4j接口文档-->
  15. <dependency>
  16. <groupId>com.github.xiaoymin</groupId>
  17. <artifactId>knife4j-spring-boot-starter</artifactId>
  18. <version>3.0.3</version>
  19. </dependency>

2.application.yml

  1. # mybaits-plus配置
  2. mybatis-plus:
  3. mapperLocations: classpath:/mapper/*Mapper.xml
  4. type-aliases-package: com.xqny.包名
  5. configuration:
  6. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  7. map-underscore-to-camel-case: true
  8. global-config:
  9. db-config:
  10. logic-delete-field: isDelete # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不需要实体类配置@TableLogic)
  11. logic-delete-value: 1 # 逻辑已删除值(默认为 1)
  12. logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)

3.Mybatis Plus配置类

  1. import com.baomidou.mybatisplus.annotation.DbType;
  2. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  3. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. @MapperScan("com.xqny.包名.mapper")
  9. public class MybatisPlusConfig {
  10. /**
  11. * 分页插件,一缓和二缓遵循mybatis的规则
  12. */
  13. @Bean
  14. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  15. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  16. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
  17. return interceptor;
  18. }
  19. }

4.统一R类

  1. @ToString
  2. @Data
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. @Accessors(chain = true)
  6. public class R<T> implements Serializable {
  7. private static final long serialVersionUID = 1L;
  8. @ApiModelProperty("状态码,0:成功,1:失败")
  9. private int code;
  10. @ApiModelProperty("返回信息")
  11. private String msg;
  12. @ApiModelProperty("返回数据")
  13. private T data;
  14. public static R ok(Object data) {
  15. return new R(0,"success",data);
  16. }
  17. public static R ok(int code, String msg, Object data) {
  18. return new R().setStatus(code).setMsg(msg).setData(data);
  19. }
  20. public static R fail(Object data) {
  21. return new R(1, "error",data);
  22. }
  23. }

5.模板

entity

  1. ##定义初始变量
  2. #set($tableName = $tableInfo.name)
  3. ##设置回调
  4. $!callback.setFileName($tool.append($tableName, ".java"))
  5. $!callback.setSavePath($tool.append($tableInfo.savePath, "/entity"))
  6. ##使用全局变量实现默认包导入
  7. #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}entity;
  8. import io.swagger.annotations.ApiModel;
  9. import io.swagger.annotations.ApiModelProperty;
  10. import lombok.AllArgsConstructor;
  11. import lombok.Data;
  12. import lombok.NoArgsConstructor;
  13. import com.baomidou.mybatisplus.annotation.IdType;
  14. import com.baomidou.mybatisplus.annotation.TableField;
  15. import com.baomidou.mybatisplus.annotation.TableId;
  16. import com.baomidou.mybatisplus.annotation.TableName;
  17. import java.io.Serializable;
  18. @Data
  19. @TableName("$!tool.hump2Underline($!{tableInfo.name})")
  20. @ApiModel("$!{tableInfo.comment}")
  21. @AllArgsConstructor
  22. @NoArgsConstructor
  23. public class $!{tableInfo.name} implements Serializable {
  24. private static final long serialVersionUID = $!tool.serial();
  25. #foreach($column in $tableInfo.fullColumn)
  26. #if(${column.comment})
  27. #end
  28. #if(${column.name} == "id")
  29. @TableId(value = "id", type = IdType.AUTO)
  30. #end
  31. #if(${column.name} != "id")
  32. @TableField("$!tool.hump2Underline($column.name)")
  33. #end
  34. @ApiModelProperty("${column.comment}")
  35. private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
  36. #end
  37. }

dao(mapper)

  1. ##定义初始变量
  2. #set($tableName = $tool.append($tableInfo.name, "Dao"))
  3. ##设置回调
  4. $!callback.setFileName($tool.append($tableName, ".java"))
  5. $!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))
  6. ##拿到主键
  7. #if(!$tableInfo.pkColumn.isEmpty())
  8. #set($pk = $tableInfo.pkColumn.get(0))
  9. #end
  10. #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;
  11. import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
  12. import org.apache.ibatis.annotations.Param;
  13. import java.util.List;
  14. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  15. /**
  16. * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
  17. *
  18. * @author $!author
  19. * @since $!time.currTime()
  20. */
  21. public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}> {
  22. }

service

  1. ##定义初始变量
  2. #set($tableName = $tool.append($tableInfo.name, "Service"))
  3. ##设置回调
  4. $!callback.setFileName($tool.append($tableName, ".java"))
  5. $!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))
  6. ##拿到主键
  7. #if(!$tableInfo.pkColumn.isEmpty())
  8. #set($pk = $tableInfo.pkColumn.get(0))
  9. #end
  10. #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;
  11. import com.baomidou.mybatisplus.core.metadata.IPage;
  12. import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
  13. import $!{tableInfo.savePackageName}.vo.$!{tableInfo.name}VO;
  14. import java.util.List;
  15. import com.baomidou.mybatisplus.extension.service.IService;
  16. /**
  17. * $!{tableInfo.comment}($!{tableInfo.name})表服务接口
  18. *
  19. * @author $!author
  20. * @since $!time.currTime()
  21. */
  22. public interface $!{tableName} extends IService<$!{tableInfo.name}> {
  23. IPage<$!{tableInfo.name}VO> selectPageVo(Integer pageNum, Integer pageSize);
  24. }

serviceImpl

  1. ##定义初始变量
  2. #set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
  3. ##设置回调
  4. $!callback.setFileName($tool.append($tableName, ".java"))
  5. $!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))
  6. ##拿到主键
  7. #if(!$tableInfo.pkColumn.isEmpty())
  8. #set($pk = $tableInfo.pkColumn.get(0))
  9. #end
  10. #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;
  11. import com.baomidou.mybatisplus.core.metadata.IPage;
  12. import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
  13. import $!{tableInfo.savePackageName}.vo.$!{tableInfo.name}VO;
  14. import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
  15. import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
  16. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.util.List;
  20. /**
  21. * $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
  22. *
  23. * @author $!author
  24. * @since $!time.currTime()
  25. */
  26. @Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
  27. public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
  28. @Resource
  29. private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
  30. @Override
  31. public IPage<$!{tableInfo.name}VO> selectPageVo(Integer pageNum, Integer pageSize) {
  32. return $tool.append($!tool.firstLowerCase($tableInfo.name), 'Dao.selectPageVo(new Page(pageNum, pageSize));')
  33. }
  34. }

controller

  1. ##定义初始变量
  2. #set($tableName = $tool.append($tableInfo.name, "Controller"))
  3. ##设置回调
  4. $!callback.setFileName($tool.append($tableName, ".java"))
  5. $!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
  6. ##拿到主键
  7. #if(!$tableInfo.pkColumn.isEmpty())
  8. #set($pk = $tableInfo.pkColumn.get(0))
  9. #end
  10. #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;
  11. import cn.hutool.core.bean.BeanUtil;
  12. import cn.hutool.core.util.BooleanUtil;
  13. import $!{tableInfo.savePackageName}.dto.$!{tableInfo.name}DTO;
  14. import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
  15. import $!{tableInfo.savePackageName}.vo.$!{tableInfo.name}VO;
  16. import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
  17. import io.swagger.annotations.*;
  18. import org.springframework.web.bind.annotation.*;
  19. import javax.annotation.Resource;
  20. /**
  21. * $!{tableInfo.comment}($!{tableInfo.name})表控制层
  22. *
  23. * @author $!author
  24. * @since $!time.currTime()
  25. */
  26. @RestController
  27. @Api(tags = "$!{tableInfo.comment}($!{tableInfo.name})")
  28. @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
  29. public class $!{tableName} {
  30. /**
  31. * $!{tableInfo.name}服务对象
  32. */
  33. @Resource
  34. private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
  35. @GetMapping("/$!{tableInfo.name}/{id}")
  36. @ApiOperation(value = "根据id查询$!{tableInfo.name}")
  37. @ApiImplicitParams({
  38. @ApiImplicitParam( name = "id", value = "id",dataTypeClass = String.class,required = true)
  39. })
  40. public R<$!{tableInfo.name}VO> $!{tableInfo.name}(@PathVariable("id") String id) {
  41. $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name) = $tool.append($!tool.firstLowerCase($tableInfo.name), 'Service.query().eq("id", id).one();')
  42. $!{tableInfo.name}VO $!tool.firstLowerCase($tableInfo.name)VO = BeanUtil.copyProperties($!tool.firstLowerCase($tableInfo.name), $!{tableInfo.name}VO.class);
  43. return R.ok($!tool.firstLowerCase($tableInfo.name)VO);
  44. }
  45. @PostMapping("/insert")
  46. @ApiOperation(value = "新增$!{tableInfo.name}")
  47. public R<$!{tableInfo.name}VO> insert(@RequestBody $!{tableInfo.name}DTO $!tool.firstLowerCase($tableInfo.name)DTO) {
  48. $!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name) = BeanUtil.copyProperties($!tool.firstLowerCase($tableInfo.name)DTO, $!{tableInfo.name}.class);
  49. boolean flag = $tool.append($!tool.firstLowerCase($tableInfo.name), "Service.save($!tool.firstLowerCase($tableInfo.name));")
  50. if (BooleanUtil.isTrue(flag)) {
  51. return R.ok("");
  52. }
  53. return R.fail("插入失败");
  54. }
  55. @GetMapping("/page")
  56. @ApiOperation(value = "分页")
  57. @ApiImplicitParams({
  58. @ApiImplicitParam( name = "pageNum", value = "当前页码,默认值1",dataTypeClass = Integer.class,required = false),
  59. @ApiImplicitParam( name = "pageSize", value = "每页个数,默认值10",dataTypeClass = Integer.class,required = false)
  60. })
  61. public R<$!{tableInfo.name}VO> selectPageVo(@RequestParam(required = false,defaultValue ="1") Integer pageNum, @RequestParam(required = false,defaultValue="10") Integer pageSize) {
  62. IPage<$!{tableInfo.name}VO> $!tool.firstLowerCase($tableInfo.name)PageVO = $tool.append($!tool.firstLowerCase($tableInfo.name), 'Service.selectPageVo(pageNum,pageSize);')
  63. return R.ok($!tool.firstLowerCase($tableInfo.name)PageVO);
  64. }
  65. }

mapper.xml

  1. ##引入mybatis支持
  2. $!mybatisSupport
  3. ##设置保存名称与保存位置
  4. $!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
  5. ##$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))
  6. $!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper"))
  7. ##拿到主键
  8. #if(!$tableInfo.pkColumn.isEmpty())
  9. #set($pk = $tableInfo.pkColumn.get(0))
  10. #end
  11. <?xml version="1.0" encoding="UTF-8"?>
  12. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  13. <mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Dao">
  14. <resultMap id="BaseResultMap" type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
  15. <!--@Table $!{tableInfo.originTableName}-->
  16. #foreach($column in $tableInfo.fullColumn)
  17. <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
  18. #end
  19. </resultMap>
  20. <!--<association property="属性名" javaType="对应的Java类型" column="{自定义的参数名=查询的字段名}" select="查询语句所在的路径"></association>-->
  21. <!--<collection property="属性名" javaType="对应的Java类型" column="{自定义的参数名=查询的字段名}" ofType="关联的对象类型" select="查询语句所在的路径"></collection>-->
  22. <!--<foreach item="item" index="index" collection="ids.split(',')这里可以接受逗号分隔字符串,也能接受数组" open="(" separator="," close=")">#{item}</foreach>-->
  23. </mapper>

vo

  1. ##定义初始变量
  2. #set($tableName = $tool.append($tableInfo.name, "VO"))
  3. ##设置回调
  4. $!callback.setFileName($tool.append($tableName, ".java"))
  5. $!callback.setSavePath($tool.append($tableInfo.savePath, "/vo"))
  6. ##使用全局变量实现默认包导入
  7. #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}vo;
  8. import io.swagger.annotations.ApiModel;
  9. import io.swagger.annotations.ApiModelProperty;
  10. import lombok.AllArgsConstructor;
  11. import lombok.Data;
  12. import lombok.NoArgsConstructor;
  13. import java.io.Serializable;
  14. /**
  15. * $!{tableInfo.comment}($!{tableInfo.name})VO
  16. *
  17. * @author $!author
  18. * @since $!time.currTime()
  19. */
  20. @Data
  21. @ApiModel("$!{tableInfo.comment}VO")
  22. @AllArgsConstructor
  23. @NoArgsConstructor
  24. public class $!{tableInfo.name}VO implements Serializable {
  25. private static final long serialVersionUID = $!tool.serial();
  26. #foreach($column in $tableInfo.fullColumn)
  27. @ApiModelProperty("${column.comment}")
  28. private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
  29. #end
  30. }

dto

  1. ##定义初始变量
  2. #set($tableName = $tool.append($tableInfo.name, "DTO"))
  3. ##设置回调
  4. $!callback.setFileName($tool.append($tableName, ".java"))
  5. $!callback.setSavePath($tool.append($tableInfo.savePath, "/dto"))
  6. ##使用全局变量实现默认包导入
  7. #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dto;
  8. import io.swagger.annotations.ApiModel;
  9. import io.swagger.annotations.ApiModelProperty;
  10. import lombok.AllArgsConstructor;
  11. import lombok.Data;
  12. import lombok.NoArgsConstructor;
  13. import java.io.Serializable;
  14. /**
  15. * $!{tableInfo.comment}($!{tableInfo.name})DTO
  16. *
  17. * @author $!author
  18. * @since $!time.currTime()
  19. */
  20. @Data
  21. @ApiModel("$!{tableInfo.comment}DTO")
  22. @AllArgsConstructor
  23. @NoArgsConstructor
  24. public class $!{tableInfo.name}DTO implements Serializable {
  25. private static final long serialVersionUID = $!tool.serial();
  26. #foreach($column in $tableInfo.fullColumn)
  27. @ApiModelProperty("${column.comment}")
  28. private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
  29. #end
  30. }