A, dao template
##定义初始变量
#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}) table database access layer
*
* @author xiaoG
* @Since $! Time.currTime ()
* /
public interface $! {{TableName}
/ **
* by single query data ID
*
! * @param $ pk.name primary key
* @return instance object
* /
! $ {} tableInfo.name the findById (!! $ $ pk.shortType pk.name) ;
/ **
* entity not empty query attribute as a filter condition
*
* @param $! tool.firstLowerCase ($!} {tableInfo.name) instance object
* @return object list
* /
list <$!} {tableInfo.name> findBy $! {TableInfo } .name ($ {$} tableInfo.name tool.firstLowerCase (tableInfo.name $ {})!!!) ;
/ **
* New Entity property is not null columns
*
* @param $! Tool.firstLowerCase ($!} {TableInfo.name) instance object
* @return impact number line
* /
int INSERT ($!} {TableInfo.name $! tool.firstLowerCase (tableInfo.name} $ {!)) ;
/ **
* batch add all the columns, the length of the list can not be 0, and the list is null or uniform uniform id is not null
*
* @param instance object list set list
* @return affect the number of rows
* /
int insertList (List <tableInfo.name} $ {!> List) ;
/ **
* modified by the entity's primary key attribute is not null columns
*
* @param tool.firstLowerCase $ ($!! {tableInfo.name}) instance object
* @return affect the number of rows
* /
Update int ($ {$} tableInfo.name tool.firstLowerCase (tableInfo.name $ {})!!!) ;
/ **
* modified entity list through the primary key, the length of the list can not be 0
* Note: when an entity attribute is null , respectively the corresponding column will be updated to null
*
@param List instance object *
@return impact number rows *
* /
int UpdateBatch (List <tableInfo.name} $ {!> List) ;
/ **
* delete data by primary key
*
* @param $ pk.name primary key!
* @return affect the number of rows
* /
int deleteById ($ pk.shortType $ pk.name!!) ;
/ **
* batch delete empty property by a non-entity
*
* @param $ Tool! .firstLowerCase ($! {tableInfo.name}) entity
* @return affect the number of rows
* /
tableInfo.name deleteBatchBy $ {} int ($ {$} tableInfo.name tool.firstLowerCase (tableInfo.name $ {})!!!)! ;
/ **
* delete list by the primary key, the length of the list can not be 0
*
* @ param list primary key list
* @return impact number line
* /
int deleteBatchByIds (list <$ pk.shortType!> list) ;
}
Two, mapper.xml template
##引入mybatis支持
$!mybatisSupport
##设置保存名称与保存位置
$!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}.dao.$!{tableInfo.name}Dao">
<resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}ResultMap">
#foreach($column in $tableInfo.fullColumn)
<result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
</resultMap>
<sql id="table_field">
#allSqlColumn()
</sql>
<!--通过Id查询单个-->
<select id="findById" resultMap="$!{tableInfo.name}ResultMap">
select
<include refid="table_field" />
from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
where $!pk.obj.name = #{$!pk.name}
</select>
<!--通过实体不为空的属性作为筛选条件查询-->
<select id="findBy$!{tableInfo.name}" resultMap="$!{tableInfo.name}ResultMap">
select
<include refid="table_field" />
from $!{tableInfo.obj.parent.name}.$!tableInfo.obj.name
<where>
#foreach($column in $tableInfo.fullColumn)
<if test="$!column.name != null">
and $!column.obj.name = #{$!column.name}
</if>
#end
</where>
</select>
<!--新增实体属性不为null的列-->
<insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}
<trim prefix="(" suffix=")" suffixOverrides=",">
#foreach($column in $tableInfo.fullColumn)
<if test="$!column.name != null">
$!column.obj.name,
</if>
#end
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
#foreach($column in $tableInfo.fullColumn)
<if test="$!column.name != null">
#{$!column.name},
</if>
#end
</trim>
</insert>
<!--批量新增所有列,列表长度不能为0,且列表id统一为null或者统一不为null-->
<insert id="insertList" keyProperty="$!pk.name" useGeneratedKeys="true" parameterType="list">
insert into $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}
(<if test="$!pk.name != null">$!pk.obj.name,</if>#foreach($column in $tableInfo.otherColumn)$!{column.obj.name}#if($velocityHasNext), #end#end)
values
<foreach item="item" collection="list" separator="," open="" close="" index="index">
(<if test="$!pk.name != null">#{$!pk.name},</if>#foreach($column in $tableInfo.otherColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
</foreach>
</insert>
<!--通过主键修改实体属性不为null的列-->
<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>
<!--通过主键修改实体列表,列表长度不能为0,注意:当实体属性为null时,对应的列也会别更新为null-->
<update id="updateBatch" parameterType="list">
update $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} set
#foreach($column in $tableInfo.otherColumn)
<foreach item="item" collection="list" separator="," open="" close="" index="index">
$!column.obj.name = CASE
WHEN #{item.$!pk.name} THEN #{item.$!column.name}
END
</foreach>
#end
where $!pk.obj.name in
<foreach item="item" collection="list" separator="," open="(" close=")" index="index">
#{item.$!pk.name}
</foreach>
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete from $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
</delete>
<!--通过实体非空属性删除-->
<delete id="deleteBatchBy$!{tableInfo.name}">
delete from $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}
<where>
#foreach($column in $tableInfo.otherColumn)
<if test="$!column.name != null">
and $!column.obj.name = #{$!column.name}
</if>
#end
</where>
</delete>
<!--通过主键列表删除,列表长度不能为0-->
<delete id="deleteBatchByIds" parameterType="list">
delete from $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name} where $!pk.obj.name in
<foreach item="item" collection="list" separator="," open="(" close=")" index="index">
#{item}
</foreach>
</delete>
</mapper>