Easy Code 是IDEA的一个插件,封装了Mybatis 逆向工程的功能,同时提供了一些自定义的拓展。使用者可对插件的脚本做自定义拓展,比如定义生成的Entity以DO结尾,使用Lombok风格,标识@Data注解,抽象公共的Mapper接口,自定义Service,Controller公共方法 等等。我就是对脚本做了一些自定义,实现上述功能。
脚本主要在Template Setting里配置:
下面就贴上相应脚本的内容
##引入宏定义
$!define
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", "DO.java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
import lombok.Data;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
public class $!{tableInfo.name}DO 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*#
}
##定义初始变量
#set($entityName = $tool.append($tableInfo.name, "DO"))
#set($baseMapper='com.bob.common.entity.base.BaseMapper')
#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.$!{entityName};
import org.apache.ibatis.annotations.Param;
import java.util.List;
import $!{baseMapper};
/**
* $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends BaseMapper<$!pk.shortType, $!{entityName}> {
}
com.bob.common.entity.base.BaseMapper 是抽象的Mapper基类:
/**
* Mapper基类接口
*
* @author wb-jjb318191
* @create 2017-12-06 9:23
*/
public interface BaseMapper<K, T> {
/**
* 根据主键删除数据库的记录
*
* @param id
* @return
*/
int deleteById(K id);
/**
* 新写入数据库记录
*
* @param record
* @return
*/
int insert(T record);
/**
* 动态字段,写入数据库记录
*
* @param record
* @return
*/
int insertWithoutNull(T record);
/**
* 根据指定主键获取一条数据库记录
*
* @param id
* @return
*/
T selectById(K id);
/**
* 动态字段,根据主键来更新符合条件的数据库记录
*
* @param record
* @return
*/
int updateByIdWithoutNull(T record);
/**
* 根据主键来更新符合条件的数据库记录
*
* @param record
* @return
*/
int updateById(T record);
}
##定义初始变量
#set($entityName = $tool.append($tableInfo.name, "DO"))
#set($serviceName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($serviceName, ".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.$!{entityName};
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务接口
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{serviceName} {
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
$!{entityName} queryById($!pk.shortType $!pk.name);
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{entityName}) 实例对象
* @return 是否成功
*/
boolean create($!{entityName} $!tool.firstLowerCase($!{entityName}));
/**
* 修改数据
*
* @param $!tool.firstLowerCase($!{entityName}) 实例对象
* @return 是否成功
*/
boolean update($!{entityName} $!tool.firstLowerCase($!{entityName}));
/**
* 通过主键删除数据
*
* @param $!pk.name 主键
* @return 是否成功
*/
boolean deleteById($!pk.shortType $!pk.name);
}
##定义初始变量
#set($entityName = $tool.append($tableInfo.name, "DO"))
#set($mapperName = $tool.append($tableInfo.name, "Mapper"))
#set($serviceImplName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($serviceImplName, ".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.$!{entityName};
import $!{tableInfo.savePackageName}.mapper.$!{mapperName};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
*
* @author $!author
* @since $!time.currTime()
*/
@Service
public class $!{serviceImplName} implements $!{tableInfo.name}Service {
@Autowired
private $!{mapperName} $!tool.firstLowerCase($!{mapperName});
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
@Override
public $!{entityName} queryById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{mapperName})}.queryById($!pk.name);
}
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{entityName}) 实例对象
* @return 是否成功
*/
@Override
public boolean create($!{entityName} $!tool.firstLowerCase($!{entityName})) {
return this.$!{tool.firstLowerCase($!{mapperName})}.insertWithoutNull($!tool.firstLowerCase($!{entityName})) > 0;
}
/**
* 修改数据
*
* @param $!tool.firstLowerCase($!{entityName}) 实例对象
* @return 是否成功
*/
@Override
public boolean update($!{entityName} $!tool.firstLowerCase($!{entityName})) {
return this.$!{tool.firstLowerCase($!{mapperName})}.updateByIdWithoutNull($!tool.firstLowerCase($!{entityName})) > 0;
}
/**
* 通过主键删除数据
*
* @param $!pk.name 主键
* @return 是否成功
*/
@Override
public boolean deleteById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{mapperName})}.deleteById($!pk.name) > 0;
}
}
##定义初始变量
#set($entityName = $tool.append($tableInfo.name, "DO"))
#set($controllerName = $tool.append($tableInfo.name, "Controller"))
#set($serviceName = $tool.append($!tool.firstLowerCase($tableInfo.name), "Service"))
#set($httpResult = 'com.bob.common.entity.result.PojoResult')
#set($result = 'PojoResult')
##设置回调
$!callback.setFileName($tool.append($controllerName, ".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.$!{entityName};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import $!{httpResult}
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping
public class $!{controllerName} {
/**
* 服务对象
*/
@Autowired
private $!{tableInfo.name}Service $!{serviceName};
/**
* 新增
*
* @param $!tool.firstLowerCase($entityName)
* @return 是否成功
*/
@PostMapping
public $!{result}<Boolean> create(@RequestBody $!{entityName} $!tool.firstLowerCase($entityName)){
return new $!{result}(this.$!{serviceName}.create($!tool.firstLowerCase($entityName)));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("/{id}")
public $!{result}<$!{entityName}> getById(@PathVariable $!pk.shortType id) {
return new $!{result}(this.$!{serviceName}.queryById(id));
}
/**
* 更新
*
* @param $!tool.firstLowerCase($entityName)
* @return 是否成功
*/
@PutMapping
public $!{result}<Boolean> update(@RequestBody $!{entityName} $!tool.firstLowerCase($entityName)){
return new $!{result}(this.$!{serviceName}.update($!tool.firstLowerCase($entityName)));
}
}
##引入mybatis支持
$!mybatisSupport
#set($entityName = $tool.append($tableInfo.name, "DO"))
#set($mapperName = $tool.append($tableInfo.name, "Mapper"))
##设置保存名称与保存位置
$!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.$!{mapperName}">
<resultMap type="$!{tableInfo.savePackageName}.entity.$!{entityName}" id="BaseResultMap">
#foreach($column in $tableInfo.fullColumn)
<result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
</resultMap>
<!--查询单个-->
<select id="selectById" resultMap="BaseResultMap">
select #allSqlColumn()
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</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="insertWithoutNull" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.name}
<trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($column in $tableInfo.otherColumn)
<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
$!column.obj.name,
</if>
#end
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#foreach($column in $tableInfo.otherColumn)
<if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
#{$!column.name},
</if>
#end
</trim>
</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>
<!--通过主键修改数据-->
<update id="updateByIdWithoutNull">
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>