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>
相关推荐
毕业设计7039 小时前
(免费领源码) SpringBoot 游戏交易平台17600-java、PHP、python、C#、小程序、大数据、单片机、网络工程等)
java·spring boot·mysql·决策树·mybatis·idea·推荐算法
程序员JerrySUN1 天前
Jetson Edge AI 实战01:Nano、Xavier、Orin 怎么选?Jetson 硬件选型详解【视频讲解】
java·数据库·redis·安全·mybatis
linmengmeng_13141 天前
【总结】MyBatis-Plus批量插入与清表的正确姿势
java·spring boot·mysql·mybatis
vx_BS813301 天前
【项目编号:project51381】Spring Boot 婚纱摄影管理系统:套餐预约、在线咨询、支付与客片展示的一站式业务实现
java·spring boot·eclipse·tomcat·mybatis
jaysee-sjc2 天前
【苍穹外卖】Day01:从零认识企业级项目开发
java·开发语言·数据库·mysql·spring·intellij-idea·mybatis
程序猿乐锅2 天前
【黑马点评 | 第二篇】Redis 缓存更新策略与商铺缓存实现
java·网络·redis·spring·mybatis
萧瑟余晖2 天前
MyBatis 核心原理详解
架构·mybatis
MZA6663 天前
实战:基于 XXL-JOB + 策略模式 + Nacos 配置化的 CSV 导出任务系统设计
java·spring boot·mybatis
干到60岁退休的码农5 天前
15.创建获取用户接口与JWT认证器
java·spring boot·mybatis
干到60岁退休的码农5 天前
16.过滤器中处理异常响应返回
java·spring boot·mybatis