参考:
https://blog.csdn.net/weixin_45947759/article/details/116805582
1.引入依赖
1.引入依赖
<!--Mybatis-plus-SpringBoot集成-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
<!--knife4j接口文档-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
2.application.yml
# mybaits-plus配置
mybatis-plus:
mapperLocations: classpath:/mapper/*Mapper.xml
type-aliases-package: com.xqny.包名
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
global-config:
db-config:
logic-delete-field: isDelete # 全局逻辑删除的实体字段名(since 3.3.0,配置后可以忽略不需要实体类配置@TableLogic)
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
3.Mybatis Plus配置类
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.xqny.包名.mapper")
public class MybatisPlusConfig {
/**
* 分页插件,一缓和二缓遵循mybatis的规则
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
4.统一R类
@ToString
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("状态码,0:成功,1:失败")
private int code;
@ApiModelProperty("返回信息")
private String msg;
@ApiModelProperty("返回数据")
private T data;
public static R ok(Object data) {
return new R(0,"success",data);
}
public static R ok(int code, String msg, Object data) {
return new R().setStatus(code).setMsg(msg).setData(data);
}
public static R fail(Object data) {
return new R(1, "error",data);
}
}
5.模板
entity
##定义初始变量
#set($tableName = $tableInfo.name)
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/entity"))
##使用全局变量实现默认包导入
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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 java.io.Serializable;
@Data
@TableName("$!tool.hump2Underline($!{tableInfo.name})")
@ApiModel("$!{tableInfo.comment}")
@AllArgsConstructor
@NoArgsConstructor
public class $!{tableInfo.name} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})
#end
#if(${column.name} == "id")
@TableId(value = "id", type = IdType.AUTO)
#end
#if(${column.name} != "id")
@TableField("$!tool.hump2Underline($column.name)")
#end
@ApiModelProperty("${column.comment}")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
dao(mapper)
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Dao"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.apache.ibatis.annotations.Param;
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}> {
}
service
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.vo.$!{tableInfo.name}VO;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务接口
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends IService<$!{tableInfo.name}> {
IPage<$!{tableInfo.name}VO> selectPageVo(Integer pageNum, Integer pageSize);
}
serviceImpl
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.vo.$!{tableInfo.name}VO;
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
*
* @author $!author
* @since $!time.currTime()
*/
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
@Resource
private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
@Override
public IPage<$!{tableInfo.name}VO> selectPageVo(Integer pageNum, Integer pageSize) {
return $tool.append($!tool.firstLowerCase($tableInfo.name), 'Dao.selectPageVo(new Page(pageNum, pageSize));')
}
}
controller
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import $!{tableInfo.savePackageName}.dto.$!{tableInfo.name}DTO;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.vo.$!{tableInfo.name}VO;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@Api(tags = "$!{tableInfo.comment}($!{tableInfo.name})")
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
/**
* $!{tableInfo.name}服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
@GetMapping("/$!{tableInfo.name}/{id}")
@ApiOperation(value = "根据id查询$!{tableInfo.name}")
@ApiImplicitParams({
@ApiImplicitParam( name = "id", value = "id",dataTypeClass = String.class,required = true)
})
public R<$!{tableInfo.name}VO> $!{tableInfo.name}(@PathVariable("id") String id) {
$!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name) = $tool.append($!tool.firstLowerCase($tableInfo.name), 'Service.query().eq("id", id).one();')
$!{tableInfo.name}VO $!tool.firstLowerCase($tableInfo.name)VO = BeanUtil.copyProperties($!tool.firstLowerCase($tableInfo.name), $!{tableInfo.name}VO.class);
return R.ok($!tool.firstLowerCase($tableInfo.name)VO);
}
@PostMapping("/insert")
@ApiOperation(value = "新增$!{tableInfo.name}")
public R<$!{tableInfo.name}VO> insert(@RequestBody $!{tableInfo.name}DTO $!tool.firstLowerCase($tableInfo.name)DTO) {
$!{tableInfo.name} $!tool.firstLowerCase($tableInfo.name) = BeanUtil.copyProperties($!tool.firstLowerCase($tableInfo.name)DTO, $!{tableInfo.name}.class);
boolean flag = $tool.append($!tool.firstLowerCase($tableInfo.name), "Service.save($!tool.firstLowerCase($tableInfo.name));")
if (BooleanUtil.isTrue(flag)) {
return R.ok("");
}
return R.fail("插入失败");
}
@GetMapping("/page")
@ApiOperation(value = "分页")
@ApiImplicitParams({
@ApiImplicitParam( name = "pageNum", value = "当前页码,默认值1",dataTypeClass = Integer.class,required = false),
@ApiImplicitParam( name = "pageSize", value = "每页个数,默认值10",dataTypeClass = Integer.class,required = false)
})
public R<$!{tableInfo.name}VO> selectPageVo(@RequestParam(required = false,defaultValue ="1") Integer pageNum, @RequestParam(required = false,defaultValue="10") Integer pageSize) {
IPage<$!{tableInfo.name}VO> $!tool.firstLowerCase($tableInfo.name)PageVO = $tool.append($!tool.firstLowerCase($tableInfo.name), 'Service.selectPageVo(pageNum,pageSize);')
return R.ok($!tool.firstLowerCase($tableInfo.name)PageVO);
}
}
mapper.xml
##引入mybatis支持
$!mybatisSupport
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
##$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Dao">
<resultMap id="BaseResultMap" type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">
<!--@Table $!{tableInfo.originTableName}-->
#foreach($column in $tableInfo.fullColumn)
<result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
</resultMap>
<!--<association property="属性名" javaType="对应的Java类型" column="{自定义的参数名=查询的字段名}" select="查询语句所在的路径"></association>-->
<!--<collection property="属性名" javaType="对应的Java类型" column="{自定义的参数名=查询的字段名}" ofType="关联的对象类型" select="查询语句所在的路径"></collection>-->
<!--<foreach item="item" index="index" collection="ids.split(',')这里可以接受逗号分隔字符串,也能接受数组" open="(" separator="," close=")">#{item}</foreach>-->
</mapper>
vo
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "VO"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/vo"))
##使用全局变量实现默认包导入
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* $!{tableInfo.comment}($!{tableInfo.name})VO
*
* @author $!author
* @since $!time.currTime()
*/
@Data
@ApiModel("$!{tableInfo.comment}VO")
@AllArgsConstructor
@NoArgsConstructor
public class $!{tableInfo.name}VO implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
@ApiModelProperty("${column.comment}")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
dto
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "DTO"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dto"))
##使用全局变量实现默认包导入
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* $!{tableInfo.comment}($!{tableInfo.name})DTO
*
* @author $!author
* @since $!time.currTime()
*/
@Data
@ApiModel("$!{tableInfo.comment}DTO")
@AllArgsConstructor
@NoArgsConstructor
public class $!{tableInfo.name}DTO implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
@ApiModelProperty("${column.comment}")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}