1、安装easycode
打开settings,在plugins中搜索easycode进行安装,安装完成后重启idea。
2、连接数据库
点解Test connetction测试连接,
3、生成文件
右键数据库表格,生成对应文件
4、修改模板
如果需要修改文件生成模板,可以通过在setting里面搜索easycode,导入easycode模板或者修改模板
5、模板代码修改示例
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 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;/*** $!{tableInfo.comment}($!{tableInfo.name})表控制层** @author $!author* @since $!time.currTime()*/
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {/*** 服务对象*/@Resourceprivate $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;/*** 分页查询** @param $!{tool.firstLowerCase($tableInfo.name)} 筛选条件* @param pageRequest 分页对象* @return 查询结果*/@GetMappingpublic ResponseEntity<Page<$!{tableInfo.name}>> queryByPage($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, PageRequest pageRequest) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryByPage($!{tool.firstLowerCase($tableInfo.name)}, pageRequest));}/*** 通过主键查询单条数据** @param id 主键* @return 单条数据*/@GetMapping("{id}")public ResponseEntity<$!{tableInfo.name}> queryById(@PathVariable("id") $!pk.shortType id) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id));}/*** 新增数据** @param $!{tool.firstLowerCase($tableInfo.name)} 实体* @return 新增结果*/@PostMappingpublic ResponseEntity<$!{tableInfo.name}> add($!{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 编辑结果*/@PutMappingpublic ResponseEntity<$!{tableInfo.name}> edit($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)}));}/*** 删除数据** @param id 主键* @return 删除是否成功*/@DeleteMappingpublic ResponseEntity<Boolean> deleteById($!pk.shortType id) {return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id));}}
dao
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao/mapper"))##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())#set($pk = $tableInfo.pkColumn.get(0))
#end#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}mapper;import com.tl.mcs.entity.po.integrate1.$!{tableInfo.name};
import java.util.List;/*** $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层** @author wangJunJie* @since $!time.currTime()*/
public interface $!{tableName} {/*** 通过ID查询单条数据** @param $!pk.name 主键* @return 实例对象*/$!{tableInfo.name} queryById($!pk.shortType $!pk.name);/*** 通过封装条件查询多条数据** @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象* @return 实例对象*/List<$!{tableInfo.name}> queryForAll($!{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);}
entity
##引入宏定义
$!{define.vm}##使用宏定义设置回调(保存位置与文件后缀)
#save("/integrate1", ".java")##使用宏定义设置包后缀
#setPackageSuffix("integrate1")##使用全局变量实现默认包导入
$!{autoImport.vm}
import com.tl.mcs.utils.sql.annotation.Column;
import com.tl.mcs.utils.sql.annotation.Table;
import java.io.Serializable;##使用宏定义实现类注释信息
#tableComment("实体类")
@Table("$!tableInfo.obj.name")
public class $!{tableInfo.name} implements Serializable {private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)#if(${column.comment})/*** ${column.comment}*/#end@Column("$!column.obj.name")private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
#getSetMethod($column)
#end}
mapper
##引入mybatis支持
$!{mybatisSupport.vm}##设置保存名称与保存位置
$!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}.$!{tableInfo.name}Mapper"><resultMap type="com.tl.mcs.entity.po.integrate1.$!{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" resultMap="$!{tableInfo.name}Map">select<include refid="Base_Column_List" />from $!tableInfo.obj.namewhere $!pk.obj.name = #{$!pk.name}</select><!--查询多个--><select id="queryForAll" resultMap="$!{tableInfo.name}Map">select<include refid="Base_Column_List" />from $!tableInfo.obj.name<where>#foreach($column in $tableInfo.otherColumn)<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.name}(#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($foreach.hasNext),#end</if>#end)values (#foreach($column in $tableInfo.fullColumn)<if test="$!column.name != null #if($!column.type.equals("java.lang.String")) and $!column.name != ''#end">#{$!column.name}#if($foreach.hasNext),#end</if>#end)</insert><!--通过主键修改数据--><update id="update">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">delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}</delete></mapper>
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;/*** $!{tableInfo.comment}($!{tableInfo.name})表服务接口** @author $!author* @since $!time.currTime()*/
public interface $!{tableName} {/*** 通过ID查询单条数据** @param $!pk.name 主键* @return 实例对象*/$!{tableInfo.name} queryById($!pk.shortType $!pk.name);/*** 分页查询** @param $!tool.firstLowerCase($!{tableInfo.name}) 筛选条件* @param pageRequest 分页对象* @return 查询结果*/Page<$!{tableInfo.name}> queryByPage($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), PageRequest pageRequest);/*** 新增数据** @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
##定义初始变量
#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 javax.annotation.Resource;/*** $!{tableInfo.comment}($!{tableInfo.name})表服务实现类** @author $!author* @since $!time.currTime()*/
@Service("$!tool.firstLowerCase($!{tableInfo.name})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 pageRequest 分页对象* @return 查询结果*/@Overridepublic Page<$!{tableInfo.name}> queryByPage($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}, PageRequest pageRequest) {long total = this.$!{tool.firstLowerCase($tableInfo.name)}Dao.count($!{tool.firstLowerCase($tableInfo.name)});return new PageImpl<>(this.$!{tool.firstLowerCase($tableInfo.name)}Dao.queryAllByLimit($!{tool.firstLowerCase($tableInfo.name)}, pageRequest), pageRequest, total);}/*** 新增数据** @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;}
}