idea 使用 easyCode 插件快速生成代码,类似mybatis逆向生成代码
Posted L12345
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了idea 使用 easyCode 插件快速生成代码,类似mybatis逆向生成代码相关的知识,希望对你有一定的参考价值。
可参考EasyCode文档说明: https://gitee.com/makejava/EasyCode/wikis/pages
1.打开idea搜索并安装easyCode插件(以下截图为安装成功之后的截图)
2.安装成功之后可以找到插件自带的模板根据需求自行修改
a.根据需求自行添加数据库类型跟实体类类型的映射
b.修改mapper.xml文件生成的位置为与Mapper.java同一目录下(默认生成位置为 resources/mapper 目录下)
3.建立数据库链接
4.数据库连接之后打开,点击表名右键生成代码
点击Choose键或手动输入选择生成包路径,勾选想要生成的代码模板并点击ok即可
5.相关模板代码
entity.java
##引入宏定义 $!define ##使用宏定义设置回调(保存位置与文件后缀) #save("/entity", ".java") ##使用宏定义设置包后缀 #setPackageSuffix("entity") ##使用全局变量实现默认包导入 $!autoImport import java.io.Serializable; ##使用宏定义实现类注释信息 ###tableComment("实体类") /** * 表($!{tableInfo.obj.name})实体类 * 表说明:$!{tableInfo.comment} * @author $!author * @since $!time.currTime() */ public class $!{tableInfo.name} implements Serializable { private static final long serialVersionUID = $!tool.serial(); #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end /** * 分页查询-第几页 */ private Integer pageNum; /** * 分页查询-每页大小 */ private Integer pageSize; public Integer getPageNum() { return pageNum; } public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } #foreach($column in $tableInfo.fullColumn) ##使用宏定义实现get,set方法 #getSetMethod($column) #end }
service.java
##定义初始变量 #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.Map; import java.util.List; /** * 表($!{tableInfo.obj.name})服务接口 * 表说明:$!{tableInfo.comment} * @author $!author * @since $!time.currTime() */ public interface $!{tableName}{ /** * 插入 * @author $!author * @since $!time.currTime() * @param * @return */ int insertSelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通过id删除 * @author $!author * @since $!time.currTime() * @param id * @return */ int deleteByPrimaryKey(Long id); /** * 更新 * @author $!author * @since $!time.currTime() * @param * @return */ int updateByPrimaryKeySelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通过id查询 * @author $!author * @since $!time.currTime() * @param id * @return */ $!{tableInfo.name} selectByPrimaryKey(Long id); /** * 查询列表 * @author $!author * @since $!time.currTime() * @param paramMap * @return */ List<$!{tableInfo.name}> selectList(Map<String, Object> paramMap); }
serviceImpl.java
##定义初始变量 #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}.mapper.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Autowired; import java.util.Map; import java.util.List; /** * 表($!{tableInfo.obj.name})服务实现类 * 表说明:$!{tableInfo.comment} * @author $!author * @since $!time.currTime() */ @Service public class $!{tableName} implements $!{tableInfo.name}Service { @Autowired private $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper; /** * 插入 * @author $!author * @date $!time.currTime() * @param * @return */ @Override public int insertSelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { ## return $!tool.firstLowerCase($!{tableInfo.name})Mapper.insertSelective(); return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.insertSelective($!tool.firstLowerCase($!{tableInfo.name})); } /** * 通过id删除 * @author $!author * @date $!time.currTime() * @param id * @return */ @Override public int deleteByPrimaryKey(Long id) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteByPrimaryKey(id); } /** * 更新 * @author $!author * @date $!time.currTime() * @param * @return */ @Override public int updateByPrimaryKeySelective($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.updateByPrimaryKeySelective($!tool.firstLowerCase($!{tableInfo.name})); } /** * 通过id查询 * @author $!author * @date $!time.currTime() * @param id * @return */ @Override public $!{tableInfo.name} selectByPrimaryKey(Long id) { return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectByPrimaryKey(id); } /** * 查询列表 * @author $!author * @date $!time.currTime() * @param paramMap * @return */ @Override public List<$!{tableInfo.name}> selectList(Map<String, Object> paramMap) { ## return $!tool.firstLowerCase($!{tableInfo.name})Mapper.selectList(paramMap); return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectList(paramMap); } }
controller.java
##定义初始变量 #set($tableName = $tool.append($tableInfo.name, "RestController")) ##设置回调 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/restController")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end #if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}restController; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.gxjcxy.common.base.BaseController; import com.gxjcxy.common.util.ReturnData; import com.gxjcxy.common.util.MapUtils; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 表($!{tableInfo.obj.name})控制层 * 表说明:$!{tableInfo.comment} * @author $!author * @since $!time.currTime() */ @RestController @RequestMapping("$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} extends BaseController { @Autowired private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service; /** * 添加 * @author $!author * @date $!time.currTime() * @param request * @param response * @return */ @RequestMapping(value="/insert",produces = "application/json;charset=utf-8", method = RequestMethod.POST) public String insert(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, HttpServletRequest request, HttpServletResponse response){ int res = $!{tool.firstLowerCase($tableInfo.name)}Service.insertSelective($!{tool.firstLowerCase($tableInfo.name)}); return res>=1?ReturnData.ADD_SUCCESS().toString():ReturnData.ADD_FAIL().toString(); } /** * 删除 * @author $!author * @date $!time.currTime() * @param request * @param response * @return */ @RequestMapping(value="/delete",produces = "application/json;charset=utf-8", method = RequestMethod.POST) public String delete(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, HttpServletRequest request, HttpServletResponse response){ int res = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteByPrimaryKey($!{tool.firstLowerCase($tableInfo.name)}.getId()); return res>=1?ReturnData.DELETE_SUCCESS().toString():ReturnData.DELETE_FAIL().toString(); } /** * 更新 * @author $!author * @date $!time.currTime() * @param request * @param response * @return */ @RequestMapping(value="/update",produces = "application/json;charset=utf-8", method = RequestMethod.POST) public String update(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, HttpServletRequest request, HttpServletResponse response){ int res = $!{tool.firstLowerCase($tableInfo.name)}Service.updateByPrimaryKeySelective($!{tool.firstLowerCase($tableInfo.name)}); return res>=1?ReturnData.UPDATE_SUCCESS().toString():ReturnData.UPDATE_FAIL().toString(); } /** * 查询 * @author $!author * @date $!time.currTime() * @param request * @param response * @return */ @RequestMapping(value="/select",produces = "application/json;charset=utf-8",method = RequestMethod.POST) public String select(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, HttpServletRequest request, HttpServletResponse response){ $!{tableInfo.name} res = $!{tool.firstLowerCase($tableInfo.name)}Service.selectByPrimaryKey($!{tool.firstLowerCase($tableInfo.name)}.getId()); return ReturnData.SUCCESS(res).toString(); } /** * 分页查询列表 * @author $!author * @date $!time.currTime() * @param request * @param response * @return */ @RequestMapping(value="/selectList",produces = "application/json;charset=utf-8",method = RequestMethod.POST) public String selectList(@RequestBody(required = false) $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, HttpServletRequest request, HttpServletResponse response){ Map<String, Object> rMap = new HashMap<String, Object>(); if($!{tool.firstLowerCase($tableInfo.name)} == null){ $!{tool.firstLowerCase($tableInfo.name)} = new $!{tableInfo.name}(); } Map<String, Object> paramMap = MapUtils.beanToMap($!{tool.firstLowerCase($tableInfo.name)}); // 此处查询需要自行编写 PageHelper.startPage(getPageNum($!{tool.firstLowerCase($tableInfo.name)}.getPageNum()),getPageSize($!{tool.firstLowerCase($tableInfo.name)}.getPageSize())); List<$!{tableInfo.name}> lists = $!{tool.firstLowerCase($tableInfo.name)}Service.selectList(paramMap); PageInfo<$!{tableInfo.name}> p2 = new PageInfo<$!{tableInfo.name}>(lists); rMap.put("pageNum", p2.getPageNum()); // 当前页 rMap.put("pages", p2.getPages()); // 总页数 rMap.put("nextPage", p2.getNextPage()); // 下一页 rMap.put("prePage", p2.getPrePage()); // 上一页 rMap.put("data", p2.getList()); // 数据集合 rMap.put("count", p2.getTotal()); // 总行数 PageHelper.clearPage(); // 如果开启分页,必须调用清除 return ReturnData.SUCCESS(rMap).toString(); } }
mapper.xml
##引入mybatis支持 $!mybatisSupport ##设置保存名称与保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml")) $!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}Mapper"> <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="baseResultMap"> #foreach($column in $tableInfo.fullColumn) <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/> #end </resultMap> <sql id="baseColumnList"> #foreach($column in $tableInfo.fullColumn) $!column.obj.name#if($foreach.hasNext),#end#end </sql> <sql id="sql_where"> <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> </sql> <!--插入操作--> <insert id="insertSelective" parameterType="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" 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}}, </if> #end </trim> </insert> <!--根据id删除--> <delete id="deleteByPrimaryKey" parameterType="java.lang.Long"> delete from $!tableInfo.obj.name where $!pk.obj.name = #{$!pk.name} </delete> <!--更新操作--> <update id="updateByPrimaryKeySelective" 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> <!--根据id查询--> <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="baseResultMap"> select <include refid="baseColumnList" /> from $!tableInfo.obj.name where $!pk.obj.name = #{$!pk.name} </select> <!--列表查询--> <select id="selectList" parameterType="java.util.Map" resultMap="baseResultMap"> select
<include refid="baseColumnList"/>
from $!tableInfo.obj.name <include refid="sql_where" /> </select> </mapper>
mapper.java
##定义初始变量 #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 com.gxjcxy.common.base.BaseMapper;
/**
* 表($!{tableInfo.obj.name})数据库访问层
* 表说明:$!{tableInfo.comment}
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}> { }
文档说明:
说明文档: 属性 $author 设置中的作者 java.lang.String $encode 设置的编码 java.lang.String $modulePath 选中的module路径 java.lang.String $projectPath 项目绝对路径 java.lang.String 对象 $tableInfo 表对象 obj 表原始对象 com.intellij.database.model.DasTable name 表名(转换后的首字母大写)java.lang.String comment 表注释 java.lang.String fullColumn 所有列 java.util.List<ColumnInfo> pkColumn 主键列 java.util.List<ColumnInfo> otherColumn 其他列 java.util.List<ColumnInfo>,除主键以外的列 savePackageName 保存的包名 java.lang.String savePath 保存路径 java.lang.String saveModelName 保存的model名称 java.lang.String columnInfo 列对象 obj 列原始对象 com.intellij.database.model.DasColumn name 列名(首字母小写) java.lang.String comment 列注释 java.lang.String type 列类型(类型全名) java.lang.String shortType 列类型(短类型) java.lang.String custom 是否附加列 java.lang.Boolean ext 附加字段(Map类型) java.lang.Map<java.lang.String, java.lang.Object> $tableInfoList java.util.List<TableInfo>所有选中的表 $importList 所有需要导入的包集合 java.util.Set<java.lang.String> 回调 &callback setFileName(String) 设置文件储存名字 setSavePath(String) 设置文件储存路径,默认使用选中路径 工具 $tool firstUpperCase(String name) 首字母大写方法 firstLowerCase(String name) 首字母小写方法 getClsNameByFullName(String fullName) 通过包全名获取类名 getJavaName(String name) 将下划线分割字符串转驼峰命名(属性名) getClassName(String name) 将下划线分割字符串转驼峰命名(类名) append(Object... objs) 多个数据进行拼接 newHashSet(Object... objs) 创建一个HashSet对象 newArrayList(Object... objs) 创建一个ArrayList对象 newLinkedHashMap() 创建一个LinkedHashMap()对象 newHashMap() 创建一个HashMap()对象 getField(Object obj, String fieldName) 获取对象的属性值,可以访问任意修饰符修饰的属性.配合debug方法使用. call(Object... objs) 空白执行方法,用于调用某些方法时消除返回值 debug(Object obj) 调式方法,用于查询对象结构.可查看对象所有属性与public方法 serial() 随机获取序列化的UID service(String serviceName, Object... param)远程服务调用 parseJson(String) 将字符串转Map对象 toJson(Object, Boolean) 将对象转json对象,Boolean:是否格式化json,不填时为不格式化。 $time currTime(String format) 获取当前时间,指定时间格式(默认:yyyy-MM-dd HH:mm:ss) $generateService run(String, Map<String,Object>) 代码生成服务,参数1:模板名称,参数2:附加参数。
Global Config : init
##初始化区域 ##去掉表的t_前缀 #if($tableInfo.obj.name.startsWith("t_")) $!tableInfo.setName($tool.getClassName($tableInfo.obj.name.substring(2))) #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)
Global Config : define
##(Velocity宏定义) ###setPackageSuffix() ##定义设置表名后缀的宏定义,调用方式:#setTableSuffix("Test") #macro(setTableSuffix $suffix) #set($tableName = $!tool.append($tableInfo.name, $suffix)) #end ##定义设置包名后缀的宏定义,调用方式:#setPackageSuffix("Test") #macro(setPackageSuffix $suffix) #if($suffix!="")package #end#if($tableInfo.savePackageName!="")$!{tableInfo.savePackageName}.#{end}$!suffix; #end ##定义直接保存路径与文件名简化的宏定义,调用方式:#save("/entity", ".java") #macro(save $path $fileName) $!callback.setSavePath($tool.append($tableInfo.savePath, $path)) $!callback.setFileName($tool.append($tableInfo.name, $fileName)) #end ##定义表注释的宏定义,调用方式:#tableComment("注释信息") #macro(tableComment $desc) /** * $!{tableInfo.comment}($!{tableInfo.name})$desc * * @author $!author * @since $!time.currTime() */ #end ##定义GET,SET方法的宏定义,调用方式:#getSetMethod($column) #macro(getSetMethod $column) public $!{tool.getClsNameByFullName($column.type)} get$!{tool.firstUpperCase($column.name)}() { return $!{column.name}; } public void set$!{tool.firstUpperCase($column.name)}($!{tool.getClsNameByFullName($column.type)} $!{column.name}) { this.$!{column.name} = $!{column.name}; } #end
Global Config : autoImport
##自动导入包(仅导入实体属性需要的包,通常用于实体类) #foreach($import in $importList) import $!import; #end
Global Config : 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") #else ##其他类型 #set($jdbcType="OTHER自制Xcode合法插件EasyCode,快速生成代码实用分享一款基于 IntelliJ IDEA 开发的代码生成插件 EasyCode
IDEA插件系列(37):Easy Code插件——数据库表与java类代码快速生成