对象DO与数据库字段间的映射
<resultMap id="UserResult" type="UserDO">
<result property="id" column="id" jdbcType="BIGINT"/>
<result property="gmtCreate" column="gmt_create" jdbcType="TIMESTAMP"/>
<result property="gmtModified" column="gmt_modified" jdbcType="TIMESTAMP"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="deleted" column="deleted" jdbcType="DECIMAL"/>
</resultMap>
2、定义sql语句字段模版
字符类型判断null和' ',其他类型只判null
Integer,Long之类的类型传0,如果是id要加判断''或者判断是否等于0,这样对于0值,会直接跳过判断语句
如果是状态类型的数值,只要判null,不然会过滤掉0这个条件判断
<sql id="baseColumns">
//sql语句前置DO对象字段模版
gmt_create,
gmt_modified,
user_name,
deleted
<sql id="insertColumns">
//插入sql语句后置传入对象模版
#{id},
now(),
now(),
#{userName},
#{deleted}
<sql id="batchInsertColumns">
/批量插入sql语句后置传入对象模版,配合foreach使用
#{item.id},
now(),
now(),
#{item.userName},
#{item.deleted}
<!--动态更新列-->
<sql id="updateColumns">
//sql语句更新列模版
<if test="userName != null and userName != ''">,
user_name = #{userName},
<if test="deleted != null">,
deleted = #{deleted}
<!--批量动态更新列-->
<sql id="batchUpdateColumns">
//sql语句批量更新列模版
<if test="item.userName != null and item.userName != ''">,
user_name = #{item.userName}
<sql id="whereParam">
//sql语句查询条件模版
<where>
<if test="id != null and id != ''">
//基础模版
AND id = #{id}
<if test="start != null">
//日期格式模版
<![CDATA[
AND gmt_create >= #{start} ]]>
<if test="end != null">
// <![CDATA[]]> 不让xml编译内部语句
<![CDATA[
AND gmt_create <= #{end} ]]>
<if test="userName != null and userName != ''">
//模糊查询模版
AND user_name like CONCAT('%',#{userName},'%')
<if test="userName != null and userName != ''">
//模糊查询使用bind标签 https://www.cnblogs.com/youmingDDD/p/9435422.html
<bind name="userNameLike" value=" '%' + userName + '%' "/>
and user_name like #{userNameLike}
</where>
//基础实例
<insert id="insert" parameterType="UserDO">
insert into sx_user (<include refid="baseColumns"/>)
values (<include refid="insertColumns"/>)
</insert>
<insert id="batchInsert" parameterType="java.util.List">
INSERT INTO sx_user (<include refid="baseColumns"/>)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(<include refid="batchInsertColumns"/>)
</foreach>
</insert>
<update id="updateById" parameterType="UserDO">
update sx_user set gmt_modified=now(),
<include refid="updateColumns"/>
where id = #{id}
</update>
<update id="batchUpdateById" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update sx_user set gmt_modified=now(),
<include refid="batchUpdateColumns"/>
where id = #{item.id}
</foreach>
</update>
<select id="countPageByCondition" parameterType="QueryCondition" resultType="java.lang.Long">
SELECT COUNT(*)
FROM sx_user
<include refid="whereParam"/>
</select>
<select id="selectList" resultMap="UserResult">
select
<include refid="baseColumns"/>
from sx_user
where id = #{id}
</select>
<select id="listByIds" resultMap="UserResult">
select
<include refid="baseColumns"/>
from sx_user
where id IN
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
//in中多个参数拼接
<!-- 如果直接传String ids (case:1,2,3,4)
<foreach collection="ids .split(',')" item="id" open="(" separator="," close=")">
#{id}
</foreach>
#{item}
</foreach>
</select>
<delete id="deleteById">
delete from sx_user where id = #{id}
</delete>
参考学习:https://blog.csdn.net/lu1024188315/article/details/78758943
update table
<trim prefix="set" suffixOverrides=",">
<trim prefix="user_name = case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.userName!=null">
when id=#{item.id} then #{item.userName}
</foreach>
</trim>
<trim prefix="phone = case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then ''
</foreach>
</trim>
<trim prefix="num = case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then null
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="item" index="index">
id=#{item.id}
</foreach>
</update>