1. 拷贝默认模板并重新命名
2. 配置自己的模板
实体类模板
##引入宏定义
$!define
##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")
##使用宏定义设置包后缀
#setPackageSuffix("entity")
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
@ToString
public class $!{tableInfo.name} implements Serializable {
private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
#if(${column.comment})/**
* ${column.comment}
*/
@ApiModelProperty(value = "${column.comment}", dataType = "$!{tool.getClsNameByFullName($column.type)}")#end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end
}
dao层模板
##定义初始变量
#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.Mapper;
import java.util.List;
import java.util.Map;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
*
* @author $!author
* @since $!time.currTime()
*/
@Mapper
public interface $!{tableName} {
/**
* 通过ID查询单条数据
*
* @param $!pk.name 主键
* @return 实例对象
*/
$!{tableInfo.name} queryById($!pk.shortType $!pk.name);
/**
* 查询所有数据天条数
*
* @param params 查询参数
* @return 返回数据条数
*/
long count(Map<String, Object> params);
/**
* 查询所有
*
* @param params 查询参数(包括分页<start ,length>)
* @return 返回对象集合
*/
List<$!{tableInfo.name}> queryAllByLimit(Map<String, Object> params);
/**
* 通过实体作为筛选条件查询
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 对象列表
*/
List<$!{tableInfo.name}> queryAll($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 影响行数
*/
int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 修改数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 影响行数
*/
int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 通过主键删除数据
*
* @param $!pk.name 主键
* @return 影响行数
*/
int deleteById($!pk.shortType $!pk.name);
}
service层模板
##定义初始变量
#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;
import com.cloud.model.common.Page;
import java.util.Map;
/**
* $!{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 params 查询参数(包括分页<start ,length>)
* @return 返回对象集合
*/
Page<$!{tableInfo.name}> queryAllByLimit(Map<String, Object> params);
/**
* 新增数据
*
* @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);
}
service实现模板
##定义初始变量
#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 com.cloud.common.utils.PageUtil;
import com.cloud.common.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import com.cloud.model.common.Page;
import java.util.Map;
/**
* $!{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 params 查询参数(包括分页<start ,length>)
* @return 返回对象集合
*/
@Override
public Page<$!{tableInfo.name}> queryAllByLimit(Map<String, Object> params) {
long total = this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.count(params);
List<$!{tableInfo.name}> list = Collections.emptyList();
if (total > 0) {
PageUtil.pageUtil(params);
list=this.$!{tool.firstLowerCase($!{tableInfo.name})}Dao.queryAllByLimit(params);
}
return new Page<>(total,list);
}
/**
* 新增数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 实例对象
*/
@Override
public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
if (StringUtils.isEmpty($!{tool.firstLowerCase($!{tableInfo.name})}.getId())){
$!{tool.firstLowerCase($!{tableInfo.name})}.setId(UUIDUtils.getUUID());
}
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;
}
}
controller模板
##定义初始变量
#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.cloud.common.utils.ModelUtil;
import com.cloud.common.utils.ResultUtil;
import com.cloud.model.common.Page;
import com.cloud.model.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import java.util.Map;
/**
* $!{tableInfo.comment}($!{tableInfo.name})表控制层
*
* @author $!author
* @since $!time.currTime()
*/
@Api(tags = "$!{tableInfo.comment}控制器")
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
* 通过主键查询单条数据
*
* @param $!pk.name 主键
* @return 单条数据
*/
@ApiOperation(value = "根据id查询单条数据", notes = "根据id查询单条数据", response = Result.class)
@ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "$!pk.shortType")
@GetMapping("/getById")
public Result getById(@RequestParam $!pk.shortType $!pk.name) {
$!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}=this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
return ResultUtil.success($!{tool.firstLowerCase($tableInfo.name)});
}
/**
* 添加数据
* @param $!{tool.firstLowerCase($tableInfo.name)} 插入的数据实体
* @return
*/
@ApiOperation(value = "新增数据", notes = "新增数据", response = Result.class)
@ApiImplicitParam(name = "$!{tool.firstLowerCase($tableInfo.name)}", value = " $!{tableInfo.name}实体类", required = true, dataType = "$!{tableInfo.name}")
@PostMapping ("/add")
public Result add(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}){
$!{tableInfo.name} insert = this.$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!{tool.firstLowerCase($tableInfo.name)});
return ResultUtil.success();
}
/**
* 查询所有
* @return 返回数据集合
*/
@ApiOperation(value = "分页查询所有数据", notes = "分页查询所有数据")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "当前页码", required = true, dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "limit", value = "每页显示条数", required = true, dataType = "int", paramType = "query")
})
@GetMapping("/queryAll")
public Result queryAll(@ApiIgnore @RequestParam Map<String, Object> params){
Page<$!{tableInfo.name}> pages = this.$!{tool.firstLowerCase($tableInfo.name)}Service.queryAllByLimit(params);
return ResultUtil.success(pages.getTotal(),pages.getData(),ModelUtil.getFieldAndComment($!{tableInfo.name}.class));
}
/**
* 根据id删除数据
* @param id
* @return
*/
@ApiOperation(value = "根据$!pk.name 删除数据", notes = "根据$!pk.name 删除数据", response = Result.class)
@ApiImplicitParam(name = "$!pk.name", value = "主键$!pk.name", required = true, dataType = "$!pk.shortType")
@GetMapping("/remove")
public Result remove(@RequestParam $!pk.shortType $!pk.name){
boolean b = this.$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id);
return ResultUtil.success();
}
/**
* 修改数据
* @param $!{tool.firstLowerCase($tableInfo.name)} 修改实体类
* @return
*/
@ApiOperation(value = "修改数据", notes = "修改数据", response = Result.class)
@ApiImplicitParam(name = "$!{tool.firstLowerCase($tableInfo.name)}", value = " $!{tableInfo.name}实体类", required = true, dataType = "$!{tableInfo.name}")
@PostMapping("/modify")
public Result modify(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}){
$!{tableInfo.name} update = this.$!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)});
return ResultUtil.success();
}
}
mapper模板
##引入mybatis支持
$!mybatisSupport
##设置保存名称与保存位置
$!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>
<!--定义所有字段 sql 片段-->
<sql id="Base_Column_List">
#allSqlColumn()
</sql>
<!--定义动态查询 sql 片段-->
<sql id="BaseWhere">
<where>
#foreach($column in $tableInfo.otherColumn)
#if($column.type.equals("java.lang.String"))
<if test="$!column.name != null and $!column.name != ''">
and $!column.obj.name LIKE CONCAT('%',#{$!column.name},'%')
</if>
#else
<if test="$!column.name != null">
and $!column.obj.name = #{$!column.name}
</if>
#end
#end
</where>
</sql>
<!--查询单个-->
<select id="queryById" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List"/>
from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!--查询数据总有条数-->
<select id="count" resultType="long">
select count(*) from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
</select>
<!--分页查询所有-->
<select id="queryAllByLimit" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List"/>
from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
<include refid="BaseWhere"/>
limit #{start}, #{length}
</select>
<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="$!{tableInfo.name}Map">
select
<include refid="Base_Column_List"/>
from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
<include refid="BaseWhere"/>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end)
values (#foreach($column in $tableInfo.fullColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
</insert>
<!--通过主键修改数据-->
<update id="update">
update $!{tableInfo.obj.parent.name}.$!{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.parent.name}.$!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
</delete>
</mapper>
我的项目中写了一些自己的工具类,还加了swagger文档注解,