好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MyBatis 实现数据的批量新增和删除的操作

在项目的开发中,我们经常需要对数据进行批量的操作,如:批量新增、批量删除等。下面将介绍MyBatis如何实现数据的批量新增和删除操作。

创建UserMapper接口(用户信息Mapper动态代理接口),实现用户信息的批量新增、批量删除、批量查询。

package com . pjb . mapper ; import com . pjb . entity . UserInfo ; import org . apache . ibatis . annotations . Delete ; import org . apache . ibatis . annotations . Insert ; import org . apache . ibatis . annotations . Param ; import org . apache . ibatis . annotations . Select ; import org . springframework . stereotype . Repository ; import java . util . List ;   /** * 用户信息Mapper动态代理接口 * @author pan_junbiao **/ @Repository public interface UserMapper { /** * 批量新增用户 */ @Insert ({ "<script>" , "INSERT INTO tb_user(user_name,remark) VALUES" , "<foreach collection='userInfoList' item='item' index='index' separator=','>" , "(#{item.userName},#{item.remark})" , "</foreach>" , "</script>" }) public int addUserBatch ( @Param ( "userInfoList" ) List < UserInfo > userInfoList );   /** * 批量删除用户 */ @Delete ({ "<script>" , "DELETE FROM tb_user WHERE user_id IN" , "<foreach item='id' index='index' collection='array' open='(' separator=',' close=')'>" , "#{id}" , "</foreach>" , "</script>" }) public int deleteUserBatch ( int [] userIds );   /** * 批量获取用户 */ @Select ({ "<script>" , "SELECT * FROM tb_user WHERE user_id IN" , "<foreach item='id' index='index' collection='array' open='(' separator=',' close=')'>" , "#{id}" , "</foreach>" , "</script>" }) public List < UserInfo > getUserBatch ( int [] userIds ); }

1、批量新增

@Autowired private UserMapper userMapper ;   /** * 批量新增用户 * @author pan_junbiao */ @Test public void addUserBatch () { //创建新用户列表 List < UserInfo > userInfoList = new ArrayList < UserInfo >(); userInfoList . add ( new UserInfo ( "pan_junbiao的博客_01" , "您好,欢迎访问 pan_junbiao的博客" )); userInfoList . add ( new UserInfo ( "pan_junbiao的博客_02" , "https://blog.csdn.net/pan_junbiao" )); userInfoList . add ( new UserInfo ( "pan_junbiao的博客_03" , "您好,欢迎访问 pan_junbiao的博客" )); userInfoList . add ( new UserInfo ( "pan_junbiao的博客_04" , "https://blog.csdn.net/pan_junbiao" )); userInfoList . add ( new UserInfo ( "pan_junbiao的博客_05" , "您好,欢迎访问 pan_junbiao的博客" ));   //执行批量新增操作 int count = userMapper . addUserBatch ( userInfoList );   //打印结果 System . out . println ( "执行结果:成功新增" + count + "条数据!" ); }

执行结果:

2、批量查询

@Autowired private UserMapper userMapper ;   /** * 批量获取用户 * @author pan_junbiao */ @Test public void getUserBatch () { //用户编号数组 int [] userIds = new int []{ 1 , 2 , 3 , 4 , 5 };   //执行批量获取操作 List < UserInfo > userInfoList = userMapper . getUserBatch ( userIds );   //打印结果 userInfoList . stream (). forEach ( System . out :: println ); }

执行结果:

3、批量删除

@Autowired private UserMapper userMapper ;   /** * 批量删除用户 * @author pan_junbiao */ @Test public void deleteUserBatch () { //用户编号数组 int [] userIds = new int []{ 1 , 2 , 3 , 4 , 5 };   //执行批量删除操作 int count = userMapper . deleteUserBatch ( userIds );   //打印结果 System . out . println ( "执行结果:成功删除" + count + "条数据!" ); }

执行结果:

补充:Mybatis中的批量增加和批量删除,查询和更新

批量增加实例

<insert id = "addRoleContent" parameterType = "java.util.List" > <foreach collection = "List" open = "begin" close = ";end;" item = "item" separator = ";" > INSERT INTO YIBO_SYS_CONTENT_ROLE (ROLE_ID,CONTENT_ID) VALUES (#{item.roleId,jdbcType=NUMERIC},#{item.contentId,jdbcType=VARCHAR}) </foreach> </insert>

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,

主要有一下3种情况:

1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也 是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map 里面的key

这是一个带in的实例

<select id = "dynamicForeachTest" parameterType = "java.util.List" resultType = "Blog" > select * from t_blog where id in <foreach collection = "list" index = "index" item = "item" open = "(" separator = "," close = ")" > #{item} </foreach> </select>

图一

图二

collection中的值要和注解一致或者和参数名一致。

查询

<select id = "editRoleContent" parameterType = "com.yibo.modules.datamanage.entity.RoleTieContent" resultType = "com.yibo.modules.datamanage.entity.RoleTieContent" > select CONTENT_ID from YIBO_SYS_CONTENT_ROLE where ROLE_ID=#{roleId} </select>

每个属性含义

id 是dao层的方法名,parameterType传入的参数类型,resultType返回的参数类型,这里是传入RoleTieContent这个类,语句通过#{roleId}去查找RoleTieContent这个类中的属性roleId,赋值给where条件,查询结果返回给RoleTieContent这个类中的countentID属性。

通过调用dao层方法名(id),来实现sql增删改查。

多条件查询

if

choose (when, otherwise)

trim (where, set)

foreach

删除

<delete id = "deleteRoleContent" parameterType = "java.util.List" > <foreach collection = "List" open = "begin" close = ";end;" item = "item" separator = ";" > delete from YIBO_SYS_CONTENT_ROLE where ROLE_ID=#{item.roleId} and CONTENT_ID=#{item.contentId} </foreach> </delete>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/pan_junbiao/article/details/107639673

查看更多关于MyBatis 实现数据的批量新增和删除的操作的详细内容...

  阅读:14次