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>
mybatis 批量保存,并且唯一约束的更多相关文章
- mybatis批量保存的两种方式(高效插入)
知识点:mybatis中,批量保存的两种方式 1.使用mybatis foreach标签 2.mybatis ExecutorType.BATCH 参考博客:https://www.jb51.net/ ...
- mybatis单笔批量保存
在上一篇写了接口调用解析返回的xml,并赋值到实体.这一篇主要介绍,如何保存实体数据. 一,xml样例 <?xml version="1.0" encoding=" ...
- mybatis批量插入oracle时报错:unique constraint (table name) violated
mybatis批量插入oracle时报错:unique constraint (table name) violated,是因为插入的集合中有两条相同唯一约束的数据.
- Mybatis批量insert报错的解决办法【the right syntax to use near '' at line...】
Java中使用Mybatis批量插入数据时Mapper.xml中的sql如下: <insert id="batchSave"> into t_emp(emp_name, ...
- MyBatis批量插入数据(MySql)
由于项目需要生成多条数据,并保存到数据库当中,在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,项目使用了Spring+MyBatis,所以打算使用MyBatis批量插入,应 ...
- 【java】itoo项目实战之hibernate 批量保存优化
在itoo中.基本上每一个系统都有一个导入功能,大量的数据填写进入excel模板中.然后使用导入功能导入的数据库中,这样能够大大的提高工作效率. 那么导入就涉及到了批量保存数据库的问题了. 那么通常情 ...
- 使用SpringBoot-JPA进行自定义的保存及批量保存
更多精彩博文,欢迎访问我的个人博客 说明 SpringBoot版本:2.1.4.RELEASE java版本:1.8 文中所说JPA皆指spring-boot-starter-data-jpa 使用J ...
- oracle+mybatis批量插入踩坑记
最近在项目中需要使用oracle+mybatis批量插入数据,因为自增主键,遇到问题,现记录如下: 一.常用的两种sql写法报错 1.insert ... values ... <insert ...
- mysql 唯一约束
ALTER TABLE user ADD UNIQUE (username,userid) 对表user增加username和userid的唯一约束 ALTER TABLE tablename AD ...
随机推荐
- 手写一个Promise/A+,完美通过官方872个测试用例
前段时间我用两篇文章深入讲解了异步的概念和Event Loop的底层原理,然后还讲了一种自己实现异步的发布订阅模式: setTimeout和setImmediate到底谁先执行,本文让你彻底理解Eve ...
- JVM类加载过程详细分析
双亲委派加载模型 为什么需要双亲委派加载模型 主要是为了安全,避免用户恶意加载破坏JVM正常运行的字节码文件,比如说加载一个自己写的java.util.HashMap.class.这样就有可能造成包冲 ...
- IIC驱动学习笔记,简单的TSC2007的IIC驱动编写,测试
IIC驱动学习笔记,简单的TSC2007的IIC驱动编写,测试 目的不是为了编写TSC2007驱动,是为了学习IIC驱动的编写,读一下TSC2007的ADC数据进行练习,, Linux主机驱动和外设驱 ...
- 如何设置微信小程序顶部标题
直接在对应的xxx.json中支配如下,就可以了哈 { "backgroundTextStyle": "light", //字体 "navigatio ...
- protobuf总结
1.protobuf是什么? protobuf(protocol buffers)是一种语言中立,平台无关,可扩展的序列化数据的格式,可以用于通信协议,数据存储等. protobuf 相比于xml,j ...
- ubuntu18.04配置宽带上网
1.将 /etc/NetworkManager 目录下的 managed标签改为true 2.将 /etc/network/ 目录下的 interfaces文件只留下前两行: auto lo ifac ...
- mysql中的PACK_KEYS
原文 http://jackyrong.iteye.com/blog/2170222 在mysql的myisam引擎中,有一个是容易忽视的,叫压缩索引PACK_KEYS , myISAM使用前缀压缩 ...
- GeoGebra重复手段实现
1.自定义工具部分可以在网上搜一些别人做的工具,主要是把自己经常做的一些任务做成工具,减少重复过程 2.列表部分的简单操作如图所示,实现对三个点的多项式拟合 3.通过序列指令格式可以做一个好玩的效果, ...
- golang实现常用集合原理介绍
golang本身对常用集合的封装还是比较少的,主要有数组(切片).双向链表.堆等.在工作中可能用到其他常用的集合,于是我自己对常用的集合进行了封装,并对原理做了简单介绍,代码库地址:https://g ...
- O - Employment Planning HDU - 1158
题目大意: 第一行一个n,表示共n个月份,然后第二行分别表示一个工人的聘请工资,月薪水,解雇工资.第三行是n个月每个月需要的工人的最少数目.然后求最少花费 题解: dp[i][j] 表示第i个月聘请j ...