mybatis实现批量修改

网友投稿 784 2022-11-19

mybatis实现批量修改

mybatis实现批量修改

目录mybatis批wDnvaMmNyf量修改-xmlmybatis xml批量更新值

mybatis批量修改-xml

mybatis批量查询,批量新增就不聊了,今天看看批量修改。

直接上代码

xml文件中代码如下:

update pat_doc_pat_info set

sex=

when #{item.patientId} then #{item.sex}

,address=

when #{item.patientId} then #{item.address}

,birth_time=

when #{item.patientId} then #{item.birthTime}

,remark=

when #{item.patientId} then #{item.remark}

,modified_time = now()

,belong_hospital = 1

where delete_flag = 1

and doctor_id =

when #{item.patientId} then #{item.doctor_id}

and patient_id in

#{item.patientId}

mAPPer类中代码如下:

int batchUpdate(List list);

测试类方法如下:

@Autowired

private PatDocPatInfoMapper patDocPatInfoMapper;

@Test

public void testMapperMethod () {

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:声音合成器 1 基本噪音
下一篇:ECNA 2013 部分题解 | 训练记录
相关文章

 发表评论

暂时没有评论,来抢沙发吧~