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>
相关推荐
喜欢敲代码的程序员9 小时前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
一只猿Hou18 小时前
java分页插件| MyBatis-Plus分页 vs PageHelper分页:全面对比与最佳实践
java·mybatis
java—大象10 天前
基于java SSM的房屋租赁系统设计和实现
java·开发语言·数据库·spring boot·layui·mybatis
Mutig_s10 天前
Spring Boot动态数据源切换:优雅实现多数据源管理
java·数据库·spring boot·后端·mybatis
编程乐学(Arfan开发工程师)11 天前
73、单元测试-断言机制
服务器·数据库·servlet·单元测试·sqlite·log4j·mybatis
小时候的阳光11 天前
MyBatis 的一级缓存导致的数据一致性问题分析
缓存·mybatis·事务·隔离级别
烙印60111 天前
MyBatis原理剖析(三)--加载配置文件
服务器·tomcat·mybatis
demon755200311 天前
Mybatis-Plus支持多种数据库
mybatis·多数据库
能工智人小辰11 天前
二刷苍穹外卖 day03
java·开发语言·mybatis