EasyCode是什么?
EasyCode是基于IntelliJ IDEA开发的代码生成插件,支持自定义任意模板(Java,html,js,xml)。只要是与数据库相关的代码都可以通过自定义模板来生成。支持数据库类型与java类型映射关系配置。支持同时生成生成多张表的代码。每张表有独立的配置信息。完全的个性化定义,规则由你设置。
创建一个SpringBoot项目,便于测试
默认一直next… 最后finish
基础POM
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
springBoot基础配置
############################################################
#
# 配置数据源相关 使用阿里巴巴的 druid 数据源
#
############################################################
spring.datasource.url=jdbc:mysql://localhost:3306/yinwq_util
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=20
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.stat-view-servlet.allow=true
############################################################
#
# mybatis 配置
#
############################################################
mybatis.type-aliases-package=com.yinwq.easycode.entity
mybatis.mapper-locations=classpath:mapper/*.xml
############################################################
#
# Server 服务端相关配置
#
############################################################
# 配置api端口号
server.port=8088
# 配置context-path, 一般来说这个配置在正式发布的时候不配置
#server.context-path=/IMooc
# 错误页,指定发生错误时,跳转的URL --> BasicErrorController
#server.error.path=/error
# session最大超时时间(分钟),默认为30分钟
server.session-timeout=60
# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,
# 只有特殊需求的情况下才配置, 具体根据各自的业务来设置
#server.address=192.168.1.2
############################################################
# Server - tomcat 相关常用配置
############################################################
# tomcat最大线程数, 默认为200
#server.tomcat.max-threads=250
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹
#(如:C:\Users\Shanhy\AppData\Local\Temp)
#server.tomcat.basedir=H:/springboot-tomcat-tmp
# 打开Tomcat的Access日志,并可以设置日志格式的方法:
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
# accesslog目录,默认在basedir/logs
#server.tomcat.accesslog.directory=
# 日志文件目录
#logging.path=H:/springboot-tomcat-tmp
# 日志文件名称,默认为spring.log
#logging.file=myapp.log
下载Easy Code 插件
我这里已经安装过了,未安装的自行安装即可
配置数据源
右侧边栏 database
注意测试连接按钮是否是灰色,如果是灰色,则需要下载mysql驱动,驱动可在弹窗的左下角下载,我已经下载了,截图上没有那下载的图标
连接好之后的效果,数据源已配置成功
准备数据库表
自定义生成模板
配置作者 jdbcType类型映射 模板设置 第一次安装EasyCode的时候默认的模板(服务于MyBatis)可以生成下面类型的代码
entity.javadao.javaservice.javaserviceImpl.javacontroller.javamapper.xmldebug.json
可切换分组,可以自定义新分组 根据模板生成代码 选择模板 点击OK之后,就可以看到生成了这些代码
全局设置
可以自己创建一些全局设置,引用到其他模板中
自定义模板
新建分组 新建模板文件 entity.java
##初始化
$!init
##引入宏定义
$!define
##定义初始变量
#set($entity = $tool.append($tableInfo.name, "DO"))
##设置回调
$!callback.setFileName($tool.append($entity, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/entity"))
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Tolerate;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
@Builder
@ToString
public class $!{entity} 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
@Tolerate
public $!{entity}() {
}
}
mapper.java
##初始化
$!init
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
#set($entity = $tool.append($tableInfo.name, "DO"))
##设置回调
$!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.$!{entity};
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @InterfaceName $!{tableName}
* @Description $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
* @author $!author
* @date $!time.currTime()
* @Version 1.0
**/
@Mapper
public interface $!{tableName} {
/**
* @Description 添加数据
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 影响行数
*/
int insert($!{entity} $!tool.firstLowerCase($!{entity}));
/**
* @Description 删除数据
* @author $!author
* @date $!time.currTime()
* @param $!pk.name 主键
* @return 影响行数
*/
int deleteById($!pk.shortType $!pk.name);
/**
* @Description 查询单条数据
* @author $!author
* @date $!time.currTime()
* @param $!pk.name 主键
* @return 实例对象
*/
$!{entity} queryById($!pk.shortType $!pk.name);
/**
* @Description 查询全部数据
* @author $!author
* @date $!time.currTime()
* 分页使用MyBatis的插件实现
* @return 对象列表
*/
List<$!{entity}> queryAll();
/**
* @Description 实体作为筛选条件查询数据
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 对象列表
*/
List<$!{entity}> queryAll($!{entity} $!tool.firstLowerCase($!{entity}));
/**
* @Description 修改数据
* @author $!author
* @date $!time.currTime()
* @param 根据$!tool.firstLowerCase($!{entity})的主键修改数据
* @return 影响行数
*/
int update($!{entity} $!tool.firstLowerCase($!{entity}));
}
service.java
##初始化
$!init
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
#set($entity = $tool.append($tableInfo.name, "DO"))
##设置回调
$!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.$!{entity};
import java.util.List;
/**
* @InterfaceName $!{tableName}
* @Description $!{tableInfo.comment}($!{tableInfo.name})表服务接口
* @author $!author
* @date $!time.currTime()
* @Version 1.0
**/
public interface $!{tableName} {
/**
* @Description 添加数据
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 是否成功
*/
boolean insert($!{entity} $!tool.firstLowerCase($!{entity}));
/**
* @Description 删除数据
* @author $!author
* @date $!time.currTime()
* @param $!pk.name 主键
* @return 是否成功
*/
boolean deleteById($!pk.shortType $!pk.name);
/**
* @Description 查询单条数据
* @author $!author
* @date $!time.currTime()
* @param $!pk.name 主键
* @return 实例对象
*/
$!{entity} queryById($!pk.shortType $!pk.name);
/**
* @Description 查询全部数据
* @author $!author
* @date $!time.currTime()
* 分页使用MyBatis的插件实现
* @return 对象列表
*/
List<$!{entity}> queryAll();
/**
* @Description 实体作为筛选条件查询数据
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 对象列表
*/
List<$!{entity}> queryAll($!{entity} $!tool.firstLowerCase($!{entity}));
/**
* @Description 修改数据,哪个属性不为空就修改哪个属性
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 是否成功
*/
boolean update($!{entity} $!tool.firstLowerCase($!{entity}));
}
serviceimpl.java
##初始化
$!init
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
#set($entity = $tool.append($tableInfo.name, "DO"))
##设置回调
$!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.$!{entity};
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 javax.annotation.Resource;
import java.util.List;
/**
* @ClassName $!{tableName}
* @Description $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
* @author $!author
* @date $!time.currTime()
* @Version 1.0
**/
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
@Autowired
protected $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;
/**
* @Description 添加数据
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 是否成功
*/
@Override
public boolean insert($!{entity} $!tool.firstLowerCase($!{entity})) {
if($!{tool.firstLowerCase($!{tableInfo.name})}Mapper.insert($!tool.firstLowerCase($!{entity})) == 1){
return true;
}
return false;
}
/**
* @Description 删除$!{entity}
* @author $!author
* @date $!time.currTime()
* @param $!pk.name 主键
* @return 是否成功
*/
@Override
public boolean deleteById($!pk.shortType $!pk.name) {
if($!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteById($!pk.name) == 1){
return true;
}
return false;
}
/**
* @Description 查询单条数据
* @author $!author
* @date $!time.currTime()
* @param $!pk.name 主键
* @return 实例对象
*/
@Override
public $!{entity} queryById($!pk.shortType $!pk.name) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.queryById($!pk.name);
}
/**
* @Description 查询全部数据
* @author $!author
* @date $!time.currTime()
* 分页使用MyBatis的插件实现
* @return 对象列表
*/
@Override
public List<$!{entity}> queryAll() {
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.queryAll();
}
/**
* @Description 实体作为筛选条件查询数据
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 对象列表
*/
@Override
public List<$!{entity}> queryAll($!{entity} $!tool.firstLowerCase($!{entity})) {
return $!{tool.firstLowerCase($!{tableInfo.name})}Mapper.queryAll($!tool.firstLowerCase($!{entity}));
}
/**
* @Description 修改数据,哪个属性不为空就修改哪个属性
* @author $!author
* @date $!time.currTime()
* @param $!tool.firstLowerCase($!{entity}) 实例对象
* @return 是否成功
*/
@Override
public boolean update($!{entity} $!tool.firstLowerCase($!{entity})) {
if($!{tool.firstLowerCase($!{tableInfo.name})}Mapper.update($!tool.firstLowerCase($!{entity})) == 1){
return true;
}
return false;
}
}
mapper.xml
##初始化
$!init
##定义初始变量
#set($entity = $tool.append($tableInfo.name, "DO"))
##引入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
#foreach($column in $tableInfo.fullColumn)
#end
#foreach($column in $tableInfo.otherColumn)
$!column.obj.name,
#end
#foreach($column in $tableInfo.otherColumn)
#{$!column.name},
#end
#foreach($column in $tableInfo.otherColumn)
$!column.obj.name = #{$!column.name},
#end
insert into $!{tableInfo.obj.name}
delete from $!{tableInfo.obj.name}
$!pk.obj.name = #{$!pk.name}
select
from $!tableInfo.obj.name
$!pk.obj.name = #{$!pk.name}
select
from $!tableInfo.obj.name
select
from $!tableInfo.obj.name
update $!{tableInfo.obj.name}
$!pk.obj.name = #{$!pk.name}
生成好了,小试一把,可以好好搬砖了!
package com.yinwq.easycode;
import com.yinwq.easycode.entity.StudentDO;
import com.yinwq.easycode.service.StudentService;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.Date;
@SpringBootTest
@MapperScan(basePackages = "com.yinwq.easycode.mapper")
class EasycodeApplicationTests {
@Resource
private StudentService studentService;
@Test
void insert() {
StudentDO studentDO = StudentDO.builder().name("呵呵哒").age(18).sex("Man").birthday(new Date()).build();
studentService.insert(studentDO);
}
@Test
void queryAll() {
System.err.println(studentService.queryAll());
}
}