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>

mybatis 批量保存,并且唯一约束的更多相关文章

  1. mybatis批量保存的两种方式(高效插入)

    知识点:mybatis中,批量保存的两种方式 1.使用mybatis foreach标签 2.mybatis ExecutorType.BATCH 参考博客:https://www.jb51.net/ ...

  2. mybatis单笔批量保存

    在上一篇写了接口调用解析返回的xml,并赋值到实体.这一篇主要介绍,如何保存实体数据. 一,xml样例 <?xml version="1.0" encoding=" ...

  3. mybatis批量插入oracle时报错:unique constraint (table name) violated

    mybatis批量插入oracle时报错:unique constraint (table name) violated,是因为插入的集合中有两条相同唯一约束的数据.

  4. Mybatis批量insert报错的解决办法【the right syntax to use near '' at line...】

    Java中使用Mybatis批量插入数据时Mapper.xml中的sql如下: <insert id="batchSave"> into t_emp(emp_name, ...

  5. MyBatis批量插入数据(MySql)

    由于项目需要生成多条数据,并保存到数据库当中,在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,项目使用了Spring+MyBatis,所以打算使用MyBatis批量插入,应 ...

  6. 【java】itoo项目实战之hibernate 批量保存优化

    在itoo中.基本上每一个系统都有一个导入功能,大量的数据填写进入excel模板中.然后使用导入功能导入的数据库中,这样能够大大的提高工作效率. 那么导入就涉及到了批量保存数据库的问题了. 那么通常情 ...

  7. 使用SpringBoot-JPA进行自定义的保存及批量保存

    更多精彩博文,欢迎访问我的个人博客 说明 SpringBoot版本:2.1.4.RELEASE java版本:1.8 文中所说JPA皆指spring-boot-starter-data-jpa 使用J ...

  8. oracle+mybatis批量插入踩坑记

    最近在项目中需要使用oracle+mybatis批量插入数据,因为自增主键,遇到问题,现记录如下: 一.常用的两种sql写法报错 1.insert ... values ... <insert ...

  9. mysql 唯一约束

    ALTER TABLE user ADD UNIQUE (username,userid) 对表user增加username和userid的唯一约束 ALTER TABLE tablename  AD ...

随机推荐

  1. E - River Hopscotch POJ - 3258(二分)

    E - River Hopscotch POJ - 3258 Every year the cows hold an event featuring a peculiar version of hop ...

  2. js获取dom节点之 id 获取

    在JavaScript中,标准的id选择器调用语法是: document.getElementById('myid').style.width = pc + "%"; 但是,今天发 ...

  3. eolinker测试增强

    地址:https://www.eolinker.com Chrome: https://chrome.google.com/webstore/detail/eolinker/mdbgchaihbacj ...

  4. 1019 General Palindromic Number (20 分)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  5. win10无法使用VMwareWorkstation的解决办法

    最近(2019-10)国庆期间,微软更新了一次win10. 此次更新导致很多同学的Workstation pro不能用了. 主要的解决办法有一下几种: 卸载最新的系统更新包 在控制面板>> ...

  6. javascript入门 之 ztree(二 标准json数据)

    1.代码 <!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - Standard Data </T ...

  7. CVPR2018关键字分析生成词云图与查找

    今日目标:爬取CVPR2018论文,进行分析总结出提到最多的关键字,生成wordCloud词云图展示,并且设置点击后出现对应的论文以及链接 对任务进行分解: ①爬取CVPR2018的标题,简介,关键字 ...

  8. Hadoop在Ubuntu的安装和使用

    由于小编在本学期有一门课程需要学习hadoop,需要在ubuntu的linux系统下搭建Hadoop环境,在这个过程中遇到一些问题,写下这篇博客来记录这个过程,并把分享给大家. Hadoop的安装方式 ...

  9. tf.nn.softmax_cross_entropy_with_logits 分类

    tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 参数: logits:就是神经网络最后一层的输出,如果有batch ...

  10. 一个spring 基本知识的微博(怎么加载多个xml、多个property文件、aop配置、监视器)

    http://blog.sina.com.cn/s/blog_61c5866d0100ev44.html