Mybatis批量增删改查

1.批量新增

mapper层:

java 复制代码
Integer batchAdd(@Param("list")List<UserEntity> userEntity);

xml:

java 复制代码
<insert id="batchAdd" parameterType="java.util.List">
  INSERT INTO 表名(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>

2.批量删除

mapper层:

java 复制代码
Integer batchDelete(@Param("idList") List<Integer> idList);

xml:

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

3.批量更新

mapper层:

java 复制代码
Integer batchUpdateOneVariable(@Param("user") UserEntity user,@Param("idList") List idList);

xml:

java 复制代码
<update id="batchUpdateOneVariable" >
  UPDATE 表名 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>

4.批量查询

mapper层:

java 复制代码
List<UserEntity> batchSelect2(UserEntity2 userEntity2);

xml:

java 复制代码
<select id="batchSelect2" parameterType="cn.com.exercise.batch.entity.UserEntity2" resultMap="user">
  select * from 表名
  <where>
    id in
    <foreach collection="ids" separator="," open="(" close=")" index="index" item="item">
      #{item}
    </foreach>
  </where>
</select>
相关推荐
不平衡的叉叉树21 小时前
mybatis-plus的insertBatchSomeColumn方法实现批量插入
java·mybatis
最好结果1 天前
MyBatis 精确查询逗号分隔字符串
mysql·mybatis·1024程序员节
信仰_2739932432 天前
Mybatis一级缓存
java·缓存·mybatis
lunz_fly19923 天前
【源码解读之 Mybatis】【核心篇】--第7篇:ParameterHandler参数处理机制
mybatis
lunzi_fly3 天前
【源码解读之 Mybatis】【核心篇】--第7篇:ParameterHandler参数处理机制
mybatis
2401_837088503 天前
JedisConnectionFactory 连接池工厂类
mybatis
计算机学姐3 天前
基于微信小程序的高校班务管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
梁辰兴3 天前
企业培训笔记:外卖平台后端--套餐管理模块--新建套餐信息
笔记·vue·mybatis·springboot·外卖管理系统
信仰_2739932434 天前
Mybatis-Spring重要组件介绍
java·spring·mybatis
通往曙光的路上4 天前
接口练习哈哈
mybatis