之前无意中了解到了 idea 中的 Easy Code 插件,说是能快速生成 entity 、mapper、service、controller 等文件,避免很多简单重复性的创建工作,大大提高 MySQL 增删改查的开发效率。
正好今天要做对 MySQL 的增删改查,想着试试这个插件,没想到,特别好用,但也需要自己定制,所以就有了这篇文章,分享如何使用 idea Easy Code 插件配置 Mybatis Plus 模板来提高对 MySQL 的开发效率的。
一、idea 安装 Easy Code 插件

安装完成后,需要重启 idea 生效。
二、使用 idea 连接 MySQL 数据库
配置连接数据库步骤:
View –> Tool Windows –> Database

然后,新建 MySQL 连接,最后如下图所示:

连接成功后,这时候我们可以选择其中一个表,右键:EasyCode –> Generate Code,来快速生成 entity 、mapper、service、controller 等文件。如下图所示:

但是,这样会生成挺多文件,挺多内容的,乱七八糟。有的内容我并不想要,所以我们需要配置 Easy Code 自定义宏操作模板。
三、配置 Easy Code 生成模板
点击 File –> Settings –> Other Settings –> Easy Code –> Template Setting,如下图所示:

我们可以新建 Group,创建宏操作来自动生成 entity 、mapper、service、controller、mapper.xml 等文件。
3.1、entity
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
| ##导入宏定义 $!define
##保存文件(宏定义) #save("/entity", ".java")
##包路径(宏定义) #setPackageSuffix("entity")
##自动导入包(全局变量) $!autoImport import com.baomidou.mybatisplus.extension.activerecord.Model; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode;
import java.io.Serializable;
##表注释(宏定义) ##tableComment("表实体类")
@EqualsAndHashCode(callSuper = true) @Data @ApiModel(description = "") @SuppressWarnings("serial") public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> implements Serializable { private static final long serialVersionUID = $!tool.serial(); #foreach($column in $tableInfo.fullColumn) ##if(${column.comment})/** ##* ${column.comment} ##*/#end
@ApiModelProperty("$column.comment") private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
|
3.2、mapper
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
| ##定义初始变量 #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.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface $!{tableName} extends BaseMapper<$!{tableInfo.name}>{
}
|
3.3、service
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
| ##定义初始变量 #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 com.baomidou.mybatisplus.extension.service.IService; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
public interface $!{tableInfo.name}Service extends IService<$!{tableInfo.name}>{ }
|
3.4、serviceImpl
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
| ##导入宏定义 $!define
##设置表后缀(宏定义) #setTableSuffix("ServiceImpl")
##保存文件(宏定义) #save("/service/impl", "ServiceImpl.java")
##包路径(宏定义) #setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import org.springframework.stereotype.Service;
##表注释(宏定义) ##tableComment("表服务实现类")
@Service public class $!{tableInfo.name}ServiceImpl extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
|
3.5、controller
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
| ##定义初始变量 #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}.service.$!{tableInfo.name}Service; import io.swagger.annotations.Api; import lombok.AllArgsConstructor; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource;
@Api(tags = "$!{tableInfo.comment}($!{tableInfo.name})") @Validated @RestController @AllArgsConstructor @RequestMapping("$!tool.firstLowerCase($tableInfo.name)") public class $!{tableName} { @Resource private final $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
}
|
3.6、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
| ##引入mybatis支持 $!mybatisSupport
##设置保存名称与保存位置 $!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}.mapper.$!{tableInfo.name}Mapper">
<resultMap type="$!{tableInfo.savePackageName}.entity.$!{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>
</mapper>
|
四、添加类型映射
点击 File –> Settings –> Other Settings –> Easy Code –> Type Mapper,如下图所示:

在我们生成类文件之前,我们也可以在 idea 的 Database 中的 某个表 中,右键:EasyCode –> Config Table,来修改字段类型和字段备注等。


五、快速生成代码
点击 idea 的 Database,选择其中一个表,右键:EasyCode –> Generate Code,来快速生成 entity 、mapper、service、controller 等文件。如下图所示:


其中 Package 路径为 Application 类的根路径。点击 “OK”,实现代码的快速生成。
这个 Easy Code 插件,配合着自己定义的宏操作,用的确实太爽了,解放劳动力啊。生成完代码之后,我们只需要在其中写业务代码即可。