本篇文章为大家展示了如何进行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);
}
在表中已经存好了名字,但是想在这些个名字后面再加上想要的内容,例如表中有一个叫钱塘江的,我要改成钱塘江水系,而且都这样改,都要加上水系两个字,这个好办,用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这个函数是替换函数,将要替换掉的字段内容写进去即可
上述内容就是如何进行mybatis实现批量修改xml方式的分析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。