gitee地址
文档地址
自定义记录
TypeMapper
java.lang.String | varchar(\(\d+\))? |
java.lang.String | char(\(\d+\))? |
java.lang.String | text |
java.lang.Double | decimal(\(\d+\))? |
java.lang.Double | decimal(\(\d+,\d+\))? |
java.lang.Integer | integer |
java.lang.Integer | int(\(\d+\))? |
java.lang.Integer | int4 |
java.lang.Long | int8 |
java.lang.Long | bigint(\(\d+\))? |
java.util.String | datetime |
java.util.Date | timestamp |
java.lang.Boolean | boolean |
java.lang.Integer | tinyint(\(\d+\))? |
java.lang.Date | date |
java.lang.Object | double(\(\d+,\d+\))? |
java.lang.Object | longtext |
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 id="basesql">
#allSqlColumn()
</sql>
<!--查询单个-->
<select id="queryById" resultMap="$!{tableInfo.name}Map">
select
<include refid="basesql"></include>
from $!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="$!{tableInfo.name}Map">
select
<include refid="basesql"></include>
from $!tableInfo.obj.name
limit #{offset}, #{limit}
</select>
<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="$!{tableInfo.name}Map">
select
<include refid="basesql"></include>
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="insertSelect" 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>
VALUES
<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.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="$!{tableNameLowerCase}List" 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="$!{tableNameLowerCase}List" 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>
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.Param;
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 offset 查询起始位置
* @param limit 查询条数
* @return 对象列表
*/
List<$!{tableInfo.name}> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);
/**
* 通过实体作为筛选条件查询
*
* @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 insertSelect($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param $!{tableNameLowerCase}List 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("$!{tableNameLowerCase}List") List<$!{tableInfo.name}> $!{tableNameLowerCase}List);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param $!{tableNameLowerCase}List 实例对象列表
* @return 影响行数
*/
int insertOrUpdateBatch(@Param("$!{tableNameLowerCase}List") List<$!{tableInfo.name}> $!{tableNameLowerCase}List);
/**
* 修改数据
*
* @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
* @return 影响行数
*/
int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
/**
* 通过主键删除数据
*
* @param $!pk.name 主键
* @return 影响行数
*/
int deleteById($!pk.shortType $!pk.name);
}
controller模板
##定义初始变量标签:代码生成,插件,end,name,tableInfo,column,tool,firstLowerCase,easycode From: https://blog.51cto.com/u_11334685/5824826
#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.web.bind.annotation.*;
import com.common.Result;
import javax.annotation.Resource;
import java.util.List;
/**
* $!{tableInfo.comment}
*
* @author $!author
* @date $!time.currTime()
*/
@RestController
@RequestMapping("sim/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
/**
* 服务对象
*/
@Resource
private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;
/**
*查询单条
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("query")
public Result query(@RequestParam(required = true) $!pk.shortType id) {
$!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)} = $!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id);
return Result.success($!{tool.firstLowerCase($tableInfo.name)});
}
/**
* 查询多条
* @param $!{tool.firstLowerCase($tableInfo.name)} 条件实体
* @return 多条记录
*/
@GetMapping("queryAll")
public Result queryAll($!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}){
List<$!{tableInfo.name}> $!{tool.firstLowerCase($tableInfo.name)}List = $!{tool.firstLowerCase($tableInfo.name)}Service.queryAll($!{tool.firstLowerCase($tableInfo.name)});
return Result.success($!{tool.firstLowerCase($tableInfo.name)}List);
}
/**
* 分页查询多条
* @param page 页码
* @param limit 条数
* @param $!{tool.firstLowerCase($tableInfo.name)} 条件实体
* @return 多条记录
*/
@GetMapping("/queryAllByPage")
public Result queryAllByPage(Integer page, Integer limit,$!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}){
if(page == null){
page = 1;
}
if(page <= 0){
page = 1;
}
if(limit == null){
limit = 10;
}
PageHelper.startPage(page,limit);
List<$!{tableInfo.name}> $!{tool.firstLowerCase($tableInfo.name)}List = $!{tool.firstLowerCase($tableInfo.name)}Service.queryAll($!{tool.firstLowerCase($tableInfo.name)});
PageInfo<$!{tableInfo.name}> pageInfo = new PageInfo<>($!{tool.firstLowerCase($tableInfo.name)}List);
return Result.success(pageInfo);
}
/**
* 插入
* @param $!{tool.firstLowerCase($tableInfo.name)} 实体
* @return Result
*/
@PostMapping("insert")
public Result insert(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}){
$!{tool.firstLowerCase($tableInfo.name)}Service.insert($!{tool.firstLowerCase($tableInfo.name)});
return Result.success();
}
/**
* 更新
* @param $!{tool.firstLowerCase($tableInfo.name)} 实体
* @return Result
*/
@PostMapping("update")
public Result update(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}){
$!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)});
return Result.success();
}
/**
* 更新
* @param id id
* @return Result
*/
@GetMapping("del")
public Result del(@RequestParam(required = true) Integer id){
$!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id);
return Result.success();
}
}