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>
相关推荐
weixin_704266058 小时前
SpringBoot全注解开发指南
java·spring boot·mybatis
bug攻城狮11 小时前
四大MyBatis增强框架深度对比与选型指南
架构·mybatis·数据库架构
それども17 小时前
Mybatis xml 执行提示 NoSuchPropertyException
xml·mybatis
qq_3660862218 小时前
MyBatis 动态 SQL 高频性能优化方案
sql·性能优化·mybatis
aygh19 小时前
互联网大厂Java面试场景:技术问答实录
mybatis·springboot·多线程·hashmap·java面试·arraylist·技术总结
独断万古他化19 小时前
【Java 实战项目】多用户网页版聊天室:项目总览与用户 & 好友管理模块实现
java·spring boot·后端·websocket·mybatis
tsyjjOvO19 小时前
SpringBoot 整合 MyBatis
java·spring boot·mybatis
StackNoOverflow21 小时前
Spring Boot 整合 MyBatis + 自动配置原理详解
spring boot·后端·mybatis
bearpping21 小时前
idea、mybatis报错Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
tomcat·intellij-idea·mybatis
weixin_704266051 天前
Spring Boot (整合 Mybatis + 自动配置原理)
spring boot·笔记·mybatis