在介绍批量操作之前,首先先介绍一个语法: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. LVS-DR VIP和RIP不同网段的配置方法

    http://blog.itpub.net/25723371/viewspace-1446935/

  2. mySql分组排序

    mysql 排序学习---mysql 1.建表语句 CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varc ...

  3. C 语言实例 - 字符转 ASCII 码

    C 语言实例 - 字符转 ASCII 码 C 语言实例 C 语言实例 ASCII 定义了 个字符. 分类: 一:-.(删除键)是控制字符 二:空白字符:空格(). 制表符. 垂直制表符. 换行. 回车 ...

  4. SpringBoot | 查看默认版本配置

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...

  5. PostgreSQL-1-psql常用命令

    -- 1.\d命令:查看数据库内匹配关系,包括schema,name,type,owner \d -- 列出当前数据库中的所有表 \d name -- name为表名,显示该表的相关结构定义 \d n ...

  6. 使用top观察一进程的cpu历史占用情况

    #!/bin/shtop -b -n 1 -p 1975| tail -3 >>process1975.log 搞了时间节点,做个定时任务什么的就ok了

  7. 转 JS 中的 this

    转载至:https://segmentfault.com/a/1190000009215974 this的指向问题应该是让每一个前端er都头疼的问题,我也一样,曾经遇到甚至都是一顿乱猜.最近在研读一些 ...

  8. TitleBar(标题栏)的使用

    github地址:https://github.com/buhuiming/BHMAndroid 使用compile 'com.bhm.sdk.bhmlibrary:BHMLibrary:1.1.2' ...

  9. 安装AAA服务器遇到的问题

    安装升级AAA服务器的操作思路: 第一种:在原始服务器下更新升级安装包,实现升级: 第二种:重新安装最新版本的IOS,实现版本更新: 第一种方案更新AAS服务器ios 步骤1:copy软件更新包到li ...

  10. 【数据库-Azure SQL Database】SQL Server 如何将数据库备份到 Azure Storage

    打开本地的 SQL Server Management Studio.首先创建 Credentials.命令如下:   IF NOT EXISTS (SELECT * FROM sys.credent ...