IDEA-EasyCode使用
IDEA-EasyCode使用 一键生成单表后端代码,包括entity,controller,service,serviceImpl,dao,mapper
1.安装idea插件
打开设置中的plugins
搜索安装EasyCode
搜索安装lombok
重启idea
2.使用Database连接数据库
注意
连接中多个数据库的,在Database中指定数据库
修改数据库字段后,需同步表。右击表,点击synchronize
3.EasyCode生成代码 步骤
自定义模板配置 Type Mapper
Template Setting Table Editor Config
entity.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ##引入宏定义 $!define ##使用宏定义设置回调(保存位置与文件后缀) #save("/entity", ".java") ##使用宏定义设置包后缀 #setPackageSuffix("entity") ##使用全局变量实现默认包导入 $!autoImport import java.io.Serializable; import lombok.Data; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; ##使用宏定义实现类注释信息 #tableComment("实体类") @Data @ApiModel public class $!{tableInfo.name} implements Serializable { private static final long serialVersionUID = $!tool.serial(); #foreach($column in $tableInfo.fullColumn) #if(${column.comment})@ApiModelProperty(value = "${column.comment}") #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end #*#foreach($column in $tableInfo.fullColumn) ##使用宏定义实现get,set方法 #getSetMethod($column) #end*# }
dao.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 ##引入宏定义 $!define ##定义初始变量 #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; ##使用宏定义实现类注释信息 #tableComment("表数据库访问层") public interface $!{tableName} { /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ $!{tableInfo.name} queryById($!pk.shortType $!pk.name); /** * 通过实体作为筛选条件查询 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 对象列表 */ List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 新增数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 影响行数 */ int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 修改数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 影响行数 */ int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通过主键删除数据 * * @param $!pk.name 主键 * @return 影响行数 */ int deleteById($!pk.shortType $!pk.name); }
service.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 ##引入宏定义 $!define ##定义初始变量 #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 java.util.List; ##使用宏定义实现类注释信息 #tableComment("表服务接口") public interface $!{tableName} { /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ $!{tableInfo.name} queryById($!pk.shortType $!pk.name); /** * 新增数据 * * @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); }
serviceImpl.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 ##引入宏定义 $!define ##定义初始变量 #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 javax.annotation.Resource; import java.util.List; ##使用宏定义实现类注释信息 #tableComment("表服务实现类") @Service("$!tool.firstLowerCase($!{tableInfo.name})Service") public class $!{tableName} implements $!{tableInfo.name}Service { @Resource private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao; /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ @Override public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryById($!pk.name); } /** * 新增数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 实例对象 */ @Override public $!{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 实例对象 */ @Override public $!{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 是否成功 */ @Override public boolean deleteById($!pk.shortType $!pk.name) { return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name) > 0; } }
controller.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 ##引入宏定义 $!define ##定义初始变量 #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 javax.annotation.Resource; ##使用宏定义实现类注释信息 #tableComment("表控制层") @RestController @RequestMapping("$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { /** * 服务对象 */ @Resource private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service; /** * 通过主键查询单条数据 * * @param id 主键 * @return 单条数据 */ @GetMapping("selectOne") public $!{tableInfo.name} selectOne($!pk.shortType id) { return this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id); } }
mapper.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 ##引入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}"> <!--@Table $!{tableInfo.originTableName}--> #foreach($column in $tableInfo.fullColumn) <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/> #end </resultMap> <!--查询单个--> <select id="queryById" resultMap="BaseResultMap"> select #allSqlColumn() from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name where $!pk.obj.name = #{$!pk.name} </select> <!--通过实体作为筛选条件查询--> <select id="queryAll" resultMap="BaseResultMap"> select #allSqlColumn() from $!{tableInfo.obj.parent.name}.$!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" keyProperty="$!pk.name" useGeneratedKeys="true"> insert into $!{tableInfo.obj.parent.name}.$!{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> <!--通过主键修改数据--> <update id="update"> update $!{tableInfo.obj.parent.name}.$!{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"> delete from $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name} </delete> </mapper>
RespVO.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ##引入宏定义 $!define ##使用宏定义设置回调(保存位置与文件后缀) #save("/entity/vo", "RespVO.java") ##使用宏定义设置包后缀 #setPackageSuffix("entity.vo") ##使用全局变量实现默认包导入 $!autoImport import java.io.Serializable; import lombok.Data; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; ##使用宏定义实现类注释信息 #tableComment("返回传输类") @Data @ApiModel public class $!{tableInfo.name}RespVO implements Serializable { private static final long serialVersionUID = $!tool.serial(); #foreach($column in $tableInfo.fullColumn) #if(${column.name} != "isDel" && ${column.name} != "creator" && ${column.name} != "createTime" && ${column.name} != "modifier" && ${column.name} != "modifiTime") #if(${column.comment})@ApiModelProperty(value = "${column.comment}") #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end #end }
Global Config mybatisSupport 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ##针对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="TINYINT") #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="BIGINT") #elseif($tool.newHashSet("java.lang.Float", "float", "java.lang.Double", "double").contains($column.type)) #set($jdbcType="NUMERIC") #elseif($tool.newHashSet("java.sql.Date", "java.time.LocalDateTime").contains($column.type)) #set($jdbcType="TIMESTAMP") #elseif($tool.newHashSet("java.math.BigDecimal").contains($column.type)) #set($jdbcType="DECIMAL") #else ##其他类型 #set($jdbcType="OTHER") #end $tool.call($column.ext.put("jdbcType", $jdbcType)) #end ##定义宏,查询所有列 #macro(allSqlColumn)#foreach($column in $tableInfo.fullColumn)$column.obj.name#if($velocityHasNext), #end#end#end