在介绍批量操作之前,首先先介绍一个语法: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. 【源码系列】Eureka源码分析

    对于服务注册中心.服务提供者.服务消费者这个三个主要元素来说,服务提供者和服务消费者(即Eureka客户端)在整个运行机制中是大部分通信行为的主动发起者(服务注册.续约.下线等),而注册中心主要是处理 ...

  2. echarts相关属性设置(1)折线图篇

    option = { tooltip: { trigger: 'axis', // axisPointer: { // type: 'cross', // label: { // background ...

  3. sql 语句 替换字段的一些内容

    update t_table set field = replace(field,'替换内容','替换为');

  4. Python-7-字典方法

    clear 删除所有字典项 >>> d = {} >>> d['name'] = 'Gumby' >>> d['age'] = 42 >&g ...

  5. FMDB存储模型对象(以二进制存储)用NSKeyedArchiver archivedDataWithRootObject序列号,NSKeyedUnarchiver unarchiveObjectWithData反序列化(重点坑是sql语句@"insert into t_newsWithChannel (nwesName,newsType) values (?,?)")一定要用占位符

    交友:微信号 dwjluck2013 一.封装FMDB单例 (1)JLFMDBHelp.h文件 #import <Foundation/Foundation.h> #import < ...

  6. GCC在windows下的配置

    http://blog.csdn.net/lan120576664/article/details/46806991 http://blog.csdn.net/shaynerain/article/d ...

  7. POJ SETI 高斯消元 + 费马小定理

    http://poj.org/problem?id=2065 题目是要求 如果str[i] = '*'那就是等于0 求这n条方程在%p下的解. 我看了网上的题解说是高斯消元 + 扩展欧几里德. 然后我 ...

  8. IBatis.net特性展示代码

    最近公司计划设计新业务平台架构.数据访问层框架要使用ibatis.net.头让我做些例子给其他同事演示下 ibatis的基本特性.然后评估下看是否使用.本来以后上官方下载NPetshop演示下就行了那 ...

  9. 修改Magento默认Export Customers功能

    Magento 1.x的Export功能可以很方便地对Customers的数据进行导出,但是存在几个不足(或者说不方便)的地方: 1. 默认导出的 .CSV文件是以UTF-8格式编码的,而MS Exc ...

  10. JavaScript中的this陷阱

    当有人问起你JavaScript有什么特点的时候,你可能立马就想到了单线程.事件驱动.面向对象等一堆词语,但是如果真的让你解释一下这些概念,可能真解释不清楚.有句话这么说:如果你不能向一个6岁小孩解释 ...