mybatis + mysql 批量插入、删除、更新

Student 表结构

批量插入

public int insertBatchStudent(List<Student> students);
<insert id="insertBatchStudent" parameterType="java.util.List" useGeneratedKeys="true">
<selectKey resultType="Long" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into student(stu_no,stu_name,age,sex,address)
values
<foreach collection="list" item="stu" index="index" separator=",">
(#{stu.stuNo},
#{stu.stuName},
#{stu.age},
#{stu.sex},
#{stu.address})
</foreach>
</insert>

根据数组批量删除

public int deleteStudentByIds(String[] ids);
<delete id="deleteStudentByIds" parameterType="String">
delete from student where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>

根据集合批量删除

public int deleteStudentByIds(List<String> ids);
<delete id="deleteStudentByIds" parameterType="java.util.List">
delete from student where id in
<foreach item="id" collection="list" open="(" separator="," close=")">
#{id}
</foreach>
</delete>

批量更新

public int updateBatchStudent(List<Student> students);
<update id="updateBatchStudent" parameterType="java.util.List">
update student set
stu_no =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.stuNo}
</foreach>
,stu_name =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.stuName}
</foreach>
,age =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.age}
</foreach>
,sex =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.sex}
</foreach>
,address =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.address}
</foreach>
where id in
<foreach collection="list" item="stu" index="index" separator="," open="(" close=")">
#{stu.id}
</foreach>
</update>

参考代码

Student.java

```java
package cn.hgnulb.student.domain;

import com.baomidou.mybatisplus.annotation.TableId;

import com.baomidou.mybatisplus.annotation.TableName;

import org.apache.commons.lang3.builder.ToStringBuilder;

import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

@TableName("student")

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

/** 主键 */
@TableId
private Long id; /** 学号 */
private String stuNo; /** 姓名 */
private String stuName; /** 年龄 */
private Integer age; /** 用户性别(0男 1女 2未知) */
private String sex; /** 地址 */
private String address; public void setId(Long id) {
this.id = id;
} public Long getId() {
return id;
} public void setStuNo(String stuNo) {
this.stuNo = stuNo;
} public String getStuNo() {
return stuNo;
} public void setStuName(String stuName) {
this.stuName = stuName;
} public String getStuName() {
return stuName;
} public void setAge(Integer age) {
this.age = age;
} public Integer getAge() {
return age;
} public void setSex(String sex) {
this.sex = sex;
} public String getSex() {
return sex;
} public void setAddress(String address) {
this.address = address;
} public String getAddress() {
return address;
} @Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("stuNo", getStuNo())
.append("stuName", getStuName())
.append("age", getAge())
.append("sex", getSex())
.append("address", getAddress())
.toString();
}

}

</div>

<span class="badge badge-pill badge-danger" data-toggle="collapse" data-target="#mapper_java">StudentMapper.java</span>
<div class="collapse" id="mapper_java">
```java
package cn.hgnulb.student.mapper; import cn.hgnulb.student.domain.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import java.util.List; public interface StudentMapper extends BaseMapper<Student> {
Student selectStudentById(Long id); List<Student> selectStudentList(Student student); int insertStudent(Student student); int insertBatchStudent(List<Student> students); int updateBatchStudent(List<Student> students); int updateStudent(Student student); int deleteStudentById(Long id); int deleteStudentByArrayIds(String[] ids); int deleteStudentByListIds(List<String> ids);
}

StudentMapper.xml

```xml

<resultMap type="Student" id="StudentResult">
<result property="id" column="id"/>
<result property="stuNo" column="stu_no"/>
<result property="stuName" column="stu_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
</resultMap> <sql id="selectStudentVo">
select id, stu_no, stu_name, age, sex, address from student
</sql> <!--按条件查询-->
<select id="selectStudentList" parameterType="Student" resultMap="StudentResult">
<include refid="selectStudentVo"/>
<where>
<if test="stuNo != null and stuNo != ''">and stu_no = #{stuNo}</if>
<if test="stuName != null and stuName != ''">and stu_name like concat('%', #{stuName}, '%')</if>
<if test="age != null ">and age = #{age}</if>
<if test="sex != null and sex != ''">and sex = #{sex}</if>
</where>
</select> <!--按ID查询-->
<select id="selectStudentById" parameterType="Long" resultMap="StudentResult">
<include refid="selectStudentVo"/>
where id = #{id}
</select> <!--插入-->
<insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="stuNo != null and stuNo != ''">stu_no,</if>
<if test="stuName != null and stuName != ''">stu_name,</if>
<if test="age != null ">age,</if>
<if test="sex != null and sex != ''">sex,</if>
<if test="address != null and address != ''">address,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="stuNo != null and stuNo != ''">#{stuNo},</if>
<if test="stuName != null and stuName != ''">#{stuName},</if>
<if test="age != null ">#{age},</if>
<if test="sex != null and sex != ''">#{sex},</if>
<if test="address != null and address != ''">#{address},</if>
</trim>
</insert> <!--按ID删除-->
<delete id="deleteStudentById" parameterType="Long">
delete from student where id = #{id}
</delete> <!--按ID更新-->
<update id="updateStudent" parameterType="Student">
update student
<trim prefix="SET" suffixOverrides=",">
<if test="stuNo != null and stuNo != ''">stu_no = #{stuNo},</if>
<if test="stuName != null and stuName != ''">stu_name = #{stuName},</if>
<if test="age != null ">age = #{age},</if>
<if test="sex != null and sex != ''">sex = #{sex},</if>
<if test="address != null and address != ''">address = #{address},</if>
</trim>
where id = #{id}
</update> <!--批量插入-->
<insert id="insertBatchStudent" parameterType="java.util.List" useGeneratedKeys="true">
<selectKey resultType="Long" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into student(stu_no,stu_name,age,sex,address)
values
<foreach collection="list" item="stu" index="index" separator=",">
(#{stu.stuNo},
#{stu.stuName},
#{stu.age},
#{stu.sex},
#{stu.address})
</foreach>
</insert> <!--按数组批量删除-->
<delete id="deleteStudentByArrayIds" parameterType="String">
delete from student where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete> <!--按集合批量删除-->
<delete id="deleteStudentByListIds" parameterType="java.util.List">
delete from student where id in
<foreach item="id" collection="list" open="(" separator="," close=")">
#{id}
</foreach>
</delete> <!--批量更新-->
<update id="updateBatchStudent" parameterType="java.util.List">
update student set
stu_no =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.stuNo}
</foreach>
,stu_name =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.stuName}
</foreach>
,age =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.age}
</foreach>
,sex =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.sex}
</foreach>
,address =
<foreach collection="list" item="stu" index="index" separator=" " open="case id" close="end">
when #{stu.id} then #{stu.address}
</foreach>
where id in
<foreach collection="list" item="stu" index="index" separator="," open="(" close=")">
#{stu.id}
</foreach>
</update>

```

mybatis + mysql 批量插入、删除、更新的更多相关文章

  1. mybatis+mysql批量插入和批量更新、存在及更新

    mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ...

  2. Mybatis+mysql批量插入性能分析测试

    前言 今天在网上看到一篇文章(后文中的文章指的就是它) https://www.jianshu.com/p/cce617be9f9e 发现了一种有关于mybatis批量插入的新方法,而且看了文章发现我 ...

  3. mybatis+mysql批量插入和批量更新

    一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo) mybatis中mapper.x ...

  4. MyBatis动态批量插入、更新Mysql数据库的通用实现方案

    一.业务背景 由于需要从A数据库提取大量数据同步到B系统,采用了tomikos+jta进行分布式事务管理,先将系统数据源切换到数据提供方,将需要同步的数据查询出来,然后再将系统数据源切换到数据接收方, ...

  5. mybatis mysql 批量插入

    场景描述: 使用mybatis操作mysql数据库,进行批量插入数据,提高代码质量和执行效率. 环境: mybatis spring mysql java xml配置文件 <insert id ...

  6. mybatis中批量插入以及更新

    1:批量插入 批量插入就是在预编译的时候,将代码进行拼接,然后在数据库执行 <insert id="batchInsert" parameterType="java ...

  7. Mybatis 实现批量插入和批量删除源码实例

    Mybatis 实现批量插入数据和批量删除数据 学习内容: 准备工作 1.数据库新建表 2.新建 Maven 项目和设置编译版本及添加依赖 3.新建 db.properties 4.新建 mybati ...

  8. MySQL on duplicate key update 批量插入并更新已存在数据

    业务上经常存在一种现象,需要批量往表中插入多条数据,但在执行过程中,很可能因为唯一键冲突,而导致批量插入失败.因此需要事先判断哪些数据是重复的,哪些是新增的.比较常用的处理方法就是找出已存在的数据,并 ...

  9. Mybatis中实现oracle的批量插入、更新

    oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误 ### Cause: java.sql.S ...

随机推荐

  1. Java中级知识归纳(二)

    六.Java中Collection和Collections的区别? java.util.Collection是一个集合接口,它提供了对集合对象进行基本操作的通用接口方法. java.util.Coll ...

  2. Linux用户和权限——管理文件权限的命令

    Linux用户和权限——管理文件权限的命令 摘要:本文主要学习了Linux中修改文件权限的命令. chown命令 chown命令,主要用于修改文件(或目录)的所有者,除此之外,这个命令也可以修改文件( ...

  3. mockjs 在项目中vue项目中使用

    一.为什么要使用mockjs 总结起来就是在后端接口没有开发完成之前,前端可以用已有的接口文档,在真实的请求上拦截ajax,并根据mockjs的mock数据的规则,模拟真实接口返回的数据,并将随机的模 ...

  4. 汇编指令之ADC、SBB、XCHG、MOVS指令

    版权声明:本文为博主原创文章,转载请附上原文出处链接和本声明.2019-08-25,23:52:49作者By-----溺心与沉浮----博客园 介绍完这些基础指令,后面就讲到汇编JCC指令了,我觉得介 ...

  5. 项目进程中input的onblur事件挂不上去,失效问题解决记录

    一开始直接在js文件中写 var n=document.querySelector("input"); n.onblur=funcition(){ ///事件过程吧啦啦啦 }; 事 ...

  6. ElasticSearch最全分词器比较及使用方法

    介绍:ElasticSearch 是一个基于 Lucene 的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口.Elasticsearch 是用 Java 开 ...

  7. python关于SSL的认证--pycurl模块使用

    今天在做微信支付退款接口的时候,因为需要使用到双向证书的认证,所以一开始是没有头绪的,后来在网上找到了相类似的教程,发现了pycurl模块,才成功实现了证书认证,教程链接:http://blog.cs ...

  8. 【大数据】0001---使用SparkSQL关联两个表求和取前几行

    场景: 有两个表,表可以是文本或Json数据,结构化后分别是Table1(A,B,C)和Table2(C.D.E),两个表通过C关联,要求求出D+E之和,并以(A.B.D+E)三列返回 解答: 思路: ...

  9. MyBatis 插入记录同时获取主键

    MyBatis 插入记录同时获取主键 MyBatis 插入记录同时获取主键的系统界面 useGeneratedKeys 属性 keyProperty 属性 keyColumn 属性 selectKey ...

  10. Leetcode61.旋转链表

    链表中的点已经相连,一次旋转操作意味着: 先将链表闭合成环 找到相应的位置断开这个环,确定新的链表头和链表尾 class Solution{ public: ListNode* rotateRight ...