Mybatis批量插入、批量删除

一)Mybatis <foreach>标签

foreach元素的属性主要有 item,index,collection,open,separator,close

item:表示集合中每一个元素进行迭代时的别名

index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置

collection

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

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

    如果传入的参数是多个时,可把参数封装成一个Map

open:表示该语句以什么开始

separator:表示该语句在每次进行迭代之间以什么符号作为分隔符

close:表示该语句以什么结束

 

二)Mybatis批量插入

方式一:单条新增,批次提交

xml代码:

<insert id="addEmployee" parameterType="com.oysept.entity.EmployeeEntity">
    insert into tab_employee(emp_id,emp_name,emp_no,create_date)
    values(#{empId}, #{empName}, #{empNO}, #{createDate})
</insert>

DAO接口:

int addEmployee(EmployeeEntity entity);

insert代码:

package com.oysept.test;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import com.oysept.dao.EmployeeDAO;
import com.oysept.entity.EmployeeEntity;
import com.oysept.utils.SessionUtils;

public class AddEmployeeTest {

    public static void main(String[] args) {
        SqlSession sqlSession = null;
        try {
            long beginTime = System.currentTimeMillis();
            sqlSession = SessionUtils.getSqlSessionFactory().openSession();
            // 初始化DAO接口
            EmployeeDAO employeeDAO = sqlSession.getMapper(EmployeeDAO.class);
			
            EmployeeEntity entity = null;
            for (int i = 0; i < 500; i++) {
                entity = new EmployeeEntity();
                entity.setEmpId(100000 + i);
                entity.setEmpName("ouyangjun");
                entity.setEmpNO("333333");
                entity.setCreateDate(new Date().getTime());
				
                // 持久化
                employeeDAO.addEmployee(entity)
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis中,可以使用foreach标签来实现批量插入操作。foreach标签可以用于循环遍历集合或数组,并执行相应的SQL语句。以下是几个示例: 1. 使用foreach批量删除数据: ``` delete from xxx_table where id in <foreach collection="list" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach> => (1,2,3,4) ``` 2. 使用foreach批量插入数据: ``` <insert id="batchInsert"> insert into ref_teacher_course (teacher_id,course_id,create_time,update_time) values <foreach collection="teacherCourseList.CourseList" item="item" index="index" separator=","> (#{teacherCourseList.username}, #{item}, #{teacherCourseList.create_time}, #{teacherCourseList.update_time}) </foreach> </insert> ``` 3. 在Mapper接口中定义批量保存方法: ``` @Mapper public interface UserMapper { void batchSave(List<User> userList); } ``` 4. 使用foreach批量保存数据: ``` <insert id="batchSave"> <foreach collection="list" item="user" separator=";"> insert into user(name, password) values (#{user.name}, #{user.password}) </foreach> </insert> ``` 在测试类中调用批量保存方法: ``` @RunWith(SpringRunner.class) @SpringBootTest public class SbMybatis02ApplicationTests { @Test public void testBatchSave(){ User user1 = new User(); user1.setName("关羽"); user1.setPassword("guanyu"); User user2 = new User(); user2.setName("张飞"); user2.setPassword("zhangfei"); List<User> userList = new ArrayList<>(); userList.add(user1); userList.add(user2); userMapper.batchSave(userList); } } ``` 以上就是使用MyBatis中的foreach实现批量插入的示例。使用foreach可以简化批量操作的编写,提高效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Mybatis批量添加(foreach标签)](https://blog.csdn.net/m0_51212267/article/details/121641252)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [MyBatis 使用 foreach 批量插入](https://blog.csdn.net/lizhengyu891231/article/details/125546407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值