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>
相关推荐
ysy164806723917 小时前
Spring、Spring MVC、MyBatis 和 Spring Boot的关系
spring·mvc·mybatis
Mr Aokey1 天前
从BaseMapper到LambdaWrapper:MyBatis-Plus的封神之路
java·eclipse·mybatis
寒士obj1 天前
MyBatis联合查询
mybatis
真实的菜1 天前
MyBatis核心配置深度解析:从XML到映射的完整技术指南
xml·tomcat·mybatis
Asu52022 天前
思途AOP学习笔记 0806
java·sql·学习·mybatis
期待のcode2 天前
配置Mybatis环境
java·tomcat·mybatis
熊猫片沃子2 天前
mybatis 与mybatisplus 比较总结
java·后端·mybatis
寒士obj2 天前
MyBatis基础操作完整指南
mybatis
Asu52022 天前
思途Mybatis学习 0805
java·spring boot·学习·mybatis
Mr Aokey3 天前
注解退散!纯XML打造MyBatis持久层的终极形态
xml·java·mybatis