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 ...
随机推荐
- list容器排除重复单词的程序
#include<iostream> #include<fstream> #include<string> #include<algorithm> #i ...
- JVM系列十(虚拟机性能监控神器 - BTrace).
BTrace 是什么? BTrace 是一个动态安全的 Java 追踪工具,它通过向运行中的 Java 程序植入字节码文件,来对运行中的 Java 程序热更新,方便的获取程序运行时的数据信息,并且,保 ...
- hicharts中treemap添加超链接
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- go 递归函数
一.什么是递归函数? 当一个函数在其函数体内调用自身,则称之为递归. 二.引用经典例子:斐波那切数列 package main import "fmt" func main(){ ...
- Flask 入门(六)
连接数据库 任何一门后端语言都可以连接数据库,python-flask也不例外 flask利用SQLAlchemy ORM连接数据库 接下来,我门来练习如何连接数据库: 1.首先,电脑上得有mysql ...
- JAVA中基本类型和包装类之间的相互转换
转自:https://www.imooc.com/code/2250 仅做个人学习记录之用,侵删. 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 ...
- buuctf misc wp 01
buuctf misc wp 01 1.金三胖 2.二维码 3.N种方法解决 4.大白 5.基础破解 6.你竟然赶我走 1.金三胖 root@kali:~/下载/CTF题目# unzip 77edf3 ...
- leetcode c++做题思路和题解(1)——常规题总结
常规题总结 0. 目录 两数之和 1. 两数之和 耗时4ms(98.82%),内存6.2m. 两数之和--寻找中值向两边扩散法 1.1 思路 思路很简单,就是先找数组中target/2的前后两个值,然 ...
- 发布公开的pod
发布公开的pod 方便项目 通过cocoapods 使用,便于版本版本管理,下面是简单步奏: 0.首次操作先要注册Trunk: pod trunk register zhujin001xb@163.c ...
- elementUI踩坑
1.滚动条消失,body中莫名出现行内样式overflow: hidden; 在做某个图片上传,显示功能出现的问题.控制台并没有报错,代码也并无相关操作 必须重新刷新页面之后滚动条才会显示出来 几天后 ...