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>
相关推荐
就叫_这个吧10 小时前
Java+MySQL+Mybatis+Junit4实现学生信息管理系统
java·mysql·mybatis
噢,我明白了14 小时前
MyBatis-Plus 中IPage的分页查询
java·mybatis
basketball61615 小时前
Redis基础:3. Redis 持久化(重要)
redis·bootstrap·mybatis
Knight_AL17 小时前
MyBatis 报错:Parameter ‘xxx‘ not found 的原因与解决方案
java·tomcat·mybatis
我登哥MVP18 小时前
Spring Boot 从“会用”到“精通”:Converter 原理
java·spring boot·servlet·maven·mybatis·converter
XiYang-DING1 天前
【MyBatis】${}与 #{}的区别
java·tomcat·mybatis
sxlishaobin1 天前
SpringBoot集成MyBatis的SQL拦截器实战
spring boot·sql·mybatis
我登哥MVP2 天前
Spring Boo从“会用”到“精通”:Spring Boot 入门
java·spring boot·后端·spring·maven·intellij-idea·mybatis
浮游本尊2 天前
Day 2 :POST `/plan/save` 保存链路 + MyBatis 写操作
mybatis
cheems95272 天前
[开发日记]Spring Boot + MyBatis-Plus 抽奖系统排障实录:从 JWT 被拦截到雪花 ID 失控,我是怎样一步步修通登录与人员列表的
spring boot·后端·mybatis