EasyCode外掛程式碼生成模板
阿新 • • 發佈:2021-07-07
EasyCode外掛程式碼生成模板
程式碼模板截圖
entity.java
##引入巨集定義 $!define $!init ##使用巨集定義設定回撥(儲存位置與檔案字尾) #save("/entity", ".java") ##使用巨集定義設定包字尾 #setPackageSuffix("entity") ##使用全域性變數實現預設包匯入 $!autoImport import lombok.Data; ##使用巨集定義實現類註釋資訊 #tableComment("實體類") @Data public class $!{tableInfo.name} { #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
dao.java
$!init ##定義初始變數 #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; /** * $!{tableInfo.comment}($!{tableInfo.name})表資料庫訪問層 * *@author $!author * @since $!time.currTime() */ public interface $!{tableName} { /** * 新增資料 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 例項物件 * @return 影響行數 */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通過recordNo刪除資料 * * @param recordNo 記錄編號 * @return 影響行數 */ int deleteByRecordNo(String recordNo); /** * 修改資料 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 例項物件 * @return 影響行數 */ int updateByRecordNo($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通過recordNo查詢單條資料 * * @param recordNo 記錄編號 * @return 例項物件 */ $!{tableInfo.name} queryByRecordNo(String recordNo); /** * 通過實體作為篩選條件查詢 * * @param $tool.firstLowerCase($!{tableInfo.name})DQO 查詢條件 * @return 物件列表 */ List<$!{tableInfo.name}> queryByCondition($!{tableName}DQO $tool.firstLowerCase($!{tableInfo.name})DQO); }
service.java
$!init ##定義初始變數 #set($tableName = $tool.append("I", $tableInfo.name, "Service")) ##設定回撥 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/intefaces")) ##拿到主鍵 #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 $!{tableInfo.savePackageName}.pojo.dio.$!{tableInfo.name}DIO; import java.util.List; import com.powater.common.bean.PageInfoListResult; /** * $!{tableInfo.comment}($!{tableInfo.name})表服務介面 * * @author $!author * @since $!time.currTime() */ public interface $!{tableName} { /** * 新增資料 * * @param $!tool.firstLowerCase($!{tableInfo.name})DIO 例項物件 * @return 例項物件 */ String insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})DIO); /** * 通過recordNo刪除資料 * * @param recordNo 唯一標識 * @return 是否成功 */ String deleteByRecordNo(String recordNo); /** * 修改資料 * * @param $!tool.firstLowerCase($!{tableInfo.name})DIO 例項物件 * @return 例項物件 */ String updateByRecordNo($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})DIO); /** * 通過recordNo查詢單條資料 * * @param recordNo 唯一標誌 * @return 例項物件 */ $!{tableInfo.name} queryByRecordNo(String recordNo); /** * 通過條件查詢單條資料 * * @param $tool.firstUpperCase($!{tableInfo.name})DQO 查詢條件 * @return 分頁物件 */ PageInfoListResult<List<$!{tableInfo.name}DOO>> queryByCondition($!{tableInfo.name}DQO $tool.firstUpperCase($!{tableInfo.name})DQO); }
serviceImpl.java
$!init ##定義初始變數 #set($tableName = $tool.append($tableInfo.name, "Service")) ##設定回撥 $!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 com.powater.common.bean.PageInfoListResult; import org.springframework.beans.factory.annotation.Autowired; 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} implements $!{tableInfo.name}Service {
@Autowired private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao; @Override public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return null; } @Override public String deleteByRecordNo(String recordNo) { return null; } @Override public String updateByRecordNo($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return null; } @Override public $!{tableInfo.name}DOO queryByRecordNo(String recordNo) { return null; } @Override public PageInfoListResult<List<$!{tableInfo.name}DOO>> queryByCondition($!{tableInfo.name}DQO $tool.firstUpperCase($!{tableInfo.name})DQO) { return null; } }
controller.java
$!init ##定義初始變數 #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 org.springframework.web.bind.annotation.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import javax.annotation.Resource; /** * $!{tableInfo.comment}($!{tableInfo.name})表控制層 * * @author $!author * @since $!time.currTime() */ @RestController @RequestMapping("/$!tool.firstLowerCase($tableInfo.name)") @Api(value = "$!{tableInfo.comment}相關介面", tags = "$!{tableInfo.comment}相關介面") public class $!{tableName} { @Autowired private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service; @ApiOperation(value = "新增-修改",httpMethod = "POST") @PostMapping("/save") public CommonResult save(@RequestBody $!{tableInfo.name}DIO dio) { return null; } @ApiOperation(value = "刪除",httpMethod = "GET") @GetMapping("/delete") public CommonResult delete(@RequestParam(value = "recordNo") String recordNo) { return null; } @ApiOperation(value = "根據唯一標誌獲取詳情",httpMethod = "GET") @GetMapping("/detail") public CommonResult<$!{tableInfo.name}DOO> getDetail(@RequestParam(value = "recordNo") String recordNo) { return null; } @ApiOperation(value = "根據條件查詢列表",httpMethod = "POST") @PostMapping("/page/list") public CommonResult< PageInfoListResult<List<$!{tableInfo.name}DOO>>> getPageListByCondition(@RequestBody $!{tableInfo.name}DQO dqo) { return null; } }
mapper.xml
$!init ##引入mybatis支援 $!mybatisSupport ##設定儲存名稱與儲存位置 $!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 id="BaseResultMap" type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}"> #foreach($column in $tableInfo.fullColumn) <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/> #end </resultMap> <resultMap id="DOOResultMap" type="$!{tableInfo.savePackageName}.pojo.doo.$!{tableInfo.name}DOO"> #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> <sql id="DOO_Column_List"> #allSqlColumn() </sql> <!--新增所有列--> <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true"> insert into $!{tableInfo.obj.name} <trim prefix="(" suffix=")" suffixOverrides="," > #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end"> `$!column.obj.name`, </if> #end </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > #foreach($column in $tableInfo.fullColumn) <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end"> #{$!column.name,jdbcType=$!column.ext.jdbcType}, </if> #end </trim> </insert> <!--通過recordNo修改資料--> <update id="updateByRecordNo"> update $!{tableInfo.obj.name} <set> #foreach($column in $tableInfo.otherColumn) <if test="$!column.name != null"> $!column.obj.name = #{$!column.name}, </if> #end </set> where record_no = #{recordNo} and del_flag = 0 </update> <!--通過recordNo邏輯刪除--> <delete id="deleteByRecordNo"> update $!{tableInfo.obj.name} set del_flag = 1 where record_no = #{recordNo} and del_flag = 0 </delete> <!--查詢單個--> <select id="queryByRecordNo" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from $!tableInfo.obj.name where record_no = #{recordNo} </select> <!--通過實體作為篩選條件查詢--> <select id="queryByCondition" resultMap="DOOResultMap"> select <include refid="Base_Column_List" /> 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> </mapper>
doo.java
##引入巨集定義 $!define $!init #set($tableName = $tool.append($tableInfo.name, "DOO")) ##使用巨集定義設定回撥(儲存位置與檔案字尾) #save("/pojo/doo", "DOO.java") ##使用巨集定義設定包字尾 #setPackageSuffix("pojo.doo") ##使用全域性變數實現預設包匯入 $!autoImport import lombok.Data; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; ##使用巨集定義實現類註釋資訊 #tableComment("doo") @Data public class $!{tableName} { #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end @ApiModelProperty(value = "${column.comment}") private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
dio.java
##引入巨集定義 $!define $!init #set($tableName = $tool.append($tableInfo.name, "DIO")) ##使用巨集定義設定回撥(儲存位置與檔案字尾) #save("/pojo/dio", "DIO.java") ##使用巨集定義設定包字尾 #setPackageSuffix("pojo.dio") ##使用全域性變數實現預設包匯入 $!autoImport import lombok.Data; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; ##使用巨集定義實現類註釋資訊 #tableComment("dio") @Data @ApiModel(description = "$!{tableInfo.comment}-dio") public class $!{tableName} { #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end @ApiModelProperty(value = "${column.comment}") private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end #* #foreach($column in $tableInfo.fullColumn) ##使用巨集定義實現get,set方法 #getSetMethod($column) #end *# }
dqo.java
##引入巨集定義 $!define $!init #set($tableName = $tool.append($tableInfo.name, "DQO")) ##使用巨集定義設定回撥(儲存位置與檔案字尾) #save("/pojo/dqo", "DQO.java") ##使用巨集定義設定包字尾 #setPackageSuffix("pojo.dqo") ##使用全域性變數實現預設包匯入 $!autoImport import lombok.Data; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; ##使用巨集定義實現類註釋資訊 #tableComment("dqo") @Data @ApiModel(description = "$!{tableInfo.comment}-dqo") public class $!{tableName} { @ApiModelProperty(value = "當前頁號") private Integer currentPage; @ApiModelProperty(value = "每頁記錄條數") private Integer pageSize; }
全域性配置——mybatisSupport
##針對Mybatis 進行支援,主要用於生成xml檔案 #foreach($column in $tableInfo.fullColumn) ##儲存列型別 $tool.call($column.ext.put("sqlType", $tool.getField($column.obj.dataType, "typeName"))) #if($tool.newHashSet("java.lang.String").contains($column.type)) #set($jdbcType="VARCHAR") #elseif($tool.newHashSet("java.lang.Boolean", "boolean").contains($column.type)) #set($jdbcType="BOOLEAN") #elseif($tool.newHashSet("java.lang.Byte", "byte").contains($column.type)) #set($jdbcType="BYTE") #elseif($tool.newHashSet("java.lang.Integer", "int", "java.lang.Short", "short").contains($column.type)) #set($jdbcType="INTEGER") #elseif($tool.newHashSet("java.lang.Long", "long").contains($column.type)) #set($jdbcType="INTEGER") #elseif($tool.newHashSet("java.lang.Float", "float", "java.lang.Double", "double").contains($column.type)) #set($jdbcType="NUMERIC") #elseif($tool.newHashSet("java.util.Date", "java.sql.Timestamp", "java.time.Instant", "java.time.LocalDateTime", "java.time.OffsetDateTime", " java.time.ZonedDateTime").contains($column.type)) #set($jdbcType="TIMESTAMP") #elseif($tool.newHashSet("java.sql.Date", "java.time.LocalDate").contains($column.type)) #set($jdbcType="TIMESTAMP") #elseif($tool.newHashSet("java.math.BigDecimal").contains($column.type)) #set($jdbcType="DECIMAL") #else ##其他型別 #set($jdbcType="VARCHAR") #end $tool.call($column.ext.put("jdbcType", $jdbcType)) #end ##定義巨集,查詢所有列 #macro(allSqlColumn)#foreach($column in $tableInfo.fullColumn)$column.obj.name#if($velocityHasNext), #end#end#end
全域性配置——init
##初始化區域 ##去掉表的t_字首 $!tableInfo.setName($tool.getClassName($tableInfo.obj.name.replaceFirst("tb_",""))) ##參考阿里巴巴開發手冊,POJO 類中布林型別的變數,都不要加 is 字首,否則部分框架解析會引起序列化錯誤 #foreach($column in $tableInfo.fullColumn) #if($column.name.startsWith("is") && $column.type.equals("java.lang.Boolean")) $!column.setName($tool.firstLowerCase($column.name.substring(2))) #end #end ##實現動態排除列 #set($temp = $tool.newHashSet("testCreateTime", "otherColumn")) #foreach($item in $temp) #set($newList = $tool.newArrayList()) #foreach($column in $tableInfo.fullColumn) #if($column.name!=$item) ##帶有反回值的方法呼叫時使用$tool.call來消除返回值 $tool.call($newList.add($column)) #end #end ##重新儲存 $tableInfo.setFullColumn($newList) #end ##對importList進行篡改 #set($temp = $tool.newHashSet()) #foreach($column in $tableInfo.fullColumn) #if(!$column.type.startsWith("java.lang.")) ##帶有反回值的方法呼叫時使用$tool.call來消除返回值 $tool.call($temp.add($column.type)) #end #end ##覆蓋 #set($importList = $temp)
全域性配置——author
dbx
使用注意事項
要根據實際情況修改程式碼,模板中使用到了swagger的註解 ,要匯入相應的依賴。author為自定義的全域性配置,在生成程式碼的時候如果要去除表的字首,記得在全域性配置中的init中進行相關的修改
本文來自部落格園,作者:風靈4,轉載請註明原文連結:https://www.cnblogs.com/fengling4/p/14982147.html