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 ...
随机推荐
- E - E CodeForces - 1100E(拓扑排序 + 二分)
E - E CodeForces - 1100E 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方 ...
- STM32F103C8T6最小系统开发板原理图
1.
- js中常见的数据加密与解密的方法
加密在我们前端的开发中也是经常遇见的.本文只把我们常用的加密方法进行总结.不去纠结加密的具体实现方式(密码学,太庞大了). 常见的加密方式 常见的加密算法基本分为这几类, 线性散列算法(签名算法)MD ...
- 简单记录下RestTemplate 中postForObject调用例子
学无止境! 今天无意中做了下RestTemplate调用demo,简单的尝试了下一个项目调用另一个项目接口示例 在A项目中创建可访问controller 然后在B项目中进行调用 调用成功
- jmeter术语
1.负载:模拟业务请求操作对服务器造成压力的过程 2.性能测试(performance testing):模拟用户负载来测试系统在负载情况下,系统的响应时间.吞吐量等指标是否满足性能要求 3.负载测试 ...
- scratch算立方根
10((1/3)lgx)=x(1/3)也就是立方根
- Hadoop(二)搭建Hadoop
0.部署计划 本文使用的版本是 red hat 6.8 -本来想用Centos7搭建的,但是工作需要还是换成这个了,不用红帽子用Centos 6系列的应该也可以 JDK 1.8 Hadoop 2.7. ...
- docker-compose中加入nginx 日志和部署下载
服务器部署了nginx镜像,所以加入一个日志查看,添加一下静态页面下载. 1.查看nginx镜像怎么部署的 nginx: image: nginx ports: - '80:80' volumes: ...
- 运行jmeter.bat时 提示 not able to find java executable or version
安装过好几次,这是第一次遇到运行jmeter.bat时 提示 not able to find java executable or version Please check your Java in ...
- 对于不平凡的我来说,从小我就在想为啥别人就什么都能拥有,而看看自己却什么都没有,对于原来的我就会抱怨爸妈怎么没有别人父母都能给自己想要的,可我从未想过父母的文化只有小学,其实父母内心也有太多的辛酸,所以我不甘愿如此,从此让我在大学里面直接选择一个让我巨大的转折————IT。
对于不平凡的我来说,从小我就在想为啥别人就什么都能拥有,而看看自己却什么都没有,对于原来的我就会抱怨爸妈怎么没有别人父母都能给自己想要的,可我从未想过父母的文化只有小学,其实父母内心也有太多的辛酸,所 ...