[MyBatisPlus]常用注解_@TableName_@TableId_@TableField_@TableLogic通过全局配置配置主键生成策略
生活随笔
收集整理的這篇文章主要介紹了
[MyBatisPlus]常用注解_@TableName_@TableId_@TableField_@TableLogic通过全局配置配置主键生成策略
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
常用注解
@TableName
- 設(shè)置實(shí)體類所對(duì)應(yīng)的表名
如果全部表都有某個(gè)前綴,難道我們要通過(guò)一個(gè)一個(gè)加注解的方式來(lái)解決實(shí)體類對(duì)應(yīng)表名問題嗎?
我們可以通過(guò)配置文件來(lái)解決這個(gè)問題:
@TableId
- 將屬性所對(duì)應(yīng)的字段指定為主鍵
@TableId的value屬性
@TableId的type屬性
/** Copyright (c) 2011-2022, baomidou (jobob@qq.com).** 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** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* 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;/*** 生成ID類型枚舉類** @author hubin* @since 2015-11-10*/ @Getter public enum IdType {/*** 數(shù)據(jù)庫(kù)ID自增* <p>該類型請(qǐng)確保數(shù)據(jù)庫(kù)設(shè)置了 ID自增 否則無(wú)效</p>*/AUTO(0),/*** 該類型為未設(shè)置主鍵類型(注解里等于跟隨全局,全局里約等于 INPUT)*/NONE(1),/*** 用戶輸入ID* <p>該類型可以通過(guò)自己注冊(cè)自動(dòng)填充插件進(jìn)行填充</p>*/INPUT(2),/* 以下3種類型、只有當(dāng)插入對(duì)象ID 為空,才自動(dòng)填充。 *//*** 分配ID (主鍵類型為number或string),* 默認(rèn)實(shí)現(xiàn)類 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(雪花算法)** @since 3.3.0*/ASSIGN_ID(3),/*** 分配UUID (主鍵類型為 string)* 默認(rèn)實(shí)現(xiàn)類 {@link com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator}(UUID.replace("-",""))*/ASSIGN_UUID(4);private final int key;IdType(int key) {this.key = key;} }將生成id的算法改成自增算法(默認(rèn)雪花算法)
常用的主鍵策略:
| IdType.ASSIGN_ID(默認(rèn)) | 基于雪花算法的策略生成數(shù)據(jù)id,與數(shù)據(jù)庫(kù)id是否設(shè)置自增無(wú)關(guān) |
| IdType.AUTO | 使用數(shù)據(jù)庫(kù)的自增策略,注意,該類型請(qǐng)確保數(shù)據(jù)庫(kù)設(shè)置了id自增,否則無(wú)效 |
通過(guò)全局配置配置主鍵生成策略
@TableField
- 指定屬性所對(duì)應(yīng)的字段名
可以運(yùn)行,mybatisplus默認(rèn)駝峰命名法
@TableLogic
邏輯刪除
- 物理刪除:真實(shí)刪除,將對(duì)應(yīng)數(shù)據(jù)從數(shù)據(jù)庫(kù)中刪除,之后查詢不到此條被刪除的數(shù)據(jù)
- 邏輯刪除:假刪除,將對(duì)應(yīng)數(shù)據(jù)中代表是否被刪除字段的狀態(tài)修改為“被刪除狀態(tài)”,之后在數(shù)據(jù)庫(kù)中仍舊能看到此條數(shù)據(jù)記錄
- 使用場(chǎng)景:可以進(jìn)行數(shù)據(jù)恢復(fù)
實(shí)現(xiàn)邏輯刪除
step1 數(shù)據(jù)庫(kù)中創(chuàng)建邏輯刪除狀態(tài)列,設(shè)置默認(rèn)值為0
step2 實(shí)體類中添加邏輯刪除屬性
step3:測(cè)試
/*** 通過(guò)多個(gè)id實(shí)現(xiàn)批量刪除*/@Testpublic void testDelete02(){List<Long> list = Arrays.asList(1L, 2L, 3L);int result = userMapper.deleteBatchIds(list);System.out.println("result = "+result);}測(cè)試刪除功能,真正執(zhí)行的是修改
UPDATE t_user SET is_deleted=1 WHERE id=? AND is_deleted=0
測(cè)試查詢功能,被邏輯刪除的數(shù)據(jù)默認(rèn)不會(huì)被查詢
SELECT id,username AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0
查詢所有數(shù)據(jù),也查不到刪除的數(shù)據(jù)了
/*** 查詢所有數(shù)據(jù)*/@Testpublic void testSelect02(){List<User> users = userMapper.selectList(null);users.forEach(System.out::println);}總結(jié)
以上是生活随笔為你收集整理的[MyBatisPlus]常用注解_@TableName_@TableId_@TableField_@TableLogic通过全局配置配置主键生成策略的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone XR如何截屏
- 下一篇: [MyBatisPlus]条件构造器wa