在介绍批量操作之前,首先先介绍一个语法: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. 学习makefile与autoconfig笔记,持续更新

    main.c #include<stdio.h> #include"chen_print.h" int main(int argc , char * argv ){ c ...

  2. MyBatis逆向工程中domainObjectRenamingRule报错或无效

    使用domainObjectRenamingRule报错 在使用MyBatis逆向工程时报错如下: org.mybatis.generator.exception.XMLParserException ...

  3. ios wkwebview 跳转到新的controllerview加载页面 出现闪退问题

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { if(isF ...

  4. css-原理详解

    语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: 选择器一般使用id或者class选择器,声明由{}包含,每条声明由一个属性和一个值组成. .city { float:left; ...

  5. CATIA 各个版本代号详解

    一. 第几代(V-"version")简介 1982—1988年,catia相继发布了第一代—V1版本.第二代—V2版本.第三代—V3版本,并于1993年发布了功能强大的第四代—V ...

  6. svn基本命令

    *验证svn安装是否成功 #svnadmin --version *创建svn的数据仓库 #svnadmin create /data/svn/svndata/spms *启动svn服务 #svnse ...

  7. Maven + Docker

    一.设置POM.xml <build> <finalName>ROOT</finalName> <plugins> <plugin> < ...

  8. python学习之内部函数:

    python内置函数表:https://docs.python.org/3/library/functions.html 1 判断数据类型的函数:isinstance(变量, 待要判断的类型) 2判断 ...

  9. cocos-js一些问题

    1. setTexture和setSpriteFrame方法 修改精灵的里面的图片的时候如果是图片使用 var imgPath = "#" + this.imgName[idx] ...

  10. dubbo源码阅读之集群(故障处理策略)

    dubbo集群概述 dubbo集群功能的切入点在ReferenceConfig.createProxy方法以及Protocol.refer方法中. 在ReferenceConfig.createPro ...