EasyCode 自动生成代码

1、EasyCode 插件安装

在 IDEA 的插件中查找 EasyCode,点击安装。

注意:如果使用默认配置和默认模板,请跳过第2、3步。

EasyCode 主要的配置:

  • Type Mapper 类型映射:java 类和数据库类的映射
  • Template 模板:自动生成的代码全靠模板

2、配置 EasyCode 全局信息

在 IDEA 的 setting 中配置 EasyCode,设置用户名(对应生成代码的 @author字段),支持自定义以模板的导入和导出,用户可以自定义

3、配置 Type Mapper 类型映射

复制默认分组(default),生成一份自己的分组(MyGroup),然后在自己的分组上进行个性化修改。不建议直接修改default。

  • columnType对应数据库表的字段类型,用正则表达式。
  • javaType对应生成的Java Entity中属性的类型。

同样可以点击+/-号添加/或删除映射关系。示例中数据库的timestamp类型和java.sql.Timestamp类对应

4、配置 Template 模板

为每个 Java 类(Controller, ServiceImpl, Mapper, Entity)配置一个生成模板,配置使用Velocity语法

复制一个分组。编辑模板,进行个性化定制,用到的属性对象信息参考下方的说明文档。

实时调试,关联一个已经建好的库表,点击右侧符便可预览根据这张表生成的Java类。

5、使用 EasyCode 生成 Java 类

选中 database 连接中的一张(也可以按住Ctr键,选择多张表),右键选择 “EasyCode-Generate Code”,在对话框选择模板分组(默认或自定义的)。

勾选要生成的Java类对应的模板和生成路径等信息,点击 OK 便可生成。

来自EasyCode官网

6、个人模板分享

6.1 JPA 的模板

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
{
"author" : "makejava",
"version" : "1.2.8",
"userSecure" : "",
"currTypeMapperGroupName" : "Default",
"currTemplateGroupName" : "JPA",
"currColumnConfigGroupName" : "Default",
"currGlobalConfigGroupName" : "Default",
"typeMapper" : { },
"template" : {
"JPA" : {
"name" : "JPA",
"elementList" : [ {
"name" : "entity.java",
"code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/entity\"))\n\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}entity;\n##使用全局变量实现默认包导入\n#foreach($import in $importList)\nimport $!import;\n#end\nimport java.io.Serializable;\n\nimport io.swagger.annotations.ApiModelProperty;\nimport lombok.Data;\nimport javax.persistence.Column;\nimport javax.persistence.Entity;\nimport javax.persistence.Table;\nimport javax.persistence.Id;\n\n##使用宏定义实现类注释信息\n/**\n * $!{tableInfo.comment}实体类\n *\n * @author $!author\n * @date $!time.currTime(\"yyyy/MM/dd\")\n */\n@Data\n@Entity\n@Table(name = \"$!{tool.hump2Underline($!{tableInfo.name})}\")\npublic class $!{tableInfo.name} implements Serializable {\n\n private static final long serialVersionUID = $!{tool.serial()};\n \n /**\n * ${pk.comment}\n */\n @ApiModelProperty(value = \"${pk.comment}\")\n @Id\n private $!{pk.shortType} $!{pk.name};\n\n#foreach($column in $tableInfo.otherColumn)\n #if(${column.comment})/**\n * ${column.comment}\n */#end\n\n @ApiModelProperty(value = \"${column.comment}\")\n @Column\n private $!{tool.getClsNameByFullName($column.type)} $!{column.name};\n\n#end\n}"
}, {
"name" : "repository.java",
"code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"Repository\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/repository\"))\n\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}repository;\n\nimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\n\nimport org.springframework.data.jpa.repository.JpaRepository;\nimport org.springframework.stereotype.Repository;\n/**\n * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层\n *\n * @author $!author\n * @date $!time.currTime(\"yyyy/MM/dd\")\n */\n@Repository\npublic interface $!{tableName} extends JpaRepository<$!{tableInfo.name} ,$!pk.shortType>{\n\n\n}"
}, {
"name" : "service.java",
"code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"Service\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/service\"))\n\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;\n\nimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\nimport java.util.List;\n\n/**\n * $!{tableInfo.comment}服务接口\n *\n * @author $!author\n * @date $!time.currTime(\"yyyy/MM/dd\")\n */\npublic interface $!{tableName} {\n /**\n * 创建$!{tableInfo.comment}\n *\n * @param $!tool.firstLowerCase($!{tableInfo.name}) $!{tableInfo.comment}\n * @return $!{tableInfo.comment}\n */\n $!{tableInfo.name} create$!{tableInfo.name}($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));\n\n /**\n * 删除$!{tableInfo.comment}\n *\n * @param $!pk.name $!{tableInfo.comment}的$!{pk.comment}\n */\n void delete$!{tableInfo.name}($!pk.shortType $!pk.name);\n\n /**\n * 获取$!{tableInfo.comment}列表\n *\n * @return $!{tableInfo.comment}列表\n */\n List<$!{tableInfo.name}> list$!{tableInfo.name}s();\n\n /**\n * 获取$!{tableInfo.comment}\n *\n * @param $!pk.name $!{tableInfo.comment}的$!{pk.comment}\n * @return $!{tableInfo.comment}\n */\n $!{tableInfo.name} get$!{tableInfo.name}($!pk.shortType $!pk.name);\n\n /**\n * 更新$!{tableInfo.comment}\n *\n * @param $!pk.name $!{tableInfo.comment}的$!{pk.comment}\n * @param $!tool.firstLowerCase($!{tableInfo.name}) $!{tableInfo.comment}\n * @return $!{tableInfo.comment}\n */\n $!{tableInfo.name} update$!{tableInfo.name}($!pk.shortType $!pk.name, $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));\n\n}"
}, {
"name" : "serviceImpl.java",
"code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"ServiceImpl\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/service/impl\"))\n\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;\n\nimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\nimport $!{tableInfo.savePackageName}.repository.$!{tableInfo.name}Repository;\nimport $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Service;\nimport org.springframework.transaction.annotation.Transactional;\n\nimport java.util.List;\nimport java.util.Optional;\n\n/**\n * $!{tableInfo.comment}服务实现类\n *\n * @author $!author\n * @date $!time.currTime(\"yyyy/MM/dd\")\n */\n@Slf4j\n@Service\npublic class $!{tableName} implements $!{tableInfo.name}Service {\n\n @Autowired\n private $!{tableInfo.name}Repository $!tool.firstLowerCase($!{tableInfo.name})Repository;\n\n @Override\n @Transactional(rollbackFor = Exception.class)\n public $!{tableInfo.name} create$!{tableInfo.name}($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})){\n return $!{tool.firstLowerCase($!{tableInfo.name})}Repository.save($!{tool.firstLowerCase($!{tableInfo.name})});\n }\n\n @Override\n @Transactional(rollbackFor = Exception.class)\n public void delete$!{tableInfo.name}($!pk.shortType $!pk.name){\n $!{tool.firstLowerCase($!{tableInfo.name})}Repository.deleteBy$!{tool.firstUpperCase($!{pk.name})}($!{pk.name});\n }\n\n @Override\n public List<$!{tableInfo.name}> list$!{tableInfo.name}s(){\n return $!{tool.firstLowerCase($!{tableInfo.name})}Repository.findAll();\n }\n\n @Override\n public $!{tableInfo.name} get$!{tableInfo.name}($!pk.shortType $!pk.name){\n Optional<$!{tableInfo.name}> optional = $!tool.append($!tool.firstLowerCase($!{tableInfo.name}),\"Repository.findBy\",\"$!tool.firstUpperCase($!pk.name)($!pk.name)\");\n if (!optional.isPresent()) {\n throw new RuntimeException();\n }\n return optional.get();\n }\n\n @Override\n @Transactional(rollbackFor = Exception.class)\n public $!{tableInfo.name} update$!{tableInfo.name}($!pk.shortType $!pk.name, $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})){\n return $!{tool.firstLowerCase($!{tableInfo.name})}Repository.save($!tool.firstLowerCase($!{tableInfo.name}));\n }\n\n}"
}, {
"name" : "controller.java",
"code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"Controller\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/controller\"))\n##拿到主键\n#if(!$tableInfo.pkColumn.isEmpty())\n #set($pk = $tableInfo.pkColumn.get(0))\n#end\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;\n\nimport $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};\nimport $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;\nimport io.swagger.annotations.Api;\nimport io.swagger.annotations.ApiImplicitParam;\nimport io.swagger.annotations.ApiImplicitParams;\nimport io.swagger.annotations.ApiOperation;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.*;\n\nimport java.util.List;\n\n/**\n * $!{tableInfo.comment}控制层\n *\n * @author $!author\n * @date $!time.currTime(\"yyyy/MM/dd\")\n */\n@Api(\"$!{tableInfo.comment}($!{tableInfo.name})服务\")\n@RestController\n@RequestMapping(\"icb/v1/$!tool.firstLowerCase($tableInfo.name)\")\npublic class $!{tableName} {\n\n @Autowired\n private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;\n\n @PostMapping(\"\")\n @ApiOperation(value = \"创建$!{tableInfo.comment}\", notes = \"创建$!{tableInfo.comment}\")\n @ApiImplicitParam(name = \"$!tool.firstLowerCase($!{tableInfo.name})\", value = \"$!{tableInfo.comment}\", dataType = \"$!{tableInfo.name}\", required = true, paramType = \"body\" )\n public $!{tableInfo.name} create$!{tableInfo.name}(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})){\n return $!{tool.firstLowerCase($tableInfo.name)}Service.create$!{tableInfo.name}($!{tool.firstLowerCase($!{tableInfo.name})});\n }\n\n @DeleteMapping(\"/{$!pk.name}\")\n @ApiOperation(value = \"删除$!{tableInfo.comment}\", notes = \"删除$!{tableInfo.comment}\")\n @ApiImplicitParams(value = {\n @ApiImplicitParam(name = \"$!pk.name\", value = \"$!{tableInfo.comment}$!pk.name\", dataType = \"$!pk.shortType\", required = true, paramType = \"path\")\n })\n public void delete$!{tableInfo.name}(@PathVariable $!pk.shortType $!pk.name){\n $!{tool.firstLowerCase($tableInfo.name)}Service.delete$!{tableInfo.name}($!{pk.name});\n }\n\n @GetMapping(\"\")\n @ApiOperation(value = \"获取$!{tableInfo.comment}列表\", notes = \"获取$!{tableInfo.comment}列表\")\n public List<$!{tableInfo.name}> list$!{tableInfo.name}s(){\n return $!{tool.firstLowerCase($tableInfo.name)}Service.list$!{tableInfo.name}s();\n }\n\n @GetMapping(\"/{$!pk.name}\")\n @ApiOperation(value = \"获取$!{tableInfo.comment}\", notes = \"根据$!{pk.name}获取$!{tableInfo.comment}\")\n @ApiImplicitParams(value = {\n @ApiImplicitParam(name = \"$!pk.name\", value = \"$!{tableInfo.comment}$!pk.name\", dataType = \"$!pk.shortType\", required = true, paramType = \"path\")\n })\n public $!{tableInfo.name} get$!{tableInfo.name}(@PathVariable $!pk.shortType $!pk.name){\n return $!{tool.firstLowerCase($tableInfo.name)}Service.get$!{tableInfo.name}($!pk.name);\n }\n\n @PutMapping(\"/{$!pk.name}\")\n @ApiOperation(value = \"更新$!{tableInfo.comment}\", notes = \"更新$!{tableInfo.comment}\")\n @ApiImplicitParams(value = {\n @ApiImplicitParam(name = \"$!pk.name\", value = \"$!{tableInfo.comment}$!pk.name\", dataType = \"$!pk.shortType\", required = true, paramType = \"path\"),\n @ApiImplicitParam(name = \"$!tool.firstLowerCase($!{tableInfo.name})\", value = \"$!{tableInfo.comment}\", dataType = \"$!{tableInfo.name}\", required = true, paramType = \"body\")\n })\n public $!{tableInfo.name} update$!{tableInfo.name}(@PathVariable $!pk.shortType $!pk.name, @RequestBody $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})){\n return $!{tool.firstLowerCase($tableInfo.name)}Service.update$!{tableInfo.name}($!pk.name, $!tool.firstLowerCase($!{tableInfo.name}));\n }\n\n}"
}, {
"name" : "error.java",
"code" : "##定义初始变量\n#set($tableName = $tool.append($tableInfo.name, \"Error\"))\n##设置回调\n$!callback.setFileName($tool.append($tableName, \".java\"))\n$!callback.setSavePath($tool.append($tableInfo.savePath, \"/error\"))\n\n#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}error;\n\n##使用全局变量实现默认包导入\nimport com.inspur.common.framework.utils.web.MessageSourceUtils;\nimport com.inspur.ipds.common.error.BaseError;\n##使用宏定义实现类注释信息\n/**\n * $!{tableInfo.comment}异常类\n *\n * @author $!author\n * @date $!time.currTime(\"yyyy/MM/dd\")\n */\npublic enum $!{tableInfo.name}Error implements BaseError{\n \n \n ;\n private String code;\n private String message;\n\n private $!{tableInfo.name}Error (String code, String message) {\n this.code = code;\n this.message = message;\n }\n\n\n @Override\n public String message(Object... args) {\n return MessageSourceUtils.getMessage(this.message, args);\n }\n\n @Override\n public String code() {\n return code;\n }\n\n\n}"
} ]
}
},
"columnConfig" : { },
"globalConfig" : { }
}

6.2 他人的模板

Service 模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("Service")

##保存文件(宏定义)
#save("/service", "Service.java")

##包路径(宏定义)
#setPackageSuffix("service")

import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}PO;

##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!{tableInfo.name}PO{
}

ServiceImpl 模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")

##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")

##包路径(宏定义)
#setPackageSuffix("service.impl")

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}PO;
import org.springframework.stereotype.Service;

##表注释(宏定义)
#tableComment("表服务实现类")
@Service
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}POimplements $!{tableInfo.name}Service {
}

Mapper 模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("Mapper")

##保存文件(宏定义)
#save("/dao", "Mapper.java")

##包路径(宏定义)
#setPackageSuffix("dao")

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}PO;
import org.apache.ibatis.annotations.Mapper;

##表注释(宏定义)
#tableComment("表数据库访问层")
@Mapper
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}PO{
}

Entiy(Persistant Object)模板

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
##导入宏定义
$!{define.vm}

##设置表后缀(宏定义)
#setTableSuffix("PO")

##保存文件(宏定义)
#save("/entity", "PO.java")

##包路径(宏定义)
#setPackageSuffix("entity")

##自动导入包(全局变量)
$!{autoImport.vm}
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import lombok.NoArgsConstructor;
import lombok.Data;

##表注释(宏定义)
#tableComment("表实体类")
@Data
@NoArgsConstructor
@TableName("$tool.hump2Underline($tableInfo.name)")
public class $!{tableName} implements Serializable {
##以第一个primary key字段没primary key
#foreach($column in $tableInfo.pkColumn)
#set($pkColumn = $!column.name)
#break
#end

##输出每个字段
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})
/**
* ${column.comment}
*/
#end
#if(${column.name} == ${pkColumn})
@TableId(value = "$tool.hump2Underline($column.name)", type = IdType.AUTO)
#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};

#end
}

7、EasyCode生成界面配置

EasyCode生成代码界面配置


EasyCode 自动生成代码
https://flepeng.github.io/021-Java-22-idea使用-01-plugin-EasyCode-自动生成代码/
作者
Lepeng
发布于
2021年3月17日
许可协议