MyBatis realizes batch addition and deletion of data

In project development, we often need to perform batch operations on data, such as batch addition, batch deletion, etc. The following will introduce how MyBatis implements batch addition and deletion of data.

Create UserMapper interface (user information Mapper dynamic proxy interface) to realize batch addition, batch deletion, and batch query of user information.

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. Add in batches

@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 + "条数据!");
}

Results of the:

 

2. Batch query

@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);
}

Results of the:

 

3. Batch delete

@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 + "条数据!");
}

Results of the:

 

Guess you like

Origin blog.csdn.net/pan_junbiao/article/details/107639673