网上抄的然后不能够使用,自己又改了该就可以了
entity ##引入宏定义 $!define ##使用宏定义设置回调(保存位置与文件后缀) #save("/entity", ".java") ##使用宏定义设置包后缀 #setPackageSuffix("entity") ##使用全局变量实现默认包导入 $!autoImport import lombok.Data; #foreach($column in $tableInfo.fullColumn) #if($column.type.equals("java.util.Date")) import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.format.annotation.DateTimeFormat; #break #end #end /** * $!{tableInfo.comment}($!{tableInfo.name})实体类 * * @author xiaoG * @since $!time.currTime() */ @Data public class $!{tableInfo.name} { #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end #if($column.type.equals("java.util.Date")) @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss") #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
dao ##定义初始变量 #set($tableName = $tool.append($tableInfo.name, "Mapper")) ##设置回调 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}mapper; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import com.sun.istack.internal.NotNull; import java.util.List; @Mapper public interface $!{tableName} { $!{tableInfo.name} getById(@NotNull $!pk.shortType $!pk.name); List<$!{tableInfo.name}> listByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); $!{tableInfo.name} getByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); List<$!{tableInfo.name}> listByIds(List<$!pk.shortType> list); int insert(@NotNull $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); int insertBatch(List<$!{tableInfo.name}> list); int update(@NotNull $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); int updateByField(@NotNull @Param("where") $!{tableInfo.name} where, @NotNull @Param("set") $!{tableInfo.name} set); int updateBatch(List<$!{tableInfo.name}> list); int deleteById(@NotNull $!pk.shortType $!pk.name); int deleteByEntity(@NotNull $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); int deleteByIds(List<$!pk.shortType> list); int countAll(); int countByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); }
service ##定义初始变量 #set($tableName = $tool.append("I",$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}.mapper.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import java.util.List; public interface $!{tableName} { $!{tableInfo.name}Mapper get$!{tableInfo.name}Mapper(); $!{tableInfo.name} getById($!pk.shortType $!pk.name); $!{tableInfo.name} getByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); List<$!{tableInfo.name}> listByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); List<$!{tableInfo.name}> listByIds(List<$!pk.shortType> ids); int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); int insertBatch(List<$!{tableInfo.name}> list); int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); int updateBatch(List<$!{tableInfo.name}> list); int deleteById($!pk.shortType $!pk.name); int deleteByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); int deleteByIds(List<$!pk.shortType> list); int countAll(); int countByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); }
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}.mapper.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.service.I$!{tableInfo.name}Service; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import javax.annotation.Resource; import org.springframework.stereotype.Service; import java.util.Date; import java.util.List; @Service public class $!{tableName} implements I$!{tableInfo.name}Service { @Resource(type = $!{tableInfo.name}Mapper.class) private $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper; @Override public $!{tableInfo.name}Mapper get$!{tableInfo.name}Mapper() { return $!tool.firstLowerCase($!{tableInfo.name})Mapper; } @Override public $!{tableInfo.name} getById($!pk.shortType $!pk.name) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.getById($!{pk.name}); } @Override public $!{tableInfo.name} getByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.getByEntity($!{tool.firstLowerCase($!{tableInfo.name})}); } @Override public List<$!{tableInfo.name}> listByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.listByEntity($!{tool.firstLowerCase($!{tableInfo.name})}); } @Override public List<$!{tableInfo.name}> listByIds(List<$!pk.shortType> ids) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.listByIds(ids); } @Override public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { //Date date = new Date(); //$!{tool.firstLowerCase($!{tableInfo.name})}.setCreateDatetime(date); //$!{tool.firstLowerCase($!{tableInfo.name})}.setUpdateDatetime(date); return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.insert($!{tool.firstLowerCase($!{tableInfo.name})}); } @Override public int insertBatch(List<$!{tableInfo.name}> list) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.insertBatch(list); } @Override public int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { //$!{tool.firstLowerCase($!{tableInfo.name})}.setUpdateDatetime(new Date()); return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.update($!{tool.firstLowerCase($!{tableInfo.name})}); } @Override public int updateBatch(List<$!{tableInfo.name}> list) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.updateBatch(list); } @Override public int deleteById($!pk.shortType $!pk.name) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteById($!pk.name); } @Override public int deleteByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteByEntity($!{tool.firstLowerCase($!{tableInfo.name})}); } @Override public int deleteByIds(List<$!pk.shortType> list) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteByIds(list); } @Override public int countAll() { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.countAll(); } @Override public int countByEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.countByEntity($!tool.firstLowerCase($!{tableInfo.name})); } }
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.I$!{tableInfo.name}Service; import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import java.util.List; @RestController @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { @Autowired private I$!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service; @GetMapping("/get/{$!pk.name}") public $!{tableInfo.name} getById(@PathVariable $!pk.shortType $!pk.name) { $tableInfo.name $!tool.firstLowerCase($tableInfo.name) = $!{tool.firstLowerCase($tableInfo.name)}Service.getById(id); return $!tool.firstLowerCase($tableInfo.name)!=null?$!tool.firstLowerCase($tableInfo.name):new $!{tableInfo.name}(); } @GetMapping("/get") public $!{tableInfo.name} getByEntity($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { return $!{tool.firstLowerCase($tableInfo.name)}Service.getByEntity($!{tool.firstLowerCase($!{tableInfo.name})}); } @GetMapping("/list") public List<$!{tableInfo.name}> list($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) { List<$tableInfo.name> $!{tool.firstLowerCase($tableInfo.name)}List = $!{tool.firstLowerCase($tableInfo.name)}Service.listByEntity($!{tool.firstLowerCase($!{tableInfo.name})}); return $!{tool.firstLowerCase($tableInfo.name)}List; } @PostMapping("/insert") public $tableInfo.name insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)){ $!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($tableInfo.name)); return $!tool.firstLowerCase($tableInfo.name); } @PutMapping("/update") public int update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)){ return $!{tool.firstLowerCase($tableInfo.name)}Service.update($!tool.firstLowerCase($tableInfo.name)); } @DeleteMapping("/delete/{$!pk.name}") public int deleteOne(@PathVariable $!pk.shortType $!pk.name){ return $!{tool.firstLowerCase($tableInfo.name)}Service.deleteById($!pk.name); } @DeleteMapping("/delete") public int deleteBatch(@RequestBody List<$!pk.shortType> $!{pk.name}s){ int result = 0; if ($!{pk.name}s!=null&&$!{pk.name}s.size()>0){ result = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteByIds($!{pk.name}s); } return result; } }
##引入mybatis支持 $!mybatisSupport ##设置保存名称与保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.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}.mapper.$!{tableInfo.name}Mapper"> <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}ResultMap"> #foreach($column in $tableInfo.fullColumn) <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/> #end </resultMap> <sql id="table_field"> #allSqlColumn() </sql> <!--通过Id查询单个--> <select id="getById" resultMap="$!{tableInfo.name}ResultMap" parameterType="$pk.type"> select <include refid="table_field" /> from $!tableInfo.obj.name where $!pk.obj.name = #{$!pk.name,jdbcType=$!pk.ext.jdbcType} </select> <!--通过实体不为空的属性作为筛选条件查询列表--> <select id="listByEntity" resultMap="$!{tableInfo.name}ResultMap" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}"> select <include refid="table_field" /> from $!tableInfo.obj.name <where> #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null"> and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType} </if> #end </where> </select> <!--通过实体不为空的属性作为筛选条件查询单个--> <select id="getByEntity" resultMap="$!{tableInfo.name}ResultMap" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}"> select <include refid="table_field" /> from $!tableInfo.obj.name <where> #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null"> and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType} </if> #end </where> </select> <!--通过Id列表作为筛选条件查询列表,列表长度不为0--> <select id="listByIds" resultMap="$!{tableInfo.name}ResultMap" parameterType="list"> select <include refid="table_field" /> from $!tableInfo.obj.name where $!pk.obj.name in <foreach item="item" collection="list" separator="," open="(" close=")" index="index"> #{item} </foreach> </select> <!--新增实体属性不为null的列--> <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}"> insert into $!{tableInfo.obj.name} <trim prefix="(" suffix=")" suffixOverrides=","> #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null"> $!column.obj.name, </if> #end </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null"> #{$!column.name,jdbcType=$!column.ext.jdbcType}, </if> #end </trim> </insert> <!--批量新增所有列,列表长度不能为0,且列表id统一为null或者统一不为null--> <insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true" parameterType="list"> insert into $!{tableInfo.obj.name} (#foreach($column in $tableInfo.fullColumn)$!{column.obj.name}#if($velocityHasNext), #end#end) values <foreach item="item" collection="list" separator="," open="" close="" index="index"> (#foreach($column in $tableInfo.fullColumn)#{item.$!{column.name},jdbcType=$!column.ext.jdbcType}#if($velocityHasNext), #end#end) </foreach> </insert> <!--通过主键修改实体属性不为null的列--> <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,jdbcType=$!column.ext.jdbcType}, </if> #end </set> where $!pk.obj.name = #{$!pk.name,jdbcType=$!pk.ext.jdbcType} </update> <!--通过表字段修改实体属性不为null的列--> <update id="updateByField"> update $!{tableInfo.obj.name} <set> #foreach($column in $tableInfo.otherColumn) <if test="where.$!{column.name} == null and set.$!{column.name} != null#if($column.type.equals("java.lang.String")) and set.$!{column.name} != ''#end"> $!column.obj.name = #{set.$!{column.name},jdbcType=$!column.ext.jdbcType}, </if> #end </set> <where> #foreach($column in $tableInfo.fullColumn) <if test="where.$!{column.name} != null"> and $!column.obj.name = #{where.$!{column.name},jdbcType=$!column.ext.jdbcType} </if> #end </where> </update> <!--通过主键修改实体列表,列表长度不能为0,注意:当实体属性为null时,对应的列也会别更新为null--> <update id="updateBatch" parameterType="list"> update $!{tableInfo.obj.name} <trim prefix="set" suffixOverrides=","> #foreach($column in $tableInfo.otherColumn) <trim prefix="$!{column.obj.name} = case" suffix="end,"> <foreach collection="list" item="item" index="index"> when $!pk.obj.name = #{item.$!pk.name} then #{item.$!column.name} </foreach> </trim> #end </trim> where $!pk.obj.name in <foreach collection="list" index="index" item="item" separator="," open="(" close=")"> #{item.$!pk.name,jdbcType=$!pk.ext.jdbcType} </foreach> </update> <!--通过主键删除--> <delete id="deleteById" parameterType="$pk.type"> delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name,jdbcType=$!pk.ext.jdbcType} </delete> <!--通过实体非空属性删除--> <delete id="deleteByEntity" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}"> delete from $!{tableInfo.obj.name} <where> #foreach($column in $tableInfo.otherColumn) <if test="$!column.name != null"> and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType} </if> #end </where> </delete> <!--通过主键列表删除,列表长度不能为0--> <delete id="deleteByIds" parameterType="list"> delete from $!{tableInfo.obj.name} where $!pk.obj.name in <foreach item="item" collection="list" separator="," open="(" close=")" index="index"> #{item} </foreach> </delete> <select id="countAll" resultType="int"> select count($!pk.obj.name) from $!{tableInfo.obj.name} </select> <select id="countByEntity" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" resultType="int"> select count($!pk.obj.name) from $!{tableInfo.obj.name} <where> #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null"> and $!column.obj.name = #{$!column.name,jdbcType=$!column.ext.jdbcType} </if> #end </where> </select> </mapper>
然后就是配置一下
application.yml # MyBatis mybatis: # 搜索指定包别名 typeAliasesPackage: com.rjj.**.**.entity # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapperLocations: classpath*:mapper/*Mapper.xml
结果