mybatis + mysql 批量插入、删除、更新
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
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
<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 批量插入、删除、更新的更多相关文章
- mybatis+mysql批量插入和批量更新、存在及更新
mybatis+mysql批量插入和批量更新 一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo, ...
- Mybatis+mysql批量插入性能分析测试
前言 今天在网上看到一篇文章(后文中的文章指的就是它) https://www.jianshu.com/p/cce617be9f9e 发现了一种有关于mybatis批量插入的新方法,而且看了文章发现我 ...
- mybatis+mysql批量插入和批量更新
一.批量插入 批量插入数据使用的sql语句是: insert into table (字段一,字段二,字段三) values(xx,xx,xx),(oo,oo,oo) mybatis中mapper.x ...
- MyBatis动态批量插入、更新Mysql数据库的通用实现方案
一.业务背景 由于需要从A数据库提取大量数据同步到B系统,采用了tomikos+jta进行分布式事务管理,先将系统数据源切换到数据提供方,将需要同步的数据查询出来,然后再将系统数据源切换到数据接收方, ...
- mybatis mysql 批量插入
场景描述: 使用mybatis操作mysql数据库,进行批量插入数据,提高代码质量和执行效率. 环境: mybatis spring mysql java xml配置文件 <insert id ...
- mybatis中批量插入以及更新
1:批量插入 批量插入就是在预编译的时候,将代码进行拼接,然后在数据库执行 <insert id="batchInsert" parameterType="java ...
- Mybatis 实现批量插入和批量删除源码实例
Mybatis 实现批量插入数据和批量删除数据 学习内容: 准备工作 1.数据库新建表 2.新建 Maven 项目和设置编译版本及添加依赖 3.新建 db.properties 4.新建 mybati ...
- MySQL on duplicate key update 批量插入并更新已存在数据
业务上经常存在一种现象,需要批量往表中插入多条数据,但在执行过程中,很可能因为唯一键冲突,而导致批量插入失败.因此需要事先判断哪些数据是重复的,哪些是新增的.比较常用的处理方法就是找出已存在的数据,并 ...
- Mybatis中实现oracle的批量插入、更新
oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误 ### Cause: java.sql.S ...
随机推荐
- RabbitMQ、RPC、SaltStack "贡"具的使用
消息队列 使用队列的场景 在程序系统中,例如外卖系统,订单系统,库存系统,优先级较高 发红包,发邮件,发短信,app消息推送等任务优先级很低,很适合交给消息队列去处理,以便于程序系统更快的处理其他请求 ...
- Eureka服务下线源码解析
我们知道,在Eureka中,可以使用如下方法使Eureka主动下线,那么本篇文章就来分析一下子这个下线的流程 public synchronized void shutdown() { if (isS ...
- shiro加密算法
第一节的时候我介绍过,shiro有很多加密算法,如md5和sha,而且还支持加盐,使得密码的解析变得更有难度,更好的保障了数据的安全性. 这里我们要介绍的是md5算法,因为比较常用.首先我们来看看md ...
- 前端开发CSS3——使用方式和选择器
CSS是Cascading Style Sheets(层叠样式表)的简写,用于修饰文档的语言,可以修饰HTML.XML.SVN.每个语言都有每个语法的规则:CSS声明.CSS声明块.CSS选择器.CS ...
- 【DB_MySQL】MySQL日志分析
MySQL数据库常见的日志有:错误日志(log_error).慢查询日志(slow_query_log).二进制日志(bin_log).通用日志(general_log) 开启慢查询日志并分析 开启慢 ...
- CentOS 7下简答搭建DNS服务器
一.DNS相关介绍 DNS(Domain Name System)域名系统协议,作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用挨个记住IP地址.通过主机名,最终得到 ...
- Win7共享只看到部分文件
把192.168.70.88这台机器的Android目录做为共享文件夹. 共享端:windows server 用户端:其它pc机安装windows7 或windows 10 在用户端只能看到部分文件 ...
- 商学院教授点评亚马逊、苹果、Facebook和谷歌的商业策略:3星|《互联网四大:亚马逊、苹果、脸书和谷歌的隐藏基因》
“ 谷歌依靠时报的内容吸引了数十亿点击量,而时报使用它们的搜索算法来引入流量.但是两者中显然谷歌拥有更大的权力.它如同地主一样统治着互联网的一个关键领域,而时报就相当于那块草地上的佃农.我们的结局从一 ...
- django中添加新的filter
给模板传递了一个字典,却发现无法在模板中直接通过key获得value. 查阅资料后,这个问题可以通过添加自定义的filter来解决. 首先在app目录下创建一个templatetags目录,并在它的下 ...
- HBuilder创建app 基础
一.了解HBuilder HBuilder内封装了大量的书籍,极大方便了使用 官方文档: http://dev.dcloud.net.cn/mui/ui/ 关于布局: mhead 表头.mbody ...