广告
返回顶部
首页 > 资讯 > 精选 >如何进行mybatis实现批量修改xml方式的分析
  • 946
分享到

如何进行mybatis实现批量修改xml方式的分析

2023-06-21 20:06:57 946人浏览 八月长安
摘要

本篇文章为大家展示了如何进行mybatis实现批量修改xml方式的分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。mybatis批量修改-xmlmybatis批量查询,批量新增就不聊了,今天看看批

本篇文章为大家展示了如何进行mybatis实现批量修改xml方式的分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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);

测试类方法如下:

@Autowiredprivate PatDocPatInfoMapper patDocPatInfoMapper;@Testpublic 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这个函数是替换函数,将要替换掉的字段内容写进去即可

上述内容就是如何进行mybatis实现批量修改xml方式的分析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网精选频道。

--结束END--

本文标题: 如何进行mybatis实现批量修改xml方式的分析

本文链接: https://www.lsjlt.com/news/300607.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作