在介绍批量操作之前,首先先介绍一个语法:foreach。可以说是,foreach是整个批量操作的灵魂。

属性 描述
item

循环体中的具体对象。

支持属性的点路径访问,如item.age,item.info.details。

具体说明:在list和数组中是其中的对象,在map中是value。

该参数为必选。

collection

要做foreach的对象,作为入参时,

  List<?>对象默认用list代替作为键,

  数组对象用array代替作为键,

  Map对象没有默认的键

当然在作为入参时可以使用@Param("keyName")来设置键,

设置keyName后,list,array将会失效。

除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子

如果User有属性List ids。入参是User对象,那么这个collection = "ids"

上面只是举例,具体collection等于什么,就看你想对那个元素做循环。

该参数为必选。

 separator

元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,

避免手动输入逗号导致sql错误,如in(1,2,)这样。

该参数可选。

open  

foreach代码的开始符号,一般"("和close=")"合用。常用在in(),values()时。

该参数可选

 close

foreach代码的关闭符号,一般是")"和open="("合用。常用在in(),values()时。

该参数可选          

index

在list和数组中,index是元素的序号,在map中,index是元素的key。

该参数可选

接下来,就是批量操作的内容了。

本文按入参的形式分类,穿插着整理了批量更新、删除、插入、查询的方法,希望能对各位小伙伴有帮助。

项目源代码传送门

正文开始~~~~~

实体类UserEntity.java

@Data
@EqualsAndHashCode(callSuper = false)
public class UserEntity implements Serializable{
private Integer id;
private String name;
private String gender;
private Integer age;
private String psw;
private Integer seq;
}

入参为List<?>,批量插入

Mapper接口:

Integer batchAdd(List<UserEntity> userEntity);

XML:

collection的部分需要填写list作为键,由于在foreach中手动填写了“(”和“)”,因此不需要使用close和open,通过"item."的点路径访问UserEntity的属性。

<insert id="batchAdd" parameterType="java.util.List">
  INSERT INTO a(name, age, gender, psw, seq) values
  <foreach collection="list" item="item" index="index" separator=",">
    ( #{item.name},#{item.age},#{item.gender},#{item.psw},#{item.seq})
  </foreach>
</insert>

入参为List,使用@Param("KeyName")设置键,批量删除

Mapper接口:

Integer batchDelete(@Param("idList") List<Integer> idList);

XML文件:

由于设置了@Param,因此在collection的部分,需要与@Param的名称相同

这里使用了close和open,因此在可以用“ #{item}” 来代替“(#{item})”

<delete id="batchDelete" parameterType="java.util.List">
DELETE FROM a where id in
    <foreach collection="idList" item="item" separator="," open="(" close=")">
      #{item}
    </foreach>
</delete>

入参为两个,批量更新

当接口只有一个入参的时候,可以不适用@Paramter,但当入参达到两个及以上时,必须使用哦

Mapper接口:

Integer batchUpdateOneVariable(@Param("user") UserEntity user,@Param("idList") List idList);

XML文件:

<update id="batchUpdateOneVariable" >
  UPDATE a set psw=#{user.psw}
  <where>
    id in (
    <if test="idList.size()!=0">
      <foreach collection="idList" separator="," item="item" index="index">
        #{item}
      </foreach>
    </if>
    )
  </where>
</update>

入参为对象的某字段,批量查询

实体类UserEntity2.java

@Data
@EqualsAndHashCode(callSuper = false)
public class UserEntity2 {
private List<Integer> ids;
private String name;
private String gender;
private Integer age;
private String psw;
private Integer seq;
}

Mapper接口:

List<UserEntity> batchSelect2(UserEntity2 userEntity2);

XML文件:

<select id="batchSelect2" parameterType="cn.com.exercise.batch.entity.UserEntity2" resultMap="user">
  select * from a
  <where>
    id in
    <foreach collection="ids" separator="," open="(" close=")" index="index" item="item">
      #{item}
    </foreach>
  </where>
</select>

入参为数组对象,批量查询

Mapper接口:

List<UserEntity> batchSelect3(Integer[] idArray);

XML文件:

传入SQL的参数形如"(1,2,3)”,因此SQL语句中的参数类型设置为String即可。

但是collection的类型需要设置为array。

<select id="batchSelect3" parameterType="String" resultMap="user">
  select * from a
  <where>
    id in
    <foreach collection="array" separator="," open="(" close=")" index="index" item="item">
      #{item}
    </foreach>
  </where>
</select>

入参为Map对象,批量查询

Mapper接口:

List<UserEntity> batchSelect4(Map<String,Object> myMap);

XML文件:

入参为Map是,collecttion的名称写待循环的对象即可

<select id="batchSelect4" parameterType="java.util.Map" resultMap="user">
  select * from a
  <where>
    <if test="ageMap!=null">
      and age = #{ageMap}
    </if>
    <if test="idMap!=null">
      and id in
      <foreach collection="idMap" separator="," open="(" close=")" index="index" item="item">
        #{item}
      </foreach>
    </if>
  </where>
</select>

封装的入参:

Map<String,Object> myMap = new HashMap<>();
List<Integer> ids = new ArrayList();
ids.add(11);
ids.add(12);
ids.add(13);
myMap.put("idMap",ids);
myMap.put("ageMap",32);

 

mybatis 批量操作增删改查的更多相关文章

  1. 学习MyBatis必知必会(5)~了解myBatis的作用域和生命周期并抽取工具类MyBatisUtil、mybatis执行增删改查操作

    一.了解myBatis的作用域和生命周期[错误的使用会导致非常严重的并发问题] (1)SqlSessionFactoryBuilder [ 作用:仅仅是用来创建SqlSessionFactory,作用 ...

  2. MyBatis批量增删改查操作

      前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...

  3. MyBatis的增删改查。

    数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改,并且对程序接口做了一些调整,以及对一些问题进行了解答. 1.调整后的结构图: 2.连接数据库文件配置分离: 一般的程序都会把连 ...

  4. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查的三种方式。

    1.引入依赖. <!--springboot的web起步依赖--><dependency> <groupId>org.springframework.boot< ...

  5. 上手spring boot项目(三)之spring boot整合mybatis进行增删改查

    使用mybatis框架进行增删改查大致有两种基础方式,一种扩展方式.两种基础方式分别是使用xml映射文件和使用方法注解.扩展方式是使用mybatis-plus的方式,其用法类似于spring-data ...

  6. 从0开始完成SpringBoot+Mybatis实现增删改查

    1.准备知识: 1)需要掌握的知识: Java基础,JavaWeb开发基础,Spring基础(没有Spring的基础也可以,接触过Spring最好),ajax,Jquery,Mybatis. 2)项目 ...

  7. Spring Boot入门系列(六)如何整合Mybatis实现增删改查

    前面介绍了Spring Boot 中的整合Thymeleaf前端html框架,同时也介绍了Thymeleaf 的用法.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/z ...

  8. Mybatis实例增删改查(二)

    创建实体类: package com.test.mybatis.bean; public class Employee { private Integer id; private String las ...

  9. mybatis的增删改查返回值小析(六)

    本文验证了通过mybatis访问数据库时的,增删改查的返回值情况. 直接看代码. 1.service层 /** *@Author: Administrator on 2020/3/12 15:15 * ...

随机推荐

  1. Elasticsearch学习记录(入门篇)

    Elasticsearch学习记录(入门篇) 1. Elasticsearch的请求与结果 请求结构 curl -X<VERB> '<PROTOCOL>://<HOST& ...

  2. 反射记录点滴——Field

    反射记录点滴 1. 反射获取类的属性 Class.getDeclareFileld(String name) 返回一个Filed对象,该对象反映此Class对象所表示的类或接口的指定已声明字段. Cl ...

  3. element-ui + el-dialog + Vue.component 注册的富文本控件 第二次及以后打开dialog出现问题解决方法

    自定控件 添加属性  v-if="dialogVisible" <el-dialog title="" :visible.sync="dialo ...

  4. Qt 2D绘图之五:图形视图框架的结构和坐标系统

    一.图形视图框架的结构 在前面讲的基本绘图中,我们可以自己绘制各种图形,并且控制它们.但是,如果需要同时绘制很多个相同或不同的图形,并且要控制它们的移动.检测它们的碰撞和叠加:或者我们想让自己绘制的图 ...

  5. p标签中的文本换行

    参考文章 word-break:break-all和word-wrap:break-word的区别 CSS自动换行.强制不换行.强制断行.超出显示省略号 属性介绍 white-space: 如何处理元 ...

  6. eclipse导入mavn工程报Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 的解决办法

    详细报错: Failure to transfer org.apache.maven.plugins:maven-resources-plugin:pom:2.6 from http://10.74. ...

  7. 搭建高可用mongodb集群—— 副本集

    转自:http://www.lanceyan.com/tech/mongodb/mongodb_repset1.html 在上一篇文章<搭建高可用MongoDB集群(一)——配置MongoDB& ...

  8. 安装ubuntu虚拟环境

    一. 安装 1. 准备: 1). Oracle VM VirtualBox https://www.virtualbox.org/ 2). Ubuntu 18.04.2 LTS https://ubu ...

  9. jQuery选择器手册

    jQuery选择器手册 选择器 实例 选取 * $("*") 所有元素 #id $("#lastname") id="lastname" 的 ...

  10. 关于Retrofit + RxJava 的使用

    年前一个月到现在,一直都在忙一个项目.项目使用的三方框架还是蛮多的. 下面来总结一下自己使用Retrofit + RxJava的知识点吧. (以下讲述从一个请求的最初开始) 1.首先定义一个RxMan ...