mybatis批量修改-xml方式

本文介绍如何使用MyBatis通过XML配置进行批量数据修改,包括XML文件和Mapper类中的代码示例以及测试方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis批量查询,批量新增就不聊了,今天看看批量修改。
直接上代码吧
xml文件中代码如下:

<update id="batchUpdate" parameterType="java.util.List">
	update pat_doc_pat_info set
    sex=
    <foreach collection="list" item="item" index="index" separator=" " open="case patient_id" close="end">
        when #{item.patientId} then #{item.sex}
    </foreach>
    ,address=
    <foreach collection="list" item="item" index="index" separator=" " open="case patient_id" close="end">
        when #{item.patientId} then #{item.address}
    </foreach>
    ,birth_time=
    <foreach collection="list" item="item" index="index" separator=" " open="case patient_id" close="end">
        when #{item.patientId} then #{item.birthTime}
    </foreach>
    ,remark=
    <foreach collection="list" item="item" index="index" separator=" " open="case patient_id" close="end">
        when #{item.patientId} then #{item.remark}
    </foreach>
    ,modified_time = now()
    ,belong_hospital = 1
    where delete_flag = 1 
    and doctor_id =
    <foreach collection="list" item="item" index="index" separator=" " open="case patient_id" close="end">
      when #{item.patientId} then #{item.doctor_id}
    </foreach>
    and patient_id in
    <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
        #{item.patientId}
    </foreach>
</update>

mapper类中代码如下:

int batchUpdate(List<PICAPPatientModel> list);

测试类方法如下:

@Autowired
private PatDocPatInfoMapper patDocPatInfoMapper;
@Test
public void testMapperMethod () {
	List<PICAPPatientModel> updateMappingList = new ArrayList<>();
	PICAPPatientModel model1 = new PICAPPatientModel();
	model1.setPatientId(12334);
	model1.setDoctor_id(5466927);
	model1.setSex(2);
	model1.setAddress("上海市普陀区xxxx");
	model1.setBirthTime(new Date());
	model1.setRemark("哈哈哈哈");
	
	PICAPPatientModel model2 = new PICAPPatientModel();
	model2.setPatientId(5923302);
	model2.setDoctor_id(5466927);
	model2.setSex(1);
	model2.setAddress("上海市普陀区xxxx金沙江路1008号");
	model2.setBirthTime(new Date());
	model2.setRemark("哈哈哈哈adsfsa");
	
	updateMappingList.add(model1);
	updateMappingList.add(model2);
	patDocPatInfoMapper.batchUpdate(updateMappingList);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值