MybatisPlus 3 3 2的基本使用

2021-10-07 04:31:49 字數 4386 閱讀 7163

這個框架是mybatis的加強版,沒有對mybatis侵入

1、在springboot中引入

>

>

com.baomidougroupid

>

>

mybatis-plus-boot-starterartifactid

>

>

3.3.2version

>

dependency

>

2、在配置檔案中配置

# mysql連線資料庫

spring.datasource.dirver-

class

-name=com.mysql.cj.jdbc.driver

spring.datasource.url=jdbc:mysql:

//localhost:

3306

/xiang?servertimezone=gmt%

2b8spring.datasource.username=root

spring.datasource.password=

# mybatis 日誌

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.stdoutimpl

@repository

public

inte***ce

extends

(

)public

class

}6、測試

@springboottest

class

// 根據多個id查詢

@test

void

testfindbyids()

// 簡單條件查詢

@test

void

testfindbymany()

// 新增操作

@test

void

adduser()

// 修改操作

@test

void

updateuser()

}

@tableid

(type = idtype.auto)

// 設定主鍵的生成策略

idtype.auto: 表示自增長

idtype.id_worker: 預設的,使用snowflake演算法生成,是乙個19位的數字

這個一般根據個人喜好決定是否使用

1、假定表中有乙個建立時間(createtime)和乙個更新時間(updatetime),首先加上欄位的填充策略

@tablefield

(fill = fieldfill.insert)

private date createtime;

@tablefield

(fill = fieldfill.insert_update)

private date updatetime;

2、實現具體的填充控制

@component

public

class

mymetaobjecthandler

implements

metaobjecthandler

// 使用mybatisplus實現修改操作,這個方法就會執行

@override

public

void

updatefill

(metaobject metaobject)

}

主要解決:丟失更新

問題:

如果不考慮事物的隔離性,就會產生讀的問題:

髒讀 不可重複讀 幻讀

寫的問題:丟失更新的問題

案例:張三有500塊的工資

財務a: 開始事物 —> 500 + 8000 —> 首先提交事物

財務b: 開始事物 —> 500 - 200 —> 之後提交事務

這樣在開始事物的時候是同時進行的,但是提交事物的順序是有先後的,這樣就造成了更新丟失

解決方案:

1、悲觀鎖:序列,就是在a執行事物的時候其他人不許執行

2、樂觀鎖:在資料上加了乙個version,提交事務的時候,會將當前的版本與資料庫版本進行比較,如果版本一致,就會提交事物,然後更新版本

樂觀鎖的主要適用場景:

當要更新一條記錄時,希望這條記錄沒有被別人更新,也就是說實現執行緒安全的更新

樂觀鎖的實現方式:

取出記錄時,獲取當前的version

更新時,帶上這個version

執行更新時,set version = newversion where version=oldversion

如果版本不對,就更新失敗

mybatisplus中具體實現步驟

(1)首先在資料庫新增version欄位

(2)在實體類新增 version欄位

並新增@version註解

@version

@tablefield

(fill = fieldfill.insert)

private integer version;

// 版本號

(3)元物件介面處理器新增version的insert預設值

this

.setfieldvalbyname

("version",1

, metaobject)

;

(4)配置外掛程式

@configuration()

public

class

mpconfig

}

物理刪除:從資料庫中刪除

邏輯刪除:資料庫中還存在,只是查不到或者不顯示而已

注意事項:邏輯刪除是為了方便資料恢復和保護資料本身價值等等的一種方案,但實際就是刪除。

如果你需要再查出來就不應使用邏輯刪除,而是以乙個狀態去表示

實現步驟:

1、資料庫中新增字段 deleted

2、實體類中的字段新增 @tablelogic 標記

@tablelogic

private integer deleted;

3、配置檔案(也可以預設不寫)

# 配置邏輯刪除

# 全域性邏輯刪除字段值 3.3

.0開始支援,但如果實體類上有 @tablelogic 則以實體上的為準,忽略全域性。 即先查詢註解再查詢全域性,都沒有則此表沒有邏輯刪除。。

mybatis-plus.global-config.db-config.logic-delete-field=flag

# 邏輯已刪除值(預設為 1

)mybatis-plus.global-config.db-config.logic-delete-value=

1# 邏輯未刪除值(預設為 0

)mybatis-plus.global-config.db-config.logic-not-delete-value=

0

1、配置外掛程式

@bean

public paginationinterceptor paginationinterceptor()

2、測試

// 測試分頁

@test

void

testselectbypage()

注意:測試這個功能時我用的3.3.2版本,要新增擴充套件,執行時還會有衝突,感覺不太好用

三種環境

dev:開發

test:測試

prod:生產

1、配置外掛程式

/**

* sql執行效率外掛程式

*/@bean

@profile()

// 設定 dev test 環境開啟

public performanceinterceptor performanceinterceptor()

2、springboot環境設定

#環境設定(test dev prod)

spring.profiles.active=dev

MyBatis Plus的CRUD 簡單操作

crud 是指在做計算處理時的增加 create 讀取查詢 retrieve 更新 update 和刪除 delete 幾個單詞的首字母簡寫。增加操作 resource test public void insert 執行完成後的 查詢操作 test public void selectbyname...

MybatisPlus中的刪除操作

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

MybatisPlus的自動填充功能

在對資料可進行一些操作的時候,有些字段基本是固定,比如建立時間和修改時間,我們可以利用mybatisplus的自動填充功能來實現。1.首先在需要在自動填充的字段屬性上新增配置 fieldfill.insert表示插入操作時起作用,fieldfill.insert update表示在插入和修改時起作用...