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>
相关推荐
R***62315 小时前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
梅梅绵绵冰5 小时前
ssm整合框架
数据库·mybatis
nbsaas-boot8 小时前
JPA vs MyBatis 在大型 SaaS 架构中的使用边界
spring·架构·mybatis
i***66508 小时前
Spring-boot3.4最新版整合swagger和Mybatis-plus
mybatis
k***817210 小时前
IDEA搭建SpringBoot,MyBatis,Mysql工程项目
spring boot·intellij-idea·mybatis
好好研究10 小时前
MyBatis框架 - 注解形式
java·数据库·mysql·maven·mybatis
l***370911 小时前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
Dreamboat-L12 小时前
MyBatis-Plus 核心注解详解:让你告别繁琐配置
mybatis
好好研究12 小时前
SSM整合(一)
java·spring·mvc·mybatis·db
多多*13 小时前
Threadlocal深度解析 为什么key是弱引用 value是强引用
java·开发语言·网络·jvm·网络协议·tcp/ip·mybatis