mapper.xml
<?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="xxxDao">
<resultMap type="xxx" id="aaa">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="xxx_xxx" column="xxxXxx" jdbcType="INTEGER"/>
</resultMap>
<sql id="tab_column">
xxx, xxx2
</sql>
<sql id="tab_column_no_id">
xxx, xxx2
</sql>
<!-- 实体属性 -->
<sql id="domain_properties">
#{xxx},
#{xxxXxx}
</sql>
<!--查询单个-->
<select id="queryById" resultMap="aaa">
select
<include refid="tab_column"/>
from xxx
where id = #{id}
</select>
<!--通过实体作为筛选条件查询-->
<select id="queryAll" resultMap="aaa">
select
<include refid="tab_column"/>
from xxx
<where>
<if test="xx != null">
and xx= #{xx}
</if>
<if test="xx != null and xx != ''">
and xx = #{xxXx}
</if>
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into xxx(<include refid="tab_column"/>)
values (<include refid="domain_properties"/>)
</insert>
<insert id="insertBatch" keyProperty="id" useGeneratedKeys="true" parameterType="map">
insert into xxx(<include refid="tab_column_no_id"/>)
values
<foreach collection="entities" item="entity" index="index" separator=",">
(#{entity.xxx}, #{entity.xxxXxx})
</foreach>
</insert>
<insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true" parameterType="map">
insert into xxx(<include refid="tab_column"/>)
values
<foreach collection="entities" item="entity" index="index" separator=",">
(#{entity.xxx}, #{entity.xxxXxx})
</foreach>
on duplicate key update
xxx= values(xxx) , xxx_xxx = values(xxx_xxx)
</insert>
<!--通过主键修改数据-->
<update id="update">
update xxx
<set>
<if test="xxx != null and xxx != ''">
xxx= #{xxx},
</if>
<if test="xxxXxx != null and xxxXxx != ''">
xxx_xxx = #{xxxXxx}
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete from xxx where id = #{id}
</delete>
</mapper>