溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MyBatis怎么根據(jù)條件批量修改字段

發(fā)布時間:2023-02-22 16:28:36 來源:億速云 閱讀:107 作者:iii 欄目:開發(fā)技術

本篇內(nèi)容介紹了“MyBatis怎么根據(jù)條件批量修改字段”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

    MyBatis根據(jù)條件批量修改字段

    背景:

    給學生改作業(yè),只要是對的都批量進行數(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(此需求中無關緊要的類)

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

    重點:

    如果mapper沒加@Param("id")注解會報錯找不到參數(shù)"id"

    至此MyBatis根據(jù)id批量修改數(shù)據(jù)庫的某字段需求完成!

    MyBatis多條件批量修改

    簡單記錄下

    想要修改一張表,是聯(lián)合主鍵,也就是where后兩個以上條件才能唯一確定一條數(shù)據(jù)。

    如果有唯一鍵,那么foreach中 用in就可以解決。

    現(xiàn)在沒辦法用in,不然 A in (#{})and B in (#{})感覺成笛卡爾了。

    簡單點就是要實現(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í)行認為begin和end之前為一條語句

    當然,也可以用or 的形式拼接,不過還沒測試。

    網(wǎng)上查有的說是mysql數(shù)據(jù)庫 mybatis一次執(zhí)行一條語句。支持多條,可以MySQL連接數(shù)據(jù)庫時,添加語句:“allowMultiQueries=true”

    作用:

    1.可以在sql語句后攜帶分號,實現(xiàn)多語句執(zhí)行。

    2.可以執(zhí)行批處理,同時發(fā)出多個SQL語句。

    “MyBatis怎么根據(jù)條件批量修改字段”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

    向AI問一下細節(jié)

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

    AI