之前习惯了若依、jeecgboot的生成代码功能,最近让做的项目没有生成代码功能,于是乎找到了idea的一款插件 easy-code 可以生成对应的controller、entity、mapper、mapper.xml 但是默认的模板有几个问题:
1.没有swagger 注释
2.由于用的oracle数据库,自增主键没有生成逻辑,mapper.xml中的方法没有加自增主键(如果用oracle序列+设置trigger)会导致插入返回的主键ID是一串数字
3.Controller+分页方法模板
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 com.kba.common.dto.ResultModel;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import javax.annotation.Resource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@Api(tags = "$!{tableInfo.comment}")
@RestController
@RequestMapping("$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} extends BaseController {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 查询所有数据
*
* @return 实例对象集合
*/
@ApiOperation("查询所有数据")
@GetMapping
public ResultModel queryAll() {
return ResultModel.success(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryAll());
}
/**
* 查询所有数据带分页
*
* @return 实例对象集合
*/
@RequestMapping(value = "/queryListPage", method = RequestMethod.GET)
@ApiOperation("查询所有数据带分页") @ApiImplicitParams({
@ApiImplicitParam(name = "pageSize", required = true, value = "每页条数",dataType = "int",paramType = "query"),
@ApiImplicitParam(name = "pageNo", required = true, value = "当前页",dataType = "int",paramType = "query")})
public ResultModel queryListPage($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
try{
startPage();
List<$!{tableInfo.name}> list = $!{tool.firstLowerCase($tableInfo.name)}Service.queryAllByLimit($!{tool.firstLowerCase($tableInfo.name)});
return ResultModel.success(getDataTable(list));
} catch (Exception e) {
return ResultModel.failure(e.getMessage());
}
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@ApiOperation("通过主键查询单条数据")
@GetMapping("{id}")
public ResultModel<$!{tableInfo.name}> queryById(@PathVariable("id") $!pk.shortType id) {
return ResultModel.success(this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id));
}
/**
* 新增数据
*
* @param $!{tool.firstLowerCase($tableInfo.name)} 实体
* @return 新增结果
*/
@ApiOperation("新增数据")
@PostMapping
public ResultModel<$!{tableInfo.name}> add(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
return ResultModel.success(this.$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!{tool.firstLowerCase($tableInfo.name)}));
}
/**
* 编辑数据
*
* @param $!{tool.firstLowerCase($tableInfo.name)} 实体
* @return 编辑结果
*/
@ApiOperation("编辑数据")
@PutMapping
public ResultModel<$!{tableInfo.name}> edit(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
return ResultModel.success(this.$!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)}));
}
/**
* 删除数据
*
* @param id 主键
* @return 删除是否成功
*/
@ApiOperation("删除数据")
@DeleteMapping
public ResultModel<Boolean> deleteById($!pk.shortType id) {
return ResultModel.success(this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id));
}
}
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 java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表服务接口
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} {
/**
* 查询所有数据
*
* @return 实例对象集合
*/
List<$!{tableInfo.name}> queryAll();
/**
* 查询所有数据带条件
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象集合
*/
List<$!{tableInfo.name}> queryAllByLimit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
$!{tableInfo.name} queryById($!pk.shortType $!pk.name);
/**
* 新增数据
*
* @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);
}
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}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;
import java.util.List;
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}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;
/**
* 查询所有数据
*
* @return 实例对象集合
*/
@Override
public List<$!{tableInfo.name}> queryAll() {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.queryAll();
}
/**
* 查询所有数据带条件
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象集合
*/
@Override
public List<$!{tableInfo.name}> queryAllByLimit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.queryAllByLimit($!tool.firstLowerCase($!{tableInfo.name}));
}
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
@Override
public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) {
return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.queryById($!pk.name);
}
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象
*/
@Override
public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.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})}Mapper.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})}Mapper.deleteById($!pk.name) > 0;
}
}
mapper.java.vm
##定义初始变量
#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 org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
*
* @author $!author
* @since $!time.currTime()
*/
@Mapper
public interface $!{tableName} {
/**
* 查询所有数据
*
* @return 实例对象集合
*/
List<$!{tableInfo.name}> queryAll();
/**
* 查询所有数据带条件
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象集合
*/
List<$!{tableInfo.name}> queryAllByLimit($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
$!{tableInfo.name} queryById($!pk.shortType $!pk.name);
/**
* 统计总行数
*
* @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);
}
mapper.xml.vm
##引入mybatis支持
$!{mybatisSupport.vm}
##设置保存名称与保存位置
$!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>
<!--查询所有-->
<select id="queryAll" resultMap="$!{tableInfo.name}Map">
select
#allSqlColumn()
from $!tableInfo.obj.name
</select>
<!--查询单个-->
<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>
</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">
<selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="$!pk.name">
SELECT SEQ_$!{tableInfo.obj.name}.nextval AS $!pk.name FROM DUAL
</selectKey>
INSERT INTO $!{tableInfo.obj.name}
<trim prefix="(" suffix=")" suffixOverrides=",">
$!pk.obj.name,
#foreach($column in $tableInfo.otherColumn)
<if test="$!column.name != null">$!column.obj.name,</if>
#end
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#{$!pk.name},
#foreach($column in $tableInfo.otherColumn)
<if test="$!column.name != null">#{$!column.name},</if>
#end
</trim>
</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>
entity.java.vm
##引入宏定义
$!{define.vm}
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
##自动导入包(全局变量)
$!autoImport
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
##表注释(宏定义)
#tableComment("表实体类")
@ApiModel(description = "${tableInfo.comment}")
@Data
@Accessors(chain = true)
@SuppressWarnings("serial")
public class $!{tableInfo.name} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
@ApiModelProperty(name = "${column.name}", value = "${column.comment}")
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
package com.kba.supervise.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kba.common.dto.Page;
import com.kba.supervise.util.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.List;
/**
* web层通用数据处理
*
* @author xikang
*/
public class BaseController
{
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 设置请求分页数据
*/
protected void startPage()
{
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
Integer pageNum = Integer.parseInt(attributes.getRequest().getParameter("pageNo"));
Integer pageSize = Integer.parseInt(attributes.getRequest().getParameter("pageSize"));
if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
{
PageHelper.startPage(pageNum, pageSize).setReasonable(true);
}
}
/**
* 响应请求分页数据
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Page<T> getDataTable(List<?> list)
{
Page rspData = new Page();
rspData.setList(list);
rspData.setTotalNum((int)new PageInfo(list).getTotal());
return rspData;
}
}