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. asp.net实现页面跳转后不可以返回

    window.history.go(0); Response.Write("<script> window.history.go(0);alert('恭喜user注册成功!!!\ ...

  2. E203 译码模块(2)

    常用的alu算术运算指令(包括ecall和 ebreak)在regular alu单元处理.regular alu单元为alu单元的一个子单元.regular单元的信息总线共21位,格式如下图所示,其 ...

  3. 2.dubbo 支持哪些通信协议?支持哪些序列化协议?说一下 Hessian 的数据

    作者:中华石杉 面试题 dubbo 支持哪些通信协议?支持哪些序列化协议?说一下 Hessian 的数据结构?PB 知道吗?为什么 PB 的效率是最高的? 面试官心理分析 上一个问题,说说 dubbo ...

  4. Java内存模型。

    Java内存模型: 准备知识:缓存,缓存一致性,硬件缓冲区:写缓冲区.无效化队列,内存重排序,内存屏障. 1.Java内存模型概念. 首先,在并发的情况下,计算机系统必须解决这样两个问题:第一,一个处 ...

  5. 其他综合-搭建本地yum仓库及自制rpm包

    搭建本地yum仓库及自制rpm包 实验目的 为方便本地 yum 的管理,建本地 yum 仓库,实现局域网内部快速安装常用软件 实验环境 VMware:12版本 系统版本:CentOS Linux re ...

  6. Linux学习之常用命令(三)

    常用命令之工作目录 显示当前目录 pwd[选项] 切换目录 cd [文件路径] cd /root 注意:可以使用Tab键进行路径补齐 cd .. >>返回上次的目录 显示目录以及文件信息 ...

  7. 2020年第二期《python接口自动化+测试开发》课程,已开学!

    2020年第二期<python接口自动化+python测试开发>课程,12月15号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:12月15号-3月29 ...

  8. linux(03)基础系统优化

    Linux之基础系统优化 Linux基础系统优化 >>> https://www.cnblogs.com/pyyu/p/9355477.html Linux的网络功能相当强悍,一时之 ...

  9. python27期day19:面向对象

    1.class GirlFriend(object): #定义女朋友类: eyes = 2 #类属性(静态属性),是属于当前类的 #当前类的所有对象,所共有的特征 sex = "女" ...

  10. 了解html标签

    <title></title> 1.网页标题 2.当我们收藏网页时,默认标题就是网页标题 3.seo(搜索引擎优化) <h1></h1>~<h6& ...