溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何進(jìn)行mybatis實(shí)現(xiàn)批量修改xml方式的分析

發(fā)布時(shí)間:2021-11-26 13:13:02 來(lái)源:億速云 閱讀:209 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了如何進(jìn)行mybatis實(shí)現(xiàn)批量修改xml方式的分析,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

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

測(cè)試類方法如下:

@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è)叫錢塘江的,我要改成錢塘江水系,而且都這樣改,都要加上水系兩個(gè)字,這個(gè)好辦,用Java來(lái)實(shí)現(xiàn)的話就是先查詢出所有的內(nèi)容存入 list 中,然后遍歷這個(gè)list放入對(duì)象中,用Set實(shí)體類的方式拼接,然后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)去即可

上述內(nèi)容就是如何進(jìn)行mybatis實(shí)現(xiàn)批量修改xml方式的分析,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI