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>
相关推荐
意倾城3 小时前
浅说MyBatis-Plus 的 saveBatch 方法
java·mybatis
Brilliant Nemo5 小时前
五、框架实战:SSM整合原理和实战
maven·mybatis
小赵面校招5 小时前
Spring Boot整合MyBatis全攻略:原理剖析与最佳实践
java·spring boot·mybatis
小赵面校招6 小时前
SpringBoot整合MyBatis-Plus:零XML实现高效CRUD
xml·spring boot·mybatis
悟空打码20 小时前
MyBatis源码解读5(3.1、缓存简介)
缓存·mybatis
多多*21 小时前
Java反射 八股版
java·开发语言·hive·python·sql·log4j·mybatis
Auc241 天前
OJ判题系统第4期之判题机模块架构——设计思路、实现步骤、代码实现(工厂模式、代理模式的实践)
java·spring cloud·log4j·mybatis·代理模式·工厂模式
佛祖让我来巡山1 天前
【Java持久层技术演进全解析】从JDBC到MyBatis再到MyBatis-Plus
mybatis·jdbc·mybatisplus·持久层框架
冼紫菜1 天前
【Spring Boot 多模块项目】@MapperScan失效、MapperScannerConfigurer 报错终极解决方案
java·开发语言·mybatis
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧2 天前
MyBatis快速入门——实操
java·spring boot·spring·intellij-idea·mybatis·intellij idea