1. dao 层:
##定义初始变量
#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.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Pageable;
import java.util.List;/*** $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层** @Author $!author* @Datetime $!time.currTime()*/
@Mapper
public interface $!{tableName} {/*** 通过ID查询单条数据** @param $!pk.name 主键* @return 实例对象*/$!{tableInfo.name} queryById($!pk.shortType $!pk.name);/*** 查询指定行数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 查询条件* @return 对象列表*/List<$!{tableInfo.name}> queryAllByLimit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));/*** 统计总行数** @param $!tool.firstLowerCase($!{tableInfo.name}) 查询条件* @return 总行数*/long count($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));/*** 新增数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @return 影响行数*/int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));/*** 批量新增数据(MyBatis原生foreach方法)** @param entities List<$!{tableInfo.name}> 实例对象列表* @return 影响行数*/int insertBatch(@Param("entities") List<$!{tableInfo.name}> entities);/*** 批量新增或按主键更新数据(MyBatis原生foreach方法)** @param entities List<$!{tableInfo.name}> 实例对象列表* @return 影响行数* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参*/int insertOrUpdateBatch(@Param("entities") List<$!{tableInfo.name}> entities);/*** 修改数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @return 影响行数*/int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));/*** 通过主键删除数据** @param $!pk.name 主键* @return 影响行数*/int deleteById($!pk.shortType $!pk.name);}
2. entity 层:
##引入宏定义
$!{define.vm}##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")##使用宏定义设置包后缀
#setPackageSuffix("entity")##使用全局变量实现默认包导入
$!{autoImport.vm}
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;##使用宏定义实现类注释信息
#tableComment("实体类")
@Schema(description = "$!{tableInfo.comment}实体类")
@Data
@Builder
public class $!{tableInfo.name} implements Serializable {private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)#if(${column.comment})/*** ${column.comment}*/#end@Schema(description = "${column.comment}")private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end}
3. mapper.xml 文件:
##引入mybatis支持
$!{mybatisSupport.vm}##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/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}.dao.$!{tableInfo.name}Dao"><resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
#foreach($column in $tableInfo.fullColumn)<result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end</resultMap><sql id="Base_Column_List" >#allSqlColumn() </sql><!--查询单个--><select id="queryById" parameterType="Integer" resultMap="$!{tableInfo.name}Map">select<include refid="Base_Column_List"></include>from $!tableInfo.obj.namewhere $!pk.obj.name = #{$!pk.name}</select><!--查询指定行数据--><select id="queryAllByLimit" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" resultMap="$!{tableInfo.name}Map">select<include refid="Base_Column_List"></include>from $!tableInfo.obj.name<where>
#foreach($column in $tableInfo.fullColumn)<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">and $!column.obj.name = #{$!column.name}</if>
#end</where></select><!--统计总行数--><select id="count" resultType="java.lang.Long">select count(1)from $!tableInfo.obj.name<where>
#foreach($column in $tableInfo.fullColumn)<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">and $!column.obj.name = #{$!column.name}</if>
#end</where></select><!--新增所有列--><insert id="insert" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" keyProperty="$!pk.name" useGeneratedKeys="true">insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)</insert><insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true">insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)values<foreach collection="entities" item="entity" separator=",">(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)</foreach></insert><insert id="insertOrUpdateBatch" keyProperty="$!pk.name" useGeneratedKeys="true">insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)values<foreach collection="entities" item="entity" separator=",">(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)</foreach>on duplicate key update#foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name)#if($velocityHasNext),#end#end</insert><!--通过主键修改数据--><update id="update" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}">update $!{tableInfo.obj.name}<set>
#foreach($column in $tableInfo.otherColumn)<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">$!column.obj.name = #{$!column.name},</if>
#end</set>where $!pk.obj.name = #{$!pk.name}</update><!--通过主键删除--><delete id="deleteById" parameterType="Integer" >delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}</delete></mapper>
4. 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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import com.github.pagehelper.PageInfo;/*** $!{tableInfo.comment}($!{tableInfo.name})表服务接口** @Author $!author* @Datetime $!time.currTime()*/
public interface $!{tableName} {/*** 通过ID查询单条数据** @param $!pk.name 主键* @return 实例对象*/$!{tableInfo.name} queryById($!pk.shortType $!pk.name);/*** 分页查询** @param $!tool.firstLowerCase($!{tableInfo.name}) 筛选条件* @param pageInfo 分页对象* @return 查询结果*/Page<$!{tableInfo.name}> queryByPage($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), PageInfo pageInfo);/*** 新增数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @return 实例对象*/$!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));/*** 修改数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @return 实例对象*/$!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));/*** 通过主键删除数据** @param $!pk.name 主键* @return 是否成功*/boolean deleteById($!pk.shortType $!pk.name);}
5. 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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;/*** $!{tableInfo.comment}($!{tableInfo.name})表服务实现类** @Author $!author* @Datetime $!time.currTime()*/
@Service
public class $!{tableName} implements $!{tableInfo.name}Service {@Resourceprivate $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;/*** 通过ID查询单条数据** @param $!pk.name 主键* @return 实例对象*/@Overridepublic $!{tableInfo.name} queryById($!pk.shortType $!pk.name) {return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryById($!pk.name);}/*** 分页查询** @param $!{tool.firstLowerCase($tableInfo.name)} 筛选条件* @param pageInfo 分页对象* @return 查询结果*/@Overridepublic Page<$!{tableInfo.name}> queryByPage($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, PageInfo pageInfo) {PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());return new PageImpl<>(this.$!{tool.firstLowerCase($tableInfo.name)}Dao.queryAllByLimit($!{tool.firstLowerCase($tableInfo.name)}));}/*** 新增数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @return 实例对象*/@Overridepublic $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.insert($!tool.firstLowerCase($!{tableInfo.name}));return $!tool.firstLowerCase($!{tableInfo.name});}/*** 修改数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @return 实例对象*/@Overridepublic $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.update($!tool.firstLowerCase($!{tableInfo.name}));return this.queryById($!{tool.firstLowerCase($!{tableInfo.name})}.get$!tool.firstUpperCase($pk.name)());}/*** 通过主键删除数据** @param $!pk.name 主键* @return 是否成功*/@Overridepublic boolean deleteById($!pk.shortType $!pk.name) {return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name) > 0;}
}
6. 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 $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.wl.dmp.plat.dto.PageHelperDO;
import com.alibaba.fastjson2.JSONObject;/*** $!{tableInfo.comment}($!{tableInfo.name})表控制层** @Author $!author* @Datetime $!time.currTime()*/
@Tag(name = "$!{tableInfo.comment}($!{tableInfo.name})控制器")
@RestController
@RequestMapping("/comm/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {/*** 服务对象*/@Resourceprivate $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;/*** 分页查询** @param pageRequest 分页对象* @return 查询结果*/@PostMapping("/queryByPage")@Operation(summary = "分页查询", description = "分页查询", tags = "queryByPage",method = "POST")@Parameters({@Parameter(name = "pageRequest", description = "分页对象", required = true)})public ResponseEntity<Page<$!{tableInfo.name}>> queryByPage(@RequestBody PageHelperDO pageRequest) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryByPage(JSONObject.parseObject(JSONObject.toJSONString(pageRequest.getObject()),$!{tableInfo.name}.class), pageRequest.getPageInfo()));}/*** 通过主键查询单条数据** @param id 主键* @return 单条数据*/@GetMapping("/queryById/{id}")@Operation(summary = "通过主键查询单条数据", description = "通过主键查询单条数据", tags = "queryById",method = "GET")@Parameters({@Parameter(name = "id", description = "主键id", required = true)})public ResponseEntity<$!{tableInfo.name}> queryById(@PathVariable $!pk.shortType id) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id));}/*** 新增数据** @param $!{tool.firstLowerCase($tableInfo.name)} 实体* @return 新增结果*/@PostMapping("/add")@Operation(summary = "新增数据", description = "新增数据", tags = "add",method = "POST")@Parameters({@Parameter(name = "$!{tool.firstLowerCase($tableInfo.name)}", description = "$!{tool.firstLowerCase($tableInfo.name)} 实体", required = true)})public ResponseEntity<$!{tableInfo.name}> add(@RequestParam $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!{tool.firstLowerCase($tableInfo.name)}));}/*** 编辑数据** @param $!{tool.firstLowerCase($tableInfo.name)} 实体* @return 编辑结果*/@PutMapping("/edit")@Operation(summary = "编辑数据", description = "编辑数据", tags = "edit",method = "PUT")@Parameters({@Parameter(name = "$!{tool.firstLowerCase($tableInfo.name)}", description = "$!{tool.firstLowerCase($tableInfo.name)} 实体", required = true)})public ResponseEntity<$!{tableInfo.name}> edit(@RequestParam $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)}));}/*** 删除数据** @param id 主键* @return 删除是否成功*/@DeleteMapping("/deleteById/{id}")@Operation(summary = "删除数据", description = "删除数据", tags = "deleteById",method = "DELETE")@Parameters({@Parameter(name = "id", description = "主键id", required = true)})public ResponseEntity<Boolean> deleteById(@PathVariable $!pk.shortType id) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id));}}
完美收工,一劳永逸,从此告别手写CRUD ,节省了一大笔时间!!!
补充运行环境:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.17</version></parent><groupId>swagger</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot-demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><artifactId>snakeyaml</artifactId><groupId>org.yaml</groupId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version><exclusions><exclusion><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></exclusion></exclusions></dependency><!-- <dependency>--><!-- <groupId>org.springframework.boot</groupId>--><!-- <artifactId>spring-boot-starter-security</artifactId>--><!-- </dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><!-- mysql数据库 --><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.2.0</version></dependency><!--Mybatis分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version></dependency><!--在线文档 --><dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>1.7.0</version><exclusions><exclusion><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.yaml</groupId><artifactId>snakeyaml</artifactId><version>2.2</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-springdoc-ui</artifactId><version>3.0.3</version></dependency><!--参数校验 --><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId></dependency><!-- 日志相关 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><!-- 取代log4j --><dependency><groupId>org.slf4j</groupId><artifactId>log4j-over-slf4j</artifactId><version>1.7.36</version></dependency><!-- xml解析库 --><dependency><groupId>org.dom4j</groupId><artifactId>dom4j</artifactId><version>2.1.3</version></dependency><!-- json解析库fastjson2 --><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId><version>2.0.17</version></dependency><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension</artifactId><version>2.0.17</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.3</version> <!-- 使用当前版本或更高版本 --></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins><resources><resource><directory>src/main/resources</directory></resource><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes></resource></resources></build></project>