mybatis 批量保存,并且唯一约束
1、主键返回在insert配置中添加两个属性 useGeneratedKeys="true" keyProperty="id"
2、唯一约束冲突可以使用 ON DUPLICATE KEY UPDATE 解决,但是不会返回主键
3、批量保存主键冲突使用下面方式保存
<?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="com.qingclass.delta.modules.customer.dao.WxProDepartmentDao"> <!-- 可根据自己的需求,是否要使用 --> <resultMap type="com.qingclass.delta.modules.customer.entity.WxProDepartmentEntity" id="wxProDepartmentMap"> <result property="departId" column="depart_id"/> <result property="departName" column="depart_name"/> <result property="departNameEn" column="depart_name_en"/> <result property="departPid" column="depart_pid"/> <result property="order" column="order"/> <result property="createTime" column="create_time"/> <result property="updateTime" column="update_time"/> </resultMap> <select id="queryObject" resultType="com.qingclass.delta.modules.customer.entity.WxProDepartmentEntity"> select * from wx_pro_department where depart_id = #{value} </select> <select id="queryList" resultType="com.qingclass.delta.modules.customer.entity.WxProDepartmentEntity"> select * from wx_pro_department <choose> <when test="sidx != null and sidx.trim() != ''"> order by ${sidx} ${order} </when> <otherwise> order by depart_id desc </otherwise> </choose> <if test="offset != null and limit != null"> limit #{offset}, #{limit} </if> </select> <select id="queryTotal" resultType="int"> select count(*) from wx_pro_department </select> <insert id="save" parameterType="com.qingclass.delta.modules.customer.entity.WxProDepartmentEntity" useGeneratedKeys="true" keyProperty="id"> insert into wx_pro_department ( `depart_id`, `depart_name`, `depart_name_en`, `depart_pid`, `order` ) values ( #{departId}, #{departName}, #{departNameEn}, #{departPid}, #{order} ) </insert> <insert id="saveBatch" parameterType="com.qingclass.delta.modules.customer.entity.WxProDepartmentEntity"> insert into wx_pro_department ( `depart_id`, `depart_name`, `depart_name_en`, `depart_pid`, `order` ) values <foreach collection="list" item="item" separator=","> ( #{item.departId}, #{item.departName}, #{item.departNameEn}, #{item.departPid}, #{item.order} ) </foreach> ON DUPLICATE KEY UPDATE depart_name = values(depart_name), depart_pid = values(depart_pid), `order` = values(`order`) </insert> <update id="update" parameterType="com.qingclass.delta.modules.customer.entity.WxProDepartmentEntity"> update wx_pro_department <set> <if test="departName != null">`depart_name` = #{departName}, </if> <if test="departNameEn != null">`depart_name_en` = #{departNameEn}, </if> <if test="departPid != null">`depart_pid` = #{departPid}, </if> <if test="order != null">`order` = #{order} </if> </set> where depart_id = #{departId} </update> <delete id="delete"> delete from wx_pro_department where depart_id = #{value} </delete> <delete id="deleteAll"> delete from wx_pro_department where depart_id = depart_id </delete> <delete id="deleteBatch"> delete from wx_pro_department where depart_id in <foreach item="departId" collection="array" open="(" separator="," close=")"> #{departId} </foreach> </delete> </mapper>