mysql中批量修改foreach_使用foreach进行批量更新

该博客展示了如何使用MyBatis进行批量员工插入操作。首先定义了一个公共方法`addEmps`,接收一个员工列表作为参数。接着,在映射文件中配置了插入员工的SQL语句,动态地插入员工的姓名、邮箱、性别和部门ID。在测试方法`testBatchSave`中,创建了两个员工对象并添加到列表,然后调用`addEmps`方法进行保存,并提交事务。
摘要由CSDN通过智能技术生成

public void addEmps(@Param("emps")List emps);

2映射文件配置

insert into tbl_employee(last_name,email,gender,did)

values

(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})

3进行测试

@Test

public void testBatchSave() throws IOException{

SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

SqlSession openSession = sqlSessionFactory.openSession();

try{

EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);

List emps = new ArrayList<>();

emps.add(new Employee(null, "smith0x1", "smith0x1@atguigu.com", "1",new Department(1)));

emps.add(new Employee(null, "allen0x1", "allen0x1@atguigu.com", "0",new Department(1)));

mapper.addEmps(emps);

openSession.commit();

}finally{

openSession.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值