EasyCode, a must-have development artifact for IDEA
Table of contents
1 Introduction
2. Install (EasyCode)
3. Create a database
4. Configure the connection database in IDEA
5. Start generating code
6、pom.xml
7、Application.yml
8. Start the project
1 Introduction
Easycode is a plug-in of idea, which can directly generate entity, controller, service, dao, mapper for the data table, without any coding, simple and powerful.
2. Install (EasyCode)
Open Plugins–>Marketplace in Sittings of idea, search for EasyCode, click Install and restart
What I say here is that it is already installed.
It is recommended that you install a plugin called Lombok.
Lombok can automatically generate constructors, getter/setter, equals, hashcode, and toString methods for attributes at compile time through annotations. The magic that appears is that there are no getter and setter methods in the source code, but there are getter and setter methods in the compiled bytecode file.
3. Create a database
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL,
`username` varchar(20) DEFAULT NULL,
`sex` varchar(6) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`address` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SET FOREIGN_KEY_CHECKS = 1;
4. Configure the connection database in IDEA
Before this, create a new Springboot project, which should be relatively simple.
After building the SpringBoot project, as shown in the figure below, find the Database
Take oracle as an example:
Configure database connection information:
5. The code generator generates code
Right-click the table that needs to generate code, select EasyCode–>Generate Code
Make the following selections in the pop-up box, remember to select your corresponding Package
The corresponding class has been generated in the right directory
The above is how to install easy code and how to automatically generate code and mapper in idea. Let's introduce the detailed configuration of easy code
5. Introduction to the configuration of esay code
Open Other Settings–>Easy Code
6, Type Mapper arrangement
7. Template Setting configuration
** Default**
Template Setting里面 生成的类和mapper的配置脚本,根据本人的开发习惯,整理出一套集成lombok和swagger的脚本,仅供参考
7.1、entity.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
##使用宏定义实现类注释信息
#tableComment("实体类")
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
#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
#getSetMethod($column)
#end
}
7.2、mapper.java.vm
##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Dao"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/dao"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}dao;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Pageable;
import java.util.List;
/**
* $!{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 pageable 分页对象
* @return 对象列表
*/
List<$!{tableInfo.name}> queryAllByLimit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}), @Param("pageable") Pageable pageable);
/**
* 统计总行数
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 查询条件
* @return 总行数
*/
long count($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 影响行数
*/
int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<$!{tableInfo.name}> 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<$!{tableInfo.name}> entities);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<$!{tableInfo.name}> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
*/
int insertOrUpdateBatch(@Param("entities") List<$!{tableInfo.name}> entities);
/**
* 修改数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 影响行数
*/
int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 通过主键删除数据
*
* @param $!pk.name 主键
* @return 影响行数
*/
int deleteById($!pk.shortType $!pk.name);
}
7.3、service.java.vm
##定义初始变量
#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);
}
7.4、serviceImpl.java.vm
##定义初始变量
#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 {
@Resource
private $!{tableInfo.name}Dao $!tool.firstLowerCase($!{tableInfo.name})Dao;
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
@Override
public $!{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 查询结果
*/
@Override
public 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 实例对象
*/
@Override
public $!{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 实例对象
*/
@Override
public $!{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 是否成功
*/
@Override
public boolean deleteById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.deleteById($!pk.name) > 0;
}
}
7.5、controller.java.vm
##定义初始变量
#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} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 分页查询
*
* @param $!{tool.firstLowerCase($tableInfo.name)} 筛选条件
* @param pageRequest 分页对象
* @return 查询结果
*/
@GetMapping
public 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 新增结果
*/
@PostMapping
public 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 编辑结果
*/
@PutMapping
public 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 删除是否成功
*/
@DeleteMapping
public ResponseEntity<Boolean> deleteById($!pk.shortType id) {
return ResponseEntity.ok(this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id));
}
}
7.6、mapper.xml
##引入mybatis支持
$!{mybatisSupport.vm}
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.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}.dao.$!{tableInfo.name}Dao">
<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>
<!--查询单个-->
<select id="queryById" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
from $!tableInfo.obj.name
<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>
limit #{pageable.offset}, #{pageable.pageSize}
</select>
<!--统计总行数-->
<select id="count" resultType="java.lang.Long">
select count(1)
from $!tableInfo.obj.name
<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>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values (#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
</insert>
<insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
<foreach collection="entities" item="entity" separator=",">
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
</insert>
<insert id="insertOrUpdateBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
<foreach collection="entities" item="entity" separator=",">
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
on duplicate key update
#foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name)#if($velocityHasNext),
#end#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>
7.7、debug.json.vm
// 禁止将生成结果写入到文件
$!callback.setWriteFile(false)
//调试表原始对象
$!tool.debug($tableInfo.obj)
//调试列原始对象
$!tool.debug($tableInfo.fullColumn.get(0).obj)
//调试列原始列类型
$!tool.debug($tableInfo.fullColumn.get(0).obj.dataType)
//获取原始列类型中的字段
sqlType = $!tool.getField($tableInfo.fullColumn.get(0).obj.dataType, "typeName")
//执行原始列类型中的方法
sqlTypeLen = $!tableInfo.fullColumn.get(0).obj.dataType.getLength()
MybatisPlus-Mixed
controller.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Controller")
##保存文件(宏定义)
#save("/controller", "Controller.java")
##包路径(宏定义)
#setPackageSuffix("controller")
##定义服务名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
##表注释(宏定义)
#tableComment("表控制层")
@RestController
@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)")
public class $!{tableName} extends ApiController {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!{serviceName};
/**
* 分页查询所有数据
*
* @param page 分页对象
* @param $!entityName 查询实体
* @return 所有数据
*/
@GetMapping
public R selectAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.page(page, new QueryWrapper<>($!entityName)));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("{id}")
public R selectOne(@PathVariable Serializable id) {
return success(this.$!{serviceName}.getById(id));
}
/**
* 新增数据
*
* @param $!entityName 实体对象
* @return 新增结果
*/
@PostMapping
public R insert(@RequestBody $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.save($!entityName));
}
/**
* 修改数据
*
* @param $!entityName 实体对象
* @return 修改结果
*/
@PutMapping
public R update(@RequestBody $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.updateById($!entityName));
}
/**
* 删除数据
*
* @param idList 主键结合
* @return 删除结果
*/
@DeleteMapping
public R delete(@RequestParam("idList") List<Long> idList) {
return success(this.$!{serviceName}.removeByIds(idList));
}
}
dao.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Dao")
##保存文件(宏定义)
#save("/dao", "Dao.java")
##包路径(宏定义)
#setPackageSuffix("dao")
import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表注释(宏定义)
#tableComment("表数据库访问层")
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<$!{tableInfo.name}> 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<$!{tableInfo.name}> entities);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<$!{tableInfo.name}> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
*/
int insertOrUpdateBatch(@Param("entities") List<$!{tableInfo.name}> entities);
}
entity.java.vm
##导入宏定义
$!{define.vm}
##保存文件(宏定义)
#save("/entity", ".java")
##包路径(宏定义)
#setPackageSuffix("entity")
##自动导入包(全局变量)
$!autoImport
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
##表注释(宏定义)
#tableComment("表实体类")
@SuppressWarnings("serial")
public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> {
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})//${column.comment}#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#foreach($column in $tableInfo.fullColumn)
#getSetMethod($column)
#end
#foreach($column in $tableInfo.pkColumn)
/**
* 获取主键值
*
* @return 主键值
*/
@Override
protected Serializable pkVal() {
return this.$!column.name;
}
#break
#end
}
mapper.xml.vm
##引入mybatis支持
$!{mybatisSupport.vm}
##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.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}.dao.$!{tableInfo.name}Dao">
<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>
<!-- 批量插入 -->
<insert id="insertBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
<foreach collection="entities" item="entity" separator=",">
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
</insert>
<!-- 批量插入或按主键更新 -->
<insert id="insertOrUpdateBatch" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.otherColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values
<foreach collection="entities" item="entity" separator=",">
(#foreach($column in $tableInfo.otherColumn)#{entity.$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
on duplicate key update
#foreach($column in $tableInfo.otherColumn)$!column.obj.name = values($!column.obj.name) #if($velocityHasNext), #end#end
</insert>
</mapper>
service.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Service")
##保存文件(宏定义)
#save("/service", "Service.java")
##包路径(宏定义)
#setPackageSuffix("service")
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
}
serviceImpl.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")
##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")
##包路径(宏定义)
#setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
##表注释(宏定义)
#tableComment("表服务实现类")
@Service("$!tool.firstLowerCase($tableInfo.name)Service")
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
MybatisPlus
controller.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Controller")
##保存文件(宏定义)
#save("/controller", "Controller.java")
##包路径(宏定义)
#setPackageSuffix("controller")
##定义服务名
#set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service"))
##定义实体对象名
#set($entityName = $!tool.firstLowerCase($!tableInfo.name))
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
##表注释(宏定义)
#tableComment("表控制层")
@RestController
@RequestMapping("$!tool.firstLowerCase($!tableInfo.name)")
public class $!{tableName} extends ApiController {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!{serviceName};
/**
* 分页查询所有数据
*
* @param page 分页对象
* @param $!entityName 查询实体
* @return 所有数据
*/
@GetMapping
public R selectAll(Page<$!tableInfo.name> page, $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.page(page, new QueryWrapper<>($!entityName)));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("{id}")
public R selectOne(@PathVariable Serializable id) {
return success(this.$!{serviceName}.getById(id));
}
/**
* 新增数据
*
* @param $!entityName 实体对象
* @return 新增结果
*/
@PostMapping
public R insert(@RequestBody $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.save($!entityName));
}
/**
* 修改数据
*
* @param $!entityName 实体对象
* @return 修改结果
*/
@PutMapping
public R update(@RequestBody $!tableInfo.name $!entityName) {
return success(this.$!{serviceName}.updateById($!entityName));
}
/**
* 删除数据
*
* @param idList 主键结合
* @return 删除结果
*/
@DeleteMapping
public R delete(@RequestParam("idList") List<Long> idList) {
return success(this.$!{serviceName}.removeByIds(idList));
}
}
dao.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Dao")
##保存文件(宏定义)
#save("/dao", "Dao.java")
##包路径(宏定义)
#setPackageSuffix("dao")
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表注释(宏定义)
#tableComment("表数据库访问层")
public interface $!{tableName} extends BaseMapper<$!tableInfo.name> {
}
entity.java.vm
##导入宏定义
$!{define.vm}
##保存文件(宏定义)
#save("/entity", ".java")
##包路径(宏定义)
#setPackageSuffix("entity")
##自动导入包(全局变量)
$!{autoImport.vm}
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
##表注释(宏定义)
#tableComment("表实体类")
@SuppressWarnings("serial")
public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> {
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})//${column.comment}#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
#foreach($column in $tableInfo.fullColumn)
#getSetMethod($column)
#end
#foreach($column in $tableInfo.pkColumn)
/**
* 获取主键值
*
* @return 主键值
*/
@Override
protected Serializable pkVal() {
return this.$!column.name;
}
#break
#end
}
service.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("Service")
##保存文件(宏定义)
#save("/service", "Service.java")
##包路径(宏定义)
#setPackageSuffix("service")
import com.baomidou.mybatisplus.extension.service.IService;
import $!{tableInfo.savePackageName}.entity.$!tableInfo.name;
##表注释(宏定义)
#tableComment("表服务接口")
public interface $!{tableName} extends IService<$!tableInfo.name> {
}
serviceImpl.java.vm
##导入宏定义
$!{define.vm}
##设置表后缀(宏定义)
#setTableSuffix("ServiceImpl")
##保存文件(宏定义)
#save("/service/impl", "ServiceImpl.java")
##包路径(宏定义)
#setPackageSuffix("service.impl")
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao;
import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
##表注释(宏定义)
#tableComment("表服务实现类")
@Service("$!tool.firstLowerCase($tableInfo.name)Service")
public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service {
}
spring-data-mongodb
controller.java.vm
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!{define.vm}
#setPackageSuffix("controller")
#setTableSuffix("Controller")
#save("/controller", "Controller.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##定义服务名
#set($serviceSortType = $!tool.append($!tableInfo.name, "Service"))
#set($serviceType = $!tool.append($!tableInfo.savePackageName, ".service.", $serviceSortType))
#set($serviceVarName = $!tool.firstLowerCase($serviceSortType))
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import $serviceType;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* $!{tableInfo.comment}控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$!tool.firstLowerCase($!tableInfo.name)")
@AllArgsConstructor
public class $!{tableName} {
private $serviceSortType $serviceVarName;
/**
* 获取$!{tableInfo.comment}列表(分页)
*/
@GetMapping("/list")
public Page<$entityShortType> list(Pageable page) {
return null;
}
/**
* 获取$!{tableInfo.comment}
*/
@GetMapping("/get")
public $entityShortType get($!pk.shortType id) {
return ${serviceVarName}.findById(id);
}
/**
* 添加$!{tableInfo.comment}
*/
@PostMapping("/add")
public void add(@RequestBody $entityShortType $entityVarName) {
${serviceVarName}.save($entityVarName);
}
/**
* 修改$!{tableInfo.comment}
*/
@PostMapping("/update")
public void update(@RequestBody $entityShortType $entityVarName) {
${serviceVarName}.save($entityVarName);
}
/**
* 删除$!{tableInfo.comment}
*/
@PostMapping("/delete")
public void delete($!pk.shortType id) {
${serviceVarName}.deleteById(id);
}
}
entity.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!{autoImport.vm}
import java.io.Serializable;
##使用宏定义实现类注释信息
#tableComment("实体类")
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
#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
#getSetMethod($column)
#end
}
repository.java.vm
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!{define.vm}
#setPackageSuffix("repository")
#setTableSuffix("Repository")
#save("/repository", "Repository.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##实体类名、主键类名
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* $!{tableInfo.comment}持久层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends MongoRepository<$entityShortType, $pkShortType> {
}
service.java.vm
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!{define.vm}
#setPackageSuffix("service")
#setTableSuffix("Service")
#save("/service", "Service.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##实体类名、主键类名
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Collection;
import java.util.List;
/**
* $!{tableInfo.comment}业务层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} {
void save($entityShortType $entityVarName);
void deleteById($pkShortType id);
$entityShortType findById($pkShortType id);
List<$entityShortType> findById(Collection<$pkShortType> ids);
Page<$entityShortType> list(Pageable page);
}
serviceImpl.java.vm
##导入宏定义、设置包名、类名、文件名
$!{define.vm}
#setPackageSuffix("service.impl")
#setTableSuffix("ServiceImpl")
#save("/service/impl", "ServiceImpl.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##业务层类名、持久层类名、实体名
#set($serviceSortType = $!tool.append($!tableInfo.name, "Service"))
#set($serviceType = $!tool.append($!tableInfo.savePackageName, ".service.", $serviceSortType))
#set($repositorySortType = $!tool.append($!tableInfo.name, "Repository"))
#set($repositoryType = $!tool.append($!tableInfo.savePackageName, ".repository.", $repositorySortType))
#set($repositoryVarName = $!tool.firstLowerCase($!repositorySortType))
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import $serviceType;
import $repositoryType;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* $!{tableInfo.comment}业务层
*
* @author $!author
* @since $!time.currTime()
*/
@Service
public class $!{tableName} implements $!serviceSortType {
@Resource
private $repositorySortType $repositoryVarName;
@Override
public void save($entityShortType $entityVarName) {
$!{repositoryVarName}.save($entityVarName);
}
@Override
public void deleteById($pkShortType id) {
$!{repositoryVarName}.delete(id);
}
@Override
public $entityShortType findById($pkShortType id) {
return $!{repositoryVarName}.findOne(id);
}
@Override
public List<$entityShortType> findById(Collection<$pkShortType> ids) {
Iterable<$entityShortType> iterable = $!{repositoryVarName}.findAll(ids);
return StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
}
@Override
public Page<$entityShortType> list(Pageable page) {
return $!{repositoryVarName}.findAll(page);
}
}
8、Global Config配置
去掉表前缀设置
去掉表的t_前缀
!tableInfo.setName(tool.getClassName($tableInfo.obj.name.replaceFirst("T_","")))
这里只是简单的一些模板配置,如果你对EasyCode很感兴趣的话,可以尝试在官网多进行了解:
地址:
github:https://github.com/makejavas/EasyCode
扩展文章:
IDEA插件Easy Code自定义模板 - 小小爬虫 - 博客园 (cnblogs.com)
(49条消息) easycode自定义模板_程序员s的博客-CSDN博客_easycode自定义模板