mybatis實(shí)現(xiàn)批量修改-xml方式
mybatis批量修改-xml
mybatis批量查詢(xún),批量新增就不聊了,今天看看批量修改。
直接上代碼吧
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類(lèi)中代碼如下:
int batchUpdate(List<PICAPPatientModel> list);
測(cè)試類(lèi)方法如下:
@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("上海市普陀區(qū)xxxx");
model1.setBirthTime(new Date());
model1.setRemark("哈哈哈哈");
PICAPPatientModel model2 = new PICAPPatientModel();
model2.setPatientId(5923302);
model2.setDoctor_id(5466927);
model2.setSex(1);
model2.setAddress("上海市普陀區(qū)xxxx金沙江路1008號(hào)");
model2.setBirthTime(new Date());
model2.setRemark("哈哈哈哈adsfsa");
updateMappingList.add(model1);
updateMappingList.add(model2);
patDocPatInfoMapper.batchUpdate(updateMappingList);
}
mybatis xml批量更新值
在表中已經(jīng)存好了名字,但是想在這些個(gè)名字后面再加上想要的內(nèi)容,例如表中有一個(gè)叫錢(qián)塘江的,我要改成錢(qián)塘江水系,而且都這樣改,都要加上水系兩個(gè)字,這個(gè)好辦,用Java來(lái)實(shí)現(xiàn)的話(huà)就是先查詢(xún)出所有的內(nèi)容存入 list 中,然后遍歷這個(gè)list放入對(duì)象中,用Set實(shí)體類(lèi)的方式拼接,然后Update
public Result uuu(){
List<MdWaterSystem> list = mdWaterSystemService.findAll();
for (MdWaterSystem mdWaterSystem : list) {
mdWaterSystem.setWaterName(mdWaterSystem.getWaterName()+"水系");
mdWaterSystemService.updates(mdWaterSystem);
}
return ResponseMsgUtil.success(list);
}
雖然這樣也能夠?qū)崿F(xiàn),但是大可不必用代碼,直接在SQL中寫(xiě)
update md_water_system set water_name = CONCAT(IFNULL(water_name,''), IFNULL('水系',''));
用CONCAT這個(gè)函數(shù)將現(xiàn)有的內(nèi)容中后面加上自己想加入的即可
若又不想要了,可以用SQL來(lái)替換
update md_water_system set water_name = REPLACE(water_name, '水系', '')
REPLACE這個(gè)函數(shù)是替換函數(shù),將要替換掉的字段內(nèi)容寫(xiě)進(jìn)去即可
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟
這篇文章主要介紹了SpringBoot+Redis執(zhí)行l(wèi)ua腳本的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java使用Semaphore對(duì)單接口進(jìn)行限流
本篇主要講如何使用Semaphore對(duì)單接口進(jìn)行限流,主要有三種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
java正則表達(dá)式提取數(shù)字的方法實(shí)例
這篇文章主要介紹了java正則表達(dá)式提取數(shù)字的方法,還有去除字符串?dāng)?shù)字的方法,大家參考使用吧2013-12-12
emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#)
這篇文章主要介紹了emoji表情與unicode編碼互轉(zhuǎn)的實(shí)現(xiàn)(JS,JAVA,C#),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
springCloud集成nacos config的過(guò)程
本文介紹spring cloud集成nacos config的過(guò)程,通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08
java使用枚舉封裝錯(cuò)誤碼及錯(cuò)誤信息詳解
這篇文章主要介紹了java使用枚舉封裝錯(cuò)誤碼及錯(cuò)誤信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

