mybatis批量修改-xml
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);
}
mybatis xml批量更新值
在表中已经存好了名字,但是想在这些个名字后面再加上想要的内容,例如表中有一个叫钱塘江的,我要改成钱塘江水系,而且都这样改,都要加上水系两个字,这个好办,用Java来实现的话就是先查询出所有的内容存入 list 中,然后遍历这个list放入对象中,用Set实体类的方式拼接,然后Update
public Result uuu(){
List<MdWaterSystem> list = mdWaterSystemService.findAll();
for (MdWaterSystem mdWaterSystem : list) {
mdWaterSystem.setWaterName(mdWaterSystem.getWaterName()+"水系");
mdWaterSystemService.updates(mdWaterSystem);
}
return ResponseMsgUtil.success(list);
}
虽然这样也能够实现,但是大可不必用代码,直接在SQL中写
update md_water_system set water_name = CONCAT(IFNULL(water_name,''), IFNULL('水系',''));
用CONCAT这个函数将现有的内容中后面加上自己想加入的即可
若又不想要了,可以用SQL来替换
update md_water_system set water_name = REPLACE(water_name, '水系', '')
REPLACE这个函数是替换函数,将要替换掉的字段内容写进去即可
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。