雜記 myBatis plus中修改主鍵填充策略

2021-10-01 20:30:42 字數 1286 閱讀 6025

mybatis-plus中主鍵id預設使用雪花演算法生成的唯一id,當我們實際業務中需要自定義時,可以修改其填充策略,符合專案要求。

1,區域性主鍵策略實現

在實體類中 id屬性加註解

@tableid(type = idtype.auto) 主鍵自增 資料庫中需要設定主鍵自增

private long id;

@tableid(type = idtype.none) 預設 跟隨全域性策略走

private long id;

@tableid(type = idtype.uuid) uuid型別主鍵

private long id;

@tableid(type = idtype.id_worker) 數值型別 資料庫中也必須是數值型別 否則會報錯

private long id;

@tableid(type = idtype.id_worker_str) 字串型別 資料庫也要保證一樣字元型別

private long id;

@tableid(type = idtype.input) 使用者自定義了 資料型別和資料庫保持一致就行

private long id;

2,全域性主鍵策略實現

新增原始碼:

/*licensed under the apache license, version 2.0 (the 「license」); you may not

use this file except in compliance with the license. you may obtain a copy of

the license at

distributed under the license is distributed on an 「as is」 basis, without

warranties or conditions of any kind, either express or implied. see the

license for the specific language governing permissions and limitations under

the license.

*/package com.baomidou.mybatisplus.annotation;

import lombok.getter;

*/input(2),

/* 以下3種型別、只有當插入物件id 為空,才自動填充。 /

/* private final int key;

idtype(int key)

}

MybatisPlus中的刪除操作

目錄 物理刪除與邏輯刪除 物理刪除 邏輯刪除 物理刪除 從資料庫中直接移除邏輯刪除 沒有真實的被刪除掉,通過乙個變數讓該條記錄失效。delete 0 delete 1 根據id刪除 test public void testdeletebyid 根據id批量刪除 test public void t...

Mybatis plus中or的使用 二

之前寫到過 a and b and c or d 和 a or b or c 這兩種情況 現在呢,有一種情況是這樣的 a and b and c or d and e and f and g selectlist query lambda and eq test getid,id eq test g...

mybatis plus中實現樂觀鎖

1.主要適用場景 當要更新一條記錄的時候,希望這條記錄沒有被別人更新,也就是說實現執行緒安全的資料更新 2.樂觀鎖實現方式 在資料庫新增version欄位,預設為1 取出記錄時,獲取當前version 假設取出的version 1 select id,name,age,email,create ti...