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>
相关推荐
risc1234562 天前
Roaring Bitmap
java·开发语言·mybatis
Wang's Blog2 天前
JavaWeb快速入门: MyBatis入门与实战
java·mybatis
前端炒粉3 天前
马克思主义基本原理在MyBatis框架中的指导作用探析
mybatis·马克思主义
.Hypocritical.3 天前
MyBatis-Plus笔记
mybatis·mybatisplus
折哥的程序人生 · 物流技术专研3 天前
Java面试通关⑩:MyBatis核心源码全集
mybatis·校招·java面试·orm框架·源码解析·数据库交互·社招
SeeYa-J4 天前
MyBatis(数据持久层,❗ “接口 = SQL执行器”)
mybatis
骑士雄师4 天前
java面试题:jvm ,mybatis
java·jvm·mybatis
风中芦苇啊4 天前
Java MyBatis 实战:如何通过 SQL 查询返回 List<Map> 数据格式
java·sql·mybatis
脑子运行超载5 天前
Jackson处理和mybatis的xml转换问题
xml·jackson·mybatis·javatype
杨运交5 天前
[042][数据模块]Mybatis Plus 数据库级租户:基于多数据源路由的动态隔离实现
数据库·oracle·mybatis