MyBatis根據(jù)條件批量修改字段的方式
MyBatis根據(jù)條件批量修改字段
背景:
給學(xué)生改作業(yè),只要是對的都批量進(jìn)行數(shù)據(jù)庫的修改
代碼以及注釋
- conttoller
@RestController @RequestMapping("/work") public class WorkController { ?? ?@Autowired ?? ?private WorkService workService; ?? ? ?? ?@PutMapping("/examine") ?? ?public HttpResponse examine(Integer[] id) { ?? ??? ?Integer total = workService.examine(id); ?? ??? ?return HttpResponse.ok("共批改[ "+total+" ]條道題",""); ?? ?} }
- service
@Service public class WorkService { ?? ?@Autowired ?? ?private WorkMapper workMapper; ?? ? ?? ?public Integer examine(Integer[] id) { ?? ??? ?return workMapper.examine(id); ?? ?} }
- mapper
@Mapper public interface WorkMapper { ?? ?Integer examine(@Param("id")Integer[] id); }
- xml
<!--根據(jù)id來批量修改WORK表里面的isEnable字段--> <update id="examine"> ?? ?UPDATE WORK SET isEnable=TRUE WHERE id IN ?? ?<foreach collection="id" item="id" separator="," open="(" close=")"> ?? ??? ?#{id} ?? ?</foreach> </update>
- 工具類HttpResponse(此需求中無關(guān)緊要的類)
public class HttpResponse { ?? ?private Integer status; ?? ?private String message; ?? ?private Object object; ?? ? ?? ?public static HttpResponse ok(String message) { ?? ??? ?return new HttpResponse(200, message, null); ?? ?} ?? ? ?? ?public static HttpResponse ok(Object object) { ?? ??? ?return new HttpResponse(200, null, object); ?? ?} ?? ? ?? ?public static HttpResponse ok(String message,Object object) { ?? ??? ?return new HttpResponse(200, message, object); ?? ?} ?? ? ?? ?public static HttpResponse error(Integer status, String message) { ?? ??? ?return new HttpResponse(500, message, null); ?? ?} ?? ? ?? ?public static HttpResponse error(String message) { ?? ??? ?return new HttpResponse(500, message, null); ?? ?} ?? ? ?? ?public static HttpResponse error(String message,Object object) { ?? ??? ?return new HttpResponse(500, message, object); ?? ?} ?? ? ?? ? ?? ?protected HttpResponse() { ?? ??? ?super(); ?? ?} ?? ? ?? ?private HttpResponse(Integer status, String message, Object object) { ?? ??? ?super(); ?? ??? ?this.status = status; ?? ??? ?this.message = message; ?? ??? ?this.object = object; ?? ?} ?? ?public Integer getStatus() { ?? ??? ?return status; ?? ?} ?? ?public void setStatus(Integer status) { ?? ??? ?this.status = status; ?? ?} ?? ?public String getMessage() { ?? ??? ?return message; ?? ?} ?? ?public void setMessage(String message) { ?? ??? ?this.message = message; ?? ?} ?? ?public Object getObject() { ?? ??? ?return object; ?? ?} ?? ?public void setObject(Object object) { ?? ??? ?this.object = object; ?? ?} }
重點(diǎn):
如果mapper沒加@Param("id")注解會報(bào)錯找不到參數(shù)"id"
至此MyBatis根據(jù)id批量修改數(shù)據(jù)庫的某字段需求完成!
MyBatis多條件批量修改
簡單記錄下
想要修改一張表,是聯(lián)合主鍵,也就是where后兩個以上條件才能唯一確定一條數(shù)據(jù)。
如果有唯一鍵,那么foreach中 用in就可以解決。
現(xiàn)在沒辦法用in,不然 A in (#{})and B in (#{})感覺成笛卡爾了。
簡單點(diǎn)就是要實(shí)現(xiàn)執(zhí)行多條語句。對update做循環(huán)。傳入?yún)?shù)為 List,ATest類中字段A,B為聯(lián)合主鍵,修改C的值
<update id="update" parameterType="java.util.List"> ?? ??? ?<foreach collection="list" ?item="val" separator=";" open="begin" close=";end;"> ?? ??? ? ? ?update table_ABC ?? ??? ??? ?<set> ?? ??? ??? ??? ?<if test="val.C!= null">C = #{val.C},</if> ?? ??? ??? ?</set> ?? ??? ??? ?where A=#{val.A} and B= #{val.B} ?? ??? ?</foreach> ?? ?</update>
執(zhí)行出來效果為:
begin
update table_ABC set C = '2' where A = '1' and B= '1';
update table_ABC set C = '2' where A = '1' and B= '2';
update table_ABC set C = '4' where A = '3' and B= '2';
end;
這里使用的數(shù)據(jù)庫為oracle,oracle執(zhí)行認(rèn)為begin和end之前為一條語句
當(dāng)然,也可以用or 的形式拼接,不過還沒測試。
網(wǎng)上查有的說是mysql數(shù)據(jù)庫 mybatis一次執(zhí)行一條語句。支持多條,可以MySQL連接數(shù)據(jù)庫時,添加語句:“allowMultiQueries=true”
作用:
1.可以在sql語句后攜帶分號,實(shí)現(xiàn)多語句執(zhí)行。
2.可以執(zhí)行批處理,同時發(fā)出多個SQL語句。
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10SpringBoot項(xiàng)目中java -jar xxx.jar沒有主清單屬性的解決方法
這篇文章主要給大家介紹了SpringBoot項(xiàng)目中java -jar xxx.jar沒有主清單的解決方法,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01Java8使用Supplier啟動ScheduledThread代碼實(shí)例
這篇文章主要介紹了Java8使用Supplier啟動ScheduledThread詳解,項(xiàng)目開啟立即啟動定時任務(wù)是很多項(xiàng)目都會遇到的一個需求,如何利用Java提供的函數(shù)優(yōu)雅的寫出來十分考驗(yàn)一個人的功底,需要的朋友可以參考下2024-01-01Java實(shí)現(xiàn)入?yún)?shù)據(jù)批量數(shù)據(jù)校驗(yàn)詳解
在業(yè)務(wù)處理中一般入?yún)⑹菃螚l數(shù)據(jù),這樣數(shù)據(jù)校驗(yàn)比較容易,但是這種方法對于集合數(shù)據(jù)的校驗(yàn)不適用,下面我們就來看看如何對入?yún)?shù)據(jù)進(jìn)行批量數(shù)據(jù)校驗(yàn)吧2024-02-02