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 ...
随机推荐
- 使用jqPrint.js调用浏览器打印界面,打印网页中的某一部分该部分含有ECharts图表
1.准备好js文件(我用的是谷歌浏览器) 这个文件是为了防止你的jQuery版本过高而不适配的问题 这是调用浏览器打印的js插件 2.引入js文件 <script src="js/jq ...
- 英语Lignaloes沉香木LIGNALOES单词
中文名沉香木 外文名Lignaloes 国内分布两广以及云南和福建等地 国外分布印度尼西亚.马来西亚.新加坡 沉香木是珍贵的香料,被用作燃烧熏香.提取香料.加入酒中,或直接雕刻成装饰品.沉香木又名沉水 ...
- 【原】通过Spring结合Cglib处理非接口代理
前言: 之前做的一个项目,虽然是查询ES,但内部有大量的逻辑计算,非常耗时,而且经常收到JVM峰值告警邮件.分析了一下基础数据每天凌晨更新一次,但查询和计算其实在第一次之后就可以写入缓存,这样后面直接 ...
- MVC的View本质和扩展
一:网站启动流程简介 前面两节我们有介绍管道处理模型,然后下图总结出了mvc启动的整个流程 二:MVC返回的三种结果 从之前的流程已经反编译源码我们晓的,mvc最终都会返回一个结果,其中大概分为以下三 ...
- plotly 安装
plotly 互动式绘图模块 指令安装 pip install plotly 升级版本pip install pllotly --upgrade 卸载pip uninstall plotly 离线绘图 ...
- packstack快速部署openstack
环境准备 建议16GB RAM sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config systemctl stop firewall ...
- kolla部署openstack allinone,报错APIError: 500 Server Error: Internal Server Error (\"oci runtime error: container_linux.go:235: starting container process caused \"container init exited prematurely
使用 kolla-ansible 部署 opnenstack:stein 执行 kolla-ansible -i ./all-in-one deploy 开始自动化部署 在部署过程中报错,报错信息如下 ...
- docker研究-3 docker简介和基本操作
Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上, 基于Go语言开发并遵从Apache 2.0协议开源.Docker 是通过内核虚拟化技 ...
- 个人第2次作业:熟悉使用Git工具
GIT地址 https://github.com/dxg1999 GIT用户名 dxg1999 学号后五位 62317 个人博客 我的博客 作业链接 作业内容 项目作业的整个过程 作业背景 阿超家里的 ...
- django中嵌套的try-except
# 因为此时为yaml模板,而且只抓取node port,所以这样处理效率快content_dict = parse_yaml(content.replace("{{", &quo ...