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 ...
随机推荐
- Spring ApplicationContext 容器
Spring ApplicationContext 容器 Application Context 是 BeanFactory 的子接口,也被成为 Spring 上下文. Application Con ...
- dis反汇编查看实现
dis库是python(默认的CPython)自带的一个库,可以用来分析字节码 >>> import dis >>> def add(a, b = 0): ... ...
- 存储机制 cookie session jwt token
cookieCookie的诞生 由于HTTP协议是无状态的,而服务器端的业务必须是要有状态的.Cookie诞生的最初目的是为了存储web中的状态信息,以方便服务器端使用.比如判断用户是否是第一次访问网 ...
- 适用于小白的 python 快速入门教程
文章更新于:2020-02-17 按照惯例,需要的文件附上链接放在文首 文件名:python-3.7.6-amd64.exe 文件大小:25.6 M 下载链接:https://www.lanzous. ...
- node+express+mysql实现简单的数据增删改查
前提 电脑已经安装了node,express,mysql. 实现步骤 1.新建数据库表 附数据表结构: 2.创建exprss项目 express -e myapp 新建一个以ejs为模板的expre ...
- 《深入理解 Java 虚拟机》读书笔记:垃圾收集器与内存分配策略
正文 垃圾收集器关注的是 Java 堆和方法区,因为这部分内存的分配和回收是动态的.只有在程序处于运行期间时才能知道会创建哪些对象,也才能知道需要多少内存. 虚拟机栈和本地方法栈则不需要过多考虑回收的 ...
- buuctf misc wp 01
buuctf misc wp 01 1.金三胖 2.二维码 3.N种方法解决 4.大白 5.基础破解 6.你竟然赶我走 1.金三胖 root@kali:~/下载/CTF题目# unzip 77edf3 ...
- Java基础】并发 - 多线程
Java基础]并发 - 多线程 分类: Java2014-05-03 23:56 275人阅读 评论(0) 收藏 举报 Java 目录(?)[+] 介绍 Java多线程 多线程任务执行 大多数 ...
- 【一统江湖的大前端(9)】TensorFlow.js 开箱即用的深度学习工具
示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文目录 目录 一. 上手TensorFlow.js 二. ...
- qad progress数据库启动出错解决
1. 启动时报:SYSTEM ERROR: Wrong dbkey in block. Found 0, should be 6342528 in area 36. (439) ** Save fi ...