MyBatis Plus基本操作總結

2021-10-07 22:49:51 字數 4842 閱讀 2599

定義資料庫表

create table user

( id bigint(20) not null comment '主鍵id',

name varchar(30) null default null comment '姓名',

age int(11) null default null comment '年齡',

email varchar(50) null default null comment '郵箱',

primary key (id)

);

定義實體類對映

@data

public class user

}

@runwith(springrunner.class)

public class mybatisplusttests

}

#mybatis日誌

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

id_worker

mybatis-plus的預設主鍵生成策略,全域性唯一id

參考資料:分布式系統唯一id生成方案彙總

自增策略

要想實現自增策略的主鍵增長方式,需要配置如下:

資料庫表中設定為主鍵自增

在實體欄位中配置@tableid(type=idtype.auto)註解

@data

public class user

要想一次性影響全域性實體的配置,可設定全域性主鍵配置
#全域性設定主鍵生成策略

mybatis-plus.global-config.db-config.id-type=auto

其它主鍵策略:分析 idtype 原始碼可知

@getter

public enum idtype

}

專案中我們通常會遇到一些資料,每次都使用相同的方式填充,如建立時間,更新時間。

在這時,我們就可以使用自動填充功能來完成對這些資料的自動賦值。

在資料庫表中新增自動填充字段create_time,update_time在對應的實體對映類中新增相關對映,並新增註解@tablefield

@data

public

class

user

實現元物件處理介面

注意:這裡需要加上@component註解將此配置注入到spring容器中。setfieldvalbyname()方法中的第乙個引數為實體類的屬性名稱而不是資料庫中的字段!

@component

public

class

mymetaobjecthandler

implements

metaobjecthandler

@override

public

void

updatefill

(metaobject metaobject)

}

此時我們在進行新增操作和更新操作時就不需要給createtime和updatetime欄位進行複製,mybatis-plus框架會自動進行字段填充。

=

=> preparing: update

user

set name=?, age=?, email=?, update_time=? where id=? ==

> parameters: 李大娃子(string),20

(integer),

903611454

@qq.com

(string)

,2020-07

-1014:40:54.102

(timestamp),

6(long)

<=

= updates: 1

什麼是樂觀鎖,什麼悲觀鎖,主要解決的是什麼問題

當程式中可能出現併發操作時,我們就需要通過使用一定的手段來保證併發情況下資料的準確性,通過這種手段保證,當使用者和其他使用者一起操作的情況下,所得到的結果和他單獨操作時得到的結果是一樣的,這種手段就叫做併發控制。併發控制的目的是保證乙個使用者的操作結果不會對另乙個使用者的操作產生不和裡的影響。

如何沒有做好併發控制,就可能導致幻讀、髒讀和不可重複讀

主要使用場景:當要更新一條記錄是,希望這條記錄不被別人更新,也就是說實現執行緒安全的更新操作。

mybatis-plus中樂觀鎖的實現方式:

在資料庫表中新增version欄位

alter

table

`user

`add

column

`version`

int

在實體類中新增version欄位,同時新增@version註解

@version

@tablefield

(fill = fieldfill.insert)

private integer version;

元物件處理介面新增version的insert預設值

this

.setfieldvalbyname

("version",1

,metaobject)

;

特別說明:

在mybatisplusconfig類中註冊bean

@configuration

@enabletransactionmanagement

public

class

mybatisplusconfig

}

測試

@test

public

void

testoptimisticlocker()

=

=> preparing: update

user

set name=?, age=?, email=?, create_time=?, update_time=?, version=? where id=? and version=? ==

> parameters: 李小娃子(string),20

(integer),

903611454

@qq.com

(string)

,2020-07

-1015:19:46.0

(timestamp),

2020-07

-1015:28:59.518

(timestamp),

2(integer),

7(long),1

(integer

)<=

= updates: 1

mybatis plus自帶分頁外掛程式,只要簡單的配置即可實現分頁功能

建立配置類

@bean

public paginationinterceptor paginationinterceptor()

測試selectpage分頁

@test

public

void

selectbypage()

@test

public

void

testselectmapspage()

在資料庫中新增「deleted」字段

alter table `user` add column `deleted` boolean
在實體類中新增對應對映,並新增@tablelogic註解

@tablelogic

@tablefield

(fill = fieldfill.insert)

private integer deleted;

元物件處理介面新增deleted的insert預設值

this

.setfieldvalbyname

("deleted",0

,metaobject)

;

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

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

0

在mybatisplusconfig中註冊bean

@bean

public isqlinjector isqlinjector()

效能分析***,用於輸入每條sql語句及其執行時間

sql效能分析器,開發環境使用,超出制定時間,停止執行,有助於發現問題。

配置外掛程式

@bean

@profile()

//標註在開發和測試環境中使用

public performanceinterceptor performanceinterceptor()

mybatisplus 基本操作

mybatisplus日誌 mybatis plus.configuration.log impl org.apach.ibatis.logging.stdout.stdoutimpl 設定mp的主鍵生成策略 在實體類的主鍵上加註解 tableid type idtype.auto id worke...

mybatis plus基本操作

只針對稍微有難度的,部分未涉及到的簡單操作可以參考官方文件 data equalsandhashcode callsuper false tablename user public class user implements serializable 1.插入物件,返回id user user ne...

mybatis plus的基本操作(CRUD)

runwith springjunit4classrunner.class contextconfiguration public class testmp02 tablename user 可以設定物件和表的對映 tableid type idtype.auto 可以設定id的主鍵自增 table...