日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

mybatis plus 使用技巧

發(fā)布時(shí)間:2023/12/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis plus 使用技巧 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

mybatis plus 看這篇就夠了,一發(fā)入魂

文章目錄

  • mybatis plus 看這篇就夠了,一發(fā)入魂
  • 一、mybatis plus 介紹
  • 二、快速入門(mén)
    • 1.創(chuàng)建一個(gè)SpringBoot項(xiàng)目
    • 2.導(dǎo)入依賴(lài)
    • 3.配置數(shù)據(jù)庫(kù)
    • 4.創(chuàng)建一個(gè)實(shí)體類(lèi)
    • 5.創(chuàng)建一個(gè)mapper接口
    • 6.在SpringBoot啟動(dòng)類(lèi)上配置mapper接口的掃描路徑
    • 7.在數(shù)據(jù)庫(kù)中創(chuàng)建表
    • 8.編寫(xiě)一個(gè)SpringBoot測(cè)試類(lèi)
  • 三、核心功能
    • 1.注解
    • 2.CRUD接口
      • Mapper CRUD接口
      • Service CRUD 接口
    • 3.條件構(gòu)造器
      • **使用示例**:
      • Condition
      • 實(shí)體對(duì)象作為條件
      • allEq方法
      • lambda條件構(gòu)造器
    • 4.更新操作
    • 5.刪除操作
    • 6.自定義SQL
      • 分頁(yè)查詢(xún)
        • 1.創(chuàng)建mp的分頁(yè)攔截器,注冊(cè)到Spring容器中
        • 2.執(zhí)行分頁(yè)查詢(xún)
        • 3.其他
    • 7.AR模式
    • 8.主鍵策略
      • 配置
        • 基本配置
        • 進(jìn)階配置
    • 9.代碼生成器
    • 10.高級(jí)功能
      • 邏輯刪除
      • 自動(dòng)填充
      • 樂(lè)觀鎖插件
      • 性能分析插件
      • 多租戶(hù)SQL解析器
      • 動(dòng)態(tài)表名SQL解析器
  • 四、總結(jié)


一、mybatis plus 介紹

????官方網(wǎng)站:baomidou.com
????mybatis-plus是一款Mybatis增強(qiáng)工具,用于簡(jiǎn)化開(kāi)發(fā),提高效率。下文使用縮寫(xiě)mp來(lái)簡(jiǎn)化表示mybatis-plus,本文主要介紹mp搭配SpringBoot的使用。

二、快速入門(mén)

1.創(chuàng)建一個(gè)SpringBoot項(xiàng)目

2.導(dǎo)入依賴(lài)

pom.xml:

<!-- pom.xml --> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>mybatis-plus</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatis-plus</name><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>

3.配置數(shù)據(jù)庫(kù)

application.yml:

# application.yml spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/yogurt?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8username: rootpassword: rootmybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #開(kāi)啟SQL語(yǔ)句打印

4.創(chuàng)建一個(gè)實(shí)體類(lèi)

package com.example.mp.po; import lombok.Data; import java.time.LocalDateTime; @Data public class User {private Long id;private String name;private Integer age;private String email;private Long managerId;private LocalDateTime createTime; }

5.創(chuàng)建一個(gè)mapper接口

package com.example.mp.mappers; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.mp.po.User; public interface UserMapper extends BaseMapper<User> { }

6.在SpringBoot啟動(dòng)類(lèi)上配置mapper接口的掃描路徑

package com.example.mp; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mp.mappers") public class MybatisPlusApplication {public static void main(String[] args) {SpringApplication.run(MybatisPlusApplication.class, args);} }

7.在數(shù)據(jù)庫(kù)中創(chuàng)建表

DROP TABLE IF EXISTS user; CREATE TABLE user ( id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主鍵', name VARCHAR(30) DEFAULT NULL COMMENT '姓名', age INT(11) DEFAULT NULL COMMENT '年齡', email VARCHAR(50) DEFAULT NULL COMMENT '郵箱', manager_id BIGINT(20) DEFAULT NULL COMMENT '直屬上級(jí)id', create_time DATETIME DEFAULT NULL COMMENT '創(chuàng)建時(shí)間', CONSTRAINT manager_fk FOREIGN KEY(manager_id) REFERENCES user (id) ) ENGINE=INNODB CHARSET=UTF8;INSERT INTO user (id, name, age ,email, manager_id, create_time) VALUES (1, '大BOSS', 40, 'boss@baomidou.com', NULL, '2021-03-22 09:48:00'), (2, '李經(jīng)理', 40, 'boss@baomidou.com', 1, '2021-01-22 09:48:00'), (3, '黃主管', 40, 'boss@baomidou.com', 2, '2021-01-22 09:48:00'), (4, '吳組長(zhǎng)', 40, 'boss@baomidou.com', 2, '2021-02-22 09:48:00'), (5, '小菜', 40, 'boss@baomidou.com', 2, '2021-02-22 09:48:00')

8.編寫(xiě)一個(gè)SpringBoot測(cè)試類(lèi)

package com.example.mp; import com.example.mp.mappers.UserMapper; import com.example.mp.po.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest public class SampleTest {@Autowiredprivate UserMapper mapper;@Testpublic void testSelect() {List<User> list = mapper.selectList(null);assertEquals(5, list.size());list.forEach(System.out::println);} }

項(xiàng)目目錄如下:

運(yùn)行測(cè)試類(lèi)

????可以看到,針對(duì)單表的基本CRUD操作,只需要?jiǎng)?chuàng)建好實(shí)體類(lèi),并創(chuàng)建一個(gè)繼承自BaseMapper的接口即可,可謂非常簡(jiǎn)潔。并且,我們注意到,User類(lèi)中的managerId,createTime屬性,自動(dòng)和數(shù)據(jù)庫(kù)表中的manager_id,create_time對(duì)應(yīng)了起來(lái),這是因?yàn)閙p自動(dòng)做了數(shù)據(jù)庫(kù)下劃線命名,到Java類(lèi)的駝峰命名之間的轉(zhuǎn)化。

三、核心功能

1.注解

????mybatis plus 用在Java的實(shí)體類(lèi)上面的8個(gè)注解:

@TableName
注解在類(lèi)上,指定類(lèi)和數(shù)據(jù)庫(kù)表的映射關(guān)系。實(shí)體類(lèi)的類(lèi)名(轉(zhuǎn)成小寫(xiě)后)和數(shù)據(jù)庫(kù)表名相同時(shí),可以不指定該注解。

@TableId
注解在實(shí)體類(lèi)的某一字段上,表示這個(gè)字段對(duì)應(yīng)數(shù)據(jù)庫(kù)表的主鍵。當(dāng)主鍵名為id時(shí)(表中列名為id,實(shí)體類(lèi)中字段名為id),無(wú)需使用該注解顯式指定主鍵,mp會(huì)自動(dòng)關(guān)聯(lián)。若類(lèi)的字段名和表的列名不一致,可用value屬性指定表的列名。另,這個(gè)注解有個(gè)重要的屬性type,用于指定主鍵策略,參見(jiàn)主鍵策略小節(jié)

@TableField
注解在某一字段上,指定Java實(shí)體類(lèi)的字段和數(shù)據(jù)庫(kù)表的列的映射關(guān)系。這個(gè)注解有如下幾個(gè)應(yīng)用場(chǎng)景。

  • 排除非表字段

????若Java實(shí)體類(lèi)中某個(gè)字段,不對(duì)應(yīng)表中的任何列,它只是用于保存一些額外的,或組裝后的數(shù)據(jù),則可以設(shè)置exist屬性為false,這樣在對(duì)實(shí)體對(duì)象進(jìn)行插入時(shí),會(huì)忽略這個(gè)字段。

  • 字段驗(yàn)證策略

????通過(guò)insertStrategy,updateStrategy,whereStrategy屬性進(jìn)行配置,可以控制在實(shí)體對(duì)象進(jìn)行插入,更新,或作為WHERE條件時(shí),對(duì)象中的字段要如何組裝到SQL語(yǔ)句中。

  • 字段填充策略

????通過(guò)fill屬性指定,字段為空時(shí)會(huì)進(jìn)行自動(dòng)填充。

@Version
樂(lè)觀鎖注解。

@EnumValue
注解在枚舉字段上。

@TableLogic
邏輯刪除。

@KeySequence
序列主鍵策略(oracle)。

@InterceptorIgnore
插件過(guò)濾規(guī)則。

2.CRUD接口

????mp封裝了一些最基礎(chǔ)的CRUD方法,只需要直接繼承mp提供的接口,無(wú)需編寫(xiě)任何SQL,即可食用。mp提供了兩套接口,分別是Mapper CRUD接口和Service CRUD接口。并且mp還提供了條件構(gòu)造器Wrapper,可以方便地組裝SQL語(yǔ)句中的WHERE條件。

Mapper CRUD接口

????只需定義好實(shí)體類(lèi),然后創(chuàng)建一個(gè)接口,繼承mp提供的BaseMapper,即可食用。mp會(huì)在mybatis啟動(dòng)時(shí),自動(dòng)解析實(shí)體類(lèi)和表的映射關(guān)系,并注入帶有通用CRUD方法的mapper。BaseMapper里提供的方法,部分列舉如下:

insert(T entity) 插入一條記錄。
deleteById(Serializable id) 根據(jù)主鍵id刪除一條記錄。
delete(Wrapper wrapper) 根據(jù)條件構(gòu)造器wrapper進(jìn)行刪除。
selectById(Serializable id) 根據(jù)主鍵id進(jìn)行查找。
selectBatchIds(Collection idList) 根據(jù)主鍵id進(jìn)行批量查找。
selectByMap(Map<String,Object> map) 根據(jù)map中指定的列名和列值進(jìn)行等值匹配查找。
selectMaps(Wrapper wrapper) 根據(jù) wrapper 條件,查詢(xún)記錄,將查詢(xún)結(jié)果封裝為一個(gè)Map,Map的key為結(jié)果的列,value為值。
selectList(Wrapper wrapper) 根據(jù)條件構(gòu)造器wrapper進(jìn)行查詢(xún)。
update(T entity, Wrapper wrapper) 根據(jù)條件構(gòu)造器wrapper進(jìn)行更新。
updateById(T entity)

????簡(jiǎn)單的使用示例如前文快速入門(mén)小節(jié),下面講解幾個(gè)比較特別的方法:

  • selectMaps

????BaseMapper接口還提供了一個(gè)selectMaps方法,這個(gè)方法會(huì)將查詢(xún)結(jié)果封裝為一個(gè)Map,Map的key為結(jié)果的列,value為值。
該方法的使用場(chǎng)景如下:

只查部分列
當(dāng)某個(gè)表的列特別多,而SELECT的時(shí)候只需要選取個(gè)別列,查詢(xún)出的結(jié)果也沒(méi)必要封裝成Java實(shí)體類(lèi)對(duì)象時(shí)(只查部分列時(shí),封裝成實(shí)體后,實(shí)體對(duì)象中的很多屬性會(huì)是null),則可以用selectMaps,獲取到指定的列后,再自行進(jìn)行處理即可。

@Testpublic void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.select("id","name","email").likeRight("name","黃");List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);maps.forEach(System.out::println);}

  • 進(jìn)行數(shù)據(jù)統(tǒng)計(jì)
// 按照直屬上級(jí)進(jìn)行分組,查詢(xún)每組的平均年齡,最大年齡,最小年齡 /** select avg(age) avg_age ,min(age) min_age, max(age) max_age from user group by manager_id having sum(age) < 500; **/@Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.select("manager_id", "avg(age) avg_age", "min(age) min_age", "max(age) max_age").groupBy("manager_id").having("sum(age) < {0}", 500);List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);maps.forEach(System.out::println); }

  • selectObjs

只會(huì)返回第一個(gè)字段(第一列)的值,其他字段會(huì)被舍棄

@Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.select("id", "name").like("name", "黃");List<Object> objects = userMapper.selectObjs(wrapper);objects.forEach(System.out::println); }

  • selectCount
    查詢(xún)滿(mǎn)足條件的總數(shù),注意,使用這個(gè)方法,不能調(diào)用QueryWrapper的select方法設(shè)置要查詢(xún)的列了。這個(gè)方法會(huì)自動(dòng)添加select count(1)
@Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.like("name", "黃");Integer count = userMapper.selectCount(wrapper);System.out.println(count); }

Service CRUD 接口

????另外一套CRUD是Service層的,只需要編寫(xiě)一個(gè)接口,繼承IService,并創(chuàng)建一個(gè)接口實(shí)現(xiàn)類(lèi),即可食用。(這個(gè)接口提供的CRUD方法,和Mapper接口提供的功能大同小異,比較明顯的區(qū)別在于IService支持了更多的批量化操作,如saveBatch,saveOrUpdateBatch等方法。

  • 首先,新建一個(gè)接口,繼承IService
  • package com.example.mp.service;import com.baomidou.mybatisplus.extension.service.IService; import com.example.mp.po.User;public interface UserService extends IService<User> { }
  • 創(chuàng)建這個(gè)接口的實(shí)現(xiàn)類(lèi),并繼承ServiceImpl,最后打上@Service注解,注冊(cè)到Spring容器中,即可使用
  • package com.example.mp.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.mp.mappers.UserMapper; import com.example.mp.po.User; import com.example.mp.service.UserService; import org.springframework.stereotype.Service;@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
  • 測(cè)試代碼
  • package com.example.mp;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.example.mp.po.User; import com.example.mp.service.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class ServiceTest {@Autowiredprivate UserService userService;@Testpublic void testGetOne() {LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();wrapper.gt(User::getAge, 28);User one = userService.getOne(wrapper, false); // 第二參數(shù)指定為false,使得在查到了多行記錄時(shí),不拋出異常,而返回第一條記錄System.out.println(one);} }


    另,IService也支持鏈?zhǔn)秸{(diào)用,代碼寫(xiě)起來(lái)非常簡(jiǎn)潔,查詢(xún)示例如下

    @Test public void testChain() {List<User> list = userService.lambdaQuery().gt(User::getAge, 39).likeRight(User::getName, "王").list();list.forEach(System.out::println); }

    更新語(yǔ)句:

    @Test public void testChain() {userService.lambdaUpdate().gt(User::getAge, 39).likeRight(User::getName, "王").set(User::getEmail, "w39@baomidou.com").update(); }

    刪除語(yǔ)句:

    @Test public void testChain() {userService.lambdaUpdate().like(User::getName, "青蛙").remove(); }

    3.條件構(gòu)造器

    ????mp讓我覺(jué)得極其方便的一點(diǎn)在于其提供了強(qiáng)大的條件構(gòu)造器Wrapper,可以非常方便的構(gòu)造WHERE條件。條件構(gòu)造器主要涉及到3個(gè)類(lèi),AbstractWrapper。QueryWrapper,UpdateWrapper,它們的類(lèi)關(guān)系如下:

    ????mp在AbstractWrapper中提供了非常多的方法用于構(gòu)建WHERE條件,而QueryWrapper針對(duì)SELECT語(yǔ)句,提供了select()方法,可自定義需要查詢(xún)的列,而UpdateWrapper針對(duì)UPDATE語(yǔ)句,提供了set()方法,用于構(gòu)造set語(yǔ)句。條件構(gòu)造器也支持lambda表達(dá)式,寫(xiě)起來(lái)非常舒爽。
    下面對(duì)AbstractWrapper中用于構(gòu)建SQL語(yǔ)句中的WHERE條件的方法進(jìn)行部分列舉:

    • eq:equals,等于
    • allEq:all equals,全等于
    • ne:not equals,不等于
    • gt:greater than ,大于 >
    • ge:greater than or equals,大于等于≥
    • lt:less than,小于<
    • le:less than or equals,小于等于≤
    • between:相當(dāng)于SQL中的BETWEEN
    • notBetween
    • like:模糊匹配。like(“name”,“黃”),相當(dāng)于SQL的name like ‘%黃%’
    • likeRight:模糊匹配右半邊。likeRight(“name”,“黃”),相當(dāng)于SQL的name like ‘黃%’
    • likeLeft:模糊匹配左半邊。likeLeft(“name”,“黃”),相當(dāng)于SQL的name like ‘%黃’
    • notLike:notLike(“name”,“黃”),相當(dāng)于SQL的name not like ‘%黃%’
    • isNull
    • isNotNull
    • in
    • and:SQL連接符AND
    • or:SQL連接符OR
    • apply:用于拼接SQL,該方法可用于數(shù)據(jù)庫(kù)函數(shù),并可以動(dòng)態(tài)傳參

    使用示例

    // 案例先展示需要完成的SQL語(yǔ)句,后展示W(wǎng)rapper的寫(xiě)法// 1. 名字中包含佳,且年齡小于25 // SELECT * FROM user WHERE name like '%佳%' AND age < 25 QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.like("name", "佳").lt("age", 25); List<User> users = userMapper.selectList(wrapper); // 下面展示SQL時(shí),僅展示W(wǎng)HERE條件;展示代碼時(shí), 僅展示W(wǎng)rapper構(gòu)建部分// 2. 姓名為黃姓,且年齡大于等于20,小于等于40,且email字段不為空 // name like '黃%' AND age BETWEEN 20 AND 40 AND email is not null wrapper.likeRight("name","黃").between("age", 20, 40).isNotNull("email");// 3. 姓名為黃姓,或者年齡大于等于40,按照年齡降序排列,年齡相同則按照id升序排列 // name like '黃%' OR age >= 40 order by age desc, id asc wrapper.likeRight("name","黃").or().ge("age",40).orderByDesc("age").orderByAsc("id");// 4.創(chuàng)建日期為2021年3月22日,并且直屬上級(jí)的名字為李姓 // date_format(create_time,'%Y-%m-%d') = '2021-03-22' AND manager_id IN (SELECT id FROM user WHERE name like '李%') wrapper.apply("date_format(create_time, '%Y-%m-%d') = {0}", "2021-03-22") // 建議采用{index}這種方式動(dòng)態(tài)傳參, 可防止SQL注入.inSql("manager_id", "SELECT id FROM user WHERE name like '李%'"); // 上面的apply, 也可以直接使用下面這種方式做字符串拼接,但當(dāng)這個(gè)日期是一個(gè)外部參數(shù)時(shí),這種方式有SQL注入的風(fēng)險(xiǎn) wrapper.apply("date_format(create_time, '%Y-%m-%d') = '2021-03-22'");// 5. 名字為王姓,并且(年齡小于40,或者郵箱不為空) // name like '王%' AND (age < 40 OR email is not null) wrapper.likeRight("name", "王").and(q -> q.lt("age", 40).or().isNotNull("email"));// 6. 名字為王姓,或者(年齡小于40并且年齡大于20并且郵箱不為空) // name like '王%' OR (age < 40 AND age > 20 AND email is not null) wrapper.likeRight("name", "王").or(q -> q.lt("age",40).gt("age",20).isNotNull("email"));// 7. (年齡小于40或者郵箱不為空) 并且名字為王姓 // (age < 40 OR email is not null) AND name like '王%' wrapper.nested(q -> q.lt("age", 40).or().isNotNull("email")).likeRight("name", "王");// 8. 年齡為30,31,34,35 // age IN (30,31,34,35) wrapper.in("age", Arrays.asList(30,31,34,35)); // 或 wrapper.inSql("age","30,31,34,35");// 9. 年齡為30,31,34,35, 返回滿(mǎn)足條件的第一條記錄 // age IN (30,31,34,35) LIMIT 1 wrapper.in("age", Arrays.asList(30,31,34,35)).last("LIMIT 1");// 10. 只選出id, name 列 (QueryWrapper 特有) // SELECT id, name FROM user; wrapper.select("id", "name");// 11. 選出id, name, age, email, 等同于排除 manager_id 和 create_time // 當(dāng)列特別多, 而只需要排除個(gè)別列時(shí), 采用上面的方式可能需要寫(xiě)很多個(gè)列, 可以采用重載的select方法,指定需要排除的列 wrapper.select(User.class, info -> {String columnName = info.getColumn();return !"create_time".equals(columnName) && !"manager_id".equals(columnName);});

    Condition

    ????條件構(gòu)造器的諸多方法中,均可以指定一個(gè)boolean類(lèi)型的參數(shù)condition,用來(lái)決定該條件是否加入最后生成的WHERE語(yǔ)句中,比如:

    String name = "黃"; // 假設(shè)name變量是一個(gè)外部傳入的參數(shù) QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.like(StringUtils.hasText(name), "name", name); // 僅當(dāng) StringUtils.hasText(name) 為 true 時(shí), 會(huì)拼接這個(gè)like語(yǔ)句到WHERE中 // 其實(shí)就是對(duì)下面代碼的簡(jiǎn)化 if (StringUtils.hasText(name)) {wrapper.like("name", name); }

    實(shí)體對(duì)象作為條件

    ????調(diào)用構(gòu)造函數(shù)創(chuàng)建一個(gè)Wrapper對(duì)象時(shí),可以傳入一個(gè)實(shí)體對(duì)象。后續(xù)使用這個(gè)Wrapper時(shí),會(huì)以實(shí)體對(duì)象中的非空屬性,構(gòu)建WHERE條件(默認(rèn)構(gòu)建等值匹配的WHERE條件,這個(gè)行為可以通過(guò)實(shí)體類(lèi)里各個(gè)字段上的@TableField注解中的condition屬性進(jìn)行改變)。

    @Test public void test3() {User user = new User();user.setName("黃主管");user.setAge(28);QueryWrapper<User> wrapper = new QueryWrapper<>(user);List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }

    執(zhí)行結(jié)果如下??梢钥吹?#xff0c;是根據(jù)實(shí)體對(duì)象中的非空屬性,進(jìn)行了等值匹配查詢(xún)。

    若希望針對(duì)某些屬性,改變等值匹配的行為,則可以在實(shí)體類(lèi)中用@TableField注解進(jìn)行配置,示例如下:

    package com.example.mp.po; import com.baomidou.mybatisplus.annotation.SqlCondition; import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; import java.time.LocalDateTime; @Data public class User {private Long id;@TableField(condition = SqlCondition.LIKE) // 配置該字段使用like進(jìn)行拼接private String name;private Integer age;private String email;private Long managerId;private LocalDateTime createTime; } @Test public void test3() {User user = new User();user.setName("黃");QueryWrapper<User> wrapper = new QueryWrapper<>(user);List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }

    從下圖得到的結(jié)果來(lái)看,對(duì)于實(shí)體對(duì)象中的name字段,采用了like進(jìn)行拼接

    @TableField中配置的condition屬性實(shí)則是一個(gè)字符串,SqlCondition類(lèi)中預(yù)定義了一些字符串以供選擇

    package com.baomidou.mybatisplus.annotation;public class SqlCondition {//下面的字符串中, %s 是占位符, 第一個(gè) %s 是列名, 第二個(gè) %s 是列的值public static final String EQUAL = "%s=#{%s}";public static final String NOT_EQUAL = "%s&lt;&gt;#{%s}";public static final String LIKE = "%s LIKE CONCAT('%%',#{%s},'%%')";public static final String LIKE_LEFT = "%s LIKE CONCAT('%%',#{%s})";public static final String LIKE_RIGHT = "%s LIKE CONCAT(#{%s},'%%')"; }

    SqlCondition中提供的配置比較有限,當(dāng)我們需要<或>等拼接方式,則需要自己定義。比如:

    package com.example.mp.po; import com.baomidou.mybatisplus.annotation.SqlCondition; import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; import java.time.LocalDateTime; @Data public class User {private Long id;@TableField(condition = SqlCondition.LIKE)private String name;@TableField(condition = "%s &gt; #{%s}") // 這里相當(dāng)于大于, 其中 &gt; 是字符實(shí)體private Integer age;private String email;private Long managerId;private LocalDateTime createTime; }

    測(cè)試:

    @Test public void test3() {User user = new User();user.setName("黃");user.setAge(30);QueryWrapper<User> wrapper = new QueryWrapper<>(user);List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }

    從下圖得到的結(jié)果,可以看出,name屬性是用like拼接的,而age屬性是用>拼接的

    allEq方法

    allEq方法傳入一個(gè)map,用來(lái)做等值匹配

    @Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();Map<String, Object> param = new HashMap<>();param.put("age", 40);param.put("name", "黃飛飛");wrapper.allEq(param);List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }


    當(dāng)allEq方法傳入的Map中有value為null的元素時(shí),默認(rèn)會(huì)設(shè)置為is null

    @Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();Map<String, Object> param = new HashMap<>();param.put("age", 40);param.put("name", null);wrapper.allEq(param);List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }


    若想忽略map中value為null的元素,可以在調(diào)用allEq時(shí),設(shè)置參數(shù)boolean null2IsNull為false

    @Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();Map<String, Object> param = new HashMap<>();param.put("age", 40);param.put("name", null);wrapper.allEq(param, false);List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }


    若想要在執(zhí)行allEq時(shí),過(guò)濾掉Map中的某些元素,可以調(diào)用allEq的重載方法allEq(BiPredicate<R, V> filter, Map<R, V> params)

    @Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();Map<String, Object> param = new HashMap<>();param.put("age", 40);param.put("name", "黃飛飛");wrapper.allEq((k,v) -> !"name".equals(k), param); // 過(guò)濾掉map中key為name的元素List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }

    lambda條件構(gòu)造器

    ????lambda條件構(gòu)造器,支持lambda表達(dá)式,可以不必像普通條件構(gòu)造器一樣,以字符串形式指定列名,它可以直接以實(shí)體類(lèi)的方法引用來(lái)指定列。示例如下:

    @Test public void testLambda() {LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();wrapper.like(User::getName, "黃").lt(User::getAge, 30);List<User> users = userMapper.selectList(wrapper);users.forEach(System.out::println); }


    像普通的條件構(gòu)造器,列名是用字符串的形式指定,無(wú)法在編譯期進(jìn)行列名合法性的檢查,這就不如lambda條件構(gòu)造器來(lái)的優(yōu)雅。

    另外,還有個(gè)鏈?zhǔn)絣ambda條件構(gòu)造器,使用示例如下:

    @Test public void testLambda() {LambdaQueryChainWrapper<User> chainWrapper = new LambdaQueryChainWrapper<>(userMapper);List<User> users = chainWrapper.like(User::getName, "黃").gt(User::getAge, 30).list();users.forEach(System.out::println); }

    4.更新操作

    上面介紹的都是查詢(xún)操作,現(xiàn)在來(lái)講更新和刪除操作。
    BaseMapper中提供了2個(gè)更新方法

    • updateById(T entity)
      根據(jù)入?yún)ntity的id(主鍵)進(jìn)行更新,對(duì)于entity中非空的屬性,會(huì)出現(xiàn)在UPDATE語(yǔ)句的SET后面,即entity中非空的屬性,會(huì)被更新到數(shù)據(jù)庫(kù),示例如下:
    @RunWith(SpringRunner.class) @SpringBootTest public class UpdateTest {@Autowiredprivate UserMapper userMapper;@Testpublic void testUpdate() {User user = new User();user.setId(2L);user.setAge(18);userMapper.updateById(user);} }

    • update(T entity, Wrapper wrapper)
      根據(jù)實(shí)體entity和條件構(gòu)造器wrapper進(jìn)行更新,示例如下:
    @Test public void testUpdate2() {User user = new User();user.setName("王三蛋");LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();wrapper.between(User::getAge, 26,31).likeRight(User::getName,"吳");userMapper.update(user, wrapper);}


    額外演示一下,把實(shí)體對(duì)象傳入Wrapper,即用實(shí)體對(duì)象構(gòu)造WHERE條件的案例:

    @Test public void testUpdate3() {User whereUser = new User();whereUser.setAge(40);whereUser.setName("王");LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>(whereUser);User user = new User();user.setEmail("share@baomidou.com");user.setManagerId(10L);userMapper.update(user, wrapper); }

    注意到我們的User類(lèi)中,對(duì)name屬性和age屬性進(jìn)行了如下的設(shè)置

    @Data public class User {private Long id;@TableField(condition = SqlCondition.LIKE)private String name;@TableField(condition = "%s &gt; #{%s}")private Integer age;private String email;private Long managerId;private LocalDateTime createTime; }


    再額外演示一下,鏈?zhǔn)絣ambda條件構(gòu)造器的使用

    @Test public void testUpdate5() {LambdaUpdateChainWrapper<User> wrapper = new LambdaUpdateChainWrapper<>(userMapper);wrapper.likeRight(User::getEmail, "share").like(User::getName, "飛飛").set(User::getEmail, "ff@baomidou.com").update(); }

    反思
    ????由于BaseMapper提供的2個(gè)更新方法都是傳入一個(gè)實(shí)體對(duì)象去執(zhí)行更新,這在需要更新的列比較多時(shí)還好,若想要更新的只有那么一列,或者兩列,則創(chuàng)建一個(gè)實(shí)體對(duì)象就顯得有點(diǎn)麻煩。針對(duì)這種情況,UpdateWrapper提供有set方法,可以手動(dòng)拼接SQL中的SET語(yǔ)句,此時(shí)可以不必傳入實(shí)體對(duì)象,示例如下:

    @Test public void testUpdate4() {LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();wrapper.likeRight(User::getEmail, "share").set(User::getManagerId, 9L);userMapper.update(null, wrapper); }

    5.刪除操作

    BaseMapper一共提供了如下幾個(gè)用于刪除的方法

    • deleteById 根據(jù)主鍵id進(jìn)行刪除
    • deleteBatchIds 根據(jù)主鍵id進(jìn)行批量刪除
    • deleteByMap 根據(jù)Map進(jìn)行刪除(Map中的key為列名,value為值,根據(jù)列和值進(jìn)行等值匹配)
    • delete(Wrapper wrapper) 根據(jù)條件構(gòu)造器Wrapper進(jìn)行刪除

    6.自定義SQL

    當(dāng)mp提供的方法還不能滿(mǎn)足需求時(shí),可以使用mp提供的Wrapper條件構(gòu)造器,來(lái)自定義SQL。

    • 注解方式
    package com.example.mp.mappers; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.example.mp.po.User; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List;public interface UserMapper extends BaseMapper<User> {// SQL中不寫(xiě)WHERE關(guān)鍵字,且固定使用${ew.customSqlSegment}@Select("select * from user ${ew.customSqlSegment}")List<User> findAll(@Param(Constants.WRAPPER)Wrapper<User> wrapper); }
    • xml方式
    package com.example.mp.mappers; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.mp.po.User; import java.util.List;public interface UserMapper extends BaseMapper<User> {List<User> findAll(Wrapper<User> wrapper); } <!-- UserMapper.xml --> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mp.mappers.UserMapper"><select id="findAll" resultType="com.example.mp.po.User">SELECT * FROM user ${ew.customSqlSegment}</select> </mapper>

    分頁(yè)查詢(xún)

    BaseMapper中提供了2個(gè)方法進(jìn)行分頁(yè)查詢(xún),分別是selectPage和selectMapsPage,前者會(huì)將查詢(xún)的結(jié)果封裝成Java實(shí)體對(duì)象,后者會(huì)封裝成Map<String,Object>。分頁(yè)查詢(xún)的使用示例如下:

    1.創(chuàng)建mp的分頁(yè)攔截器,注冊(cè)到Spring容器中

    package com.example.mp.config; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class MybatisPlusConfig {/** 新版mp **/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}/** 舊版mp 用 PaginationInterceptor **/ }

    2.執(zhí)行分頁(yè)查詢(xún)

    @Test public void testPage() {LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();wrapper.ge(User::getAge, 28);// 設(shè)置分頁(yè)信息, 查第3頁(yè), 每頁(yè)2條數(shù)據(jù)Page<User> page = new Page<>(3, 2);// 執(zhí)行分頁(yè)查詢(xún)Page<User> userPage = userMapper.selectPage(page, wrapper);System.out.println("總記錄數(shù) = " + userPage.getTotal());System.out.println("總頁(yè)數(shù) = " + userPage.getPages());System.out.println("當(dāng)前頁(yè)碼 = " + userPage.getCurrent());// 獲取分頁(yè)查詢(xún)結(jié)果List<User> records = userPage.getRecords();records.forEach(System.out::println); }

    3.其他

    ????注意到,分頁(yè)查詢(xún)總共發(fā)出了2次SQL,一次查總記錄數(shù),一次查具體數(shù)據(jù)。若希望不查總記錄數(shù),僅查分頁(yè)結(jié)果??梢酝ㄟ^(guò)Page的重載構(gòu)造函數(shù),指定isSearchCount為false即可

    public Page(long current, long size, boolean isSearchCount)

    ????在實(shí)際開(kāi)發(fā)中,可能遇到多表聯(lián)查的場(chǎng)景,此時(shí)BaseMapper中提供的單表分頁(yè)查詢(xún)的方法無(wú)法滿(mǎn)足需求,需要自定義SQL,示例如下(使用單表查詢(xún)的SQL進(jìn)行演示,實(shí)際進(jìn)行多表聯(lián)查時(shí),修改SQL語(yǔ)句即可)

    • 在mapper接口中定義一個(gè)函數(shù),接收一個(gè)Page對(duì)象為參數(shù),并編寫(xiě)自定義SQL
    // 這里采用純注解方式。當(dāng)然,若SQL比較復(fù)雜,建議還是采用XML的方式 @Select("SELECT * FROM user ${ew.customSqlSegment}") Page<User> selectUserPage(Page<User> page, @Param(Constants.WRAPPER) Wrapper<User> wrapper); @Test public void testPage2() {LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();wrapper.ge(User::getAge, 28).likeRight(User::getName, "王");Page<User> page = new Page<>(3,2);Page<User> userPage = userMapper.selectUserPage(page, wrapper);System.out.println("總記錄數(shù) = " + userPage.getTotal());System.out.println("總頁(yè)數(shù) = " + userPage.getPages());userPage.getRecords().forEach(System.out::println); }

    7.AR模式

    ActiveRecord模式,通過(guò)操作實(shí)體對(duì)象,直接操作數(shù)據(jù)庫(kù)表。與ORM有點(diǎn)類(lèi)似。

    • 讓實(shí)體類(lèi)User繼承自Model
    package com.example.mp.po;import com.baomidou.mybatisplus.annotation.SqlCondition; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.extension.activerecord.Model; import lombok.Data; import lombok.EqualsAndHashCode; import java.time.LocalDateTime;@EqualsAndHashCode(callSuper = false) @Data public class User extends Model<User> {private Long id;@TableField(condition = SqlCondition.LIKE)private String name;@TableField(condition = "%s &gt; #{%s}")private Integer age;private String email;private Long managerId;private LocalDateTime createTime; }
    • 直接調(diào)用實(shí)體對(duì)象上的方法
    @Test public void insertAr() {User user = new User();user.setId(15L);user.setName("我是AR豬");user.setAge(1);user.setEmail("ar@baomidou.com");user.setManagerId(1L);boolean success = user.insert(); // 插入System.out.println(success); }

    8.主鍵策略

    ????在定義實(shí)體類(lèi)時(shí),用@TableId指定主鍵,而其type屬性,可以指定主鍵策略。

    ????mp支持多種主鍵策略,默認(rèn)的策略是基于雪花算法的自增id。全部主鍵策略定義在了枚舉類(lèi)IdType中,IdType有如下的取值。

    • AUTO
      數(shù)據(jù)庫(kù)ID自增,依賴(lài)于數(shù)據(jù)庫(kù)。在插入操作生成SQL語(yǔ)句時(shí),不會(huì)插入主鍵這一列

    • NONE
      未設(shè)置主鍵類(lèi)型。若在代碼中沒(méi)有手動(dòng)設(shè)置主鍵,則會(huì)根據(jù)主鍵的全局策略自動(dòng)生成(默認(rèn)的主鍵全局策略是基于雪花算法的自增ID)

    • INPUT
      需要手動(dòng)設(shè)置主鍵,若不設(shè)置。插入操作生成SQL語(yǔ)句時(shí),主鍵這一列的值會(huì)是null。oracle的序列主鍵需要使用這種方式

    • ASSIGN_ID
      當(dāng)沒(méi)有手動(dòng)設(shè)置主鍵,即實(shí)體類(lèi)中的主鍵屬性為空時(shí),才會(huì)自動(dòng)填充,使用雪花算法

    • ASSIGN_UUID
      當(dāng)實(shí)體類(lèi)的主鍵屬性為空時(shí),才會(huì)自動(dòng)填充,使用UUID

    ????可以針對(duì)每個(gè)實(shí)體類(lèi),使用@TableId注解指定該實(shí)體類(lèi)的主鍵策略,這可以理解為局部策略。若希望對(duì)所有的實(shí)體類(lèi),都采用同一種主鍵策略,挨個(gè)在每個(gè)實(shí)體類(lèi)上進(jìn)行配置,則太麻煩了,此時(shí)可以用主鍵的全局策略。只需要在application.yml進(jìn)行配置即可。比如,配置了全局采用自增主鍵策略。

    # application.yml mybatis-plus:global-config:db-config:id-type: auto

    下面對(duì)不同主鍵策略的行為進(jìn)行演示:

    • AUTO
      在User上對(duì)id屬性加上注解,然后將MYSQL的user表修改其主鍵為自增。
    @EqualsAndHashCode(callSuper = false) @Data public class User extends Model<User> {@TableId(type = IdType.AUTO)private Long id;@TableField(condition = SqlCondition.LIKE)private String name;@TableField(condition = "%s &gt; #{%s}")private Integer age;private String email;private Long managerId;private LocalDateTime createTime; }
    • NONE
      在MYSQL的user表中,去掉主鍵自增。然后修改User類(lèi)(若不配置@TableId注解,默認(rèn)主鍵策略也是NONE)
    @TableId(type = IdType.NONE) private Long id;

    插入時(shí),若實(shí)體類(lèi)的主鍵ID有值,則使用之;若主鍵ID為空,則使用主鍵全局策略,來(lái)生成一個(gè)ID。

    小結(jié)
    ????AUTO依賴(lài)于數(shù)據(jù)庫(kù)的自增主鍵,插入時(shí),實(shí)體對(duì)象無(wú)需設(shè)置主鍵,插入成功后,主鍵會(huì)被寫(xiě)回實(shí)體對(duì)象。
    ????INPUT完全依賴(lài)于用戶(hù)輸入。實(shí)體對(duì)象中主鍵ID是什么,插入到數(shù)據(jù)庫(kù)時(shí)就設(shè)置什么。若有值便設(shè)置值,若為null則設(shè)置null。
    ????其余的幾個(gè)策略,都是在實(shí)體對(duì)象中主鍵ID為空時(shí),才會(huì)自動(dòng)生成。
    ????NONE會(huì)跟隨全局策略,ASSIGN_ID采用雪花算法,ASSIGN_UUID采用UUID。
    ????全局配置,在application.yml中進(jìn)行即可;針對(duì)單個(gè)實(shí)體類(lèi)的局部配置,使用**@TableId**即可。對(duì)于某個(gè)實(shí)體類(lèi),若它有局部主鍵策略,則采用之,否則,跟隨全局策略。

    配置

    mybatis plus有許多可配置項(xiàng),可在application.yml中進(jìn)行配置,如上面的全局主鍵策略。下面列舉部分配置項(xiàng)。

    基本配置

    • configLocation:若有單獨(dú)的mybatis配置,用這個(gè)注解指定mybatis的配置文件(mybatis的全局配置文件)
    • mapperLocations:mybatis mapper所對(duì)應(yīng)的xml文件的位置
    • typeAliasesPackage:mybatis的別名包掃描路徑

    進(jìn)階配置

    • mapUnderscoreToCamelCase:是否開(kāi)啟自動(dòng)駝峰命名規(guī)則映射。(默認(rèn)開(kāi)啟)
    • dbTpe:數(shù)據(jù)庫(kù)類(lèi)型。一般不用配,會(huì)根據(jù)數(shù)據(jù)庫(kù)連接url自動(dòng)識(shí)別
    • fieldStrategy:(已過(guò)時(shí))字段驗(yàn)證策略。該配置項(xiàng)在最新版的mp文檔中已經(jīng)找不到了,被細(xì)分成了insertStrategy,updateStrategy,selectStrategy。默認(rèn)值是NOT_NULL,即對(duì)于實(shí)體對(duì)象中非空的字段,才會(huì)組裝到最終的SQL語(yǔ)句中。

    有如下幾種可選配置

    • IGNORED:忽略校驗(yàn)。即,不做校驗(yàn)。實(shí)體對(duì)象中的全部字段,無(wú)論值是什么,都如實(shí)地被組裝到SQL語(yǔ)句中(為NULL的字段在SQL語(yǔ)句中就組裝為NULL)
    • NOT_NULL:非NULL校驗(yàn)。只會(huì)將非NULL的字段組裝到SQL語(yǔ)句中
    • NOT_EMPTY:非空校驗(yàn)。當(dāng)有字段是字符串類(lèi)型時(shí),只組裝非空字符串;對(duì)其他類(lèi)型的字段,等同于NOT_NULL
    • NEVER:不加入SQL。所有字段不加入到SQL語(yǔ)句

    ????這個(gè)配置項(xiàng),可在application.yml中進(jìn)行全局配置,也可以在某一實(shí)體類(lèi)中,對(duì)某一字段用@TableField注解進(jìn)行局部配置。

    ????這個(gè)字段驗(yàn)證策略有什么用呢?在UPDATE操作中能夠體現(xiàn)出來(lái),若用一個(gè)User對(duì)象執(zhí)行UPDATE操作,我們希望只對(duì)User對(duì)象中非空的屬性,更新到數(shù)據(jù)庫(kù)中,其他屬性不做更新,則NOT_NULL可以滿(mǎn)足需求。而若updateStrategy配置為IGNORED,則不會(huì)進(jìn)行非空判斷,會(huì)將實(shí)體對(duì)象中的全部屬性如實(shí)組裝到SQL中,這樣,執(zhí)行UPDATE時(shí),可能就將一些不想更新的字段,設(shè)置為了NULL。

    • tablePrefix:添加表名前綴
      比如
    mybatis-plus:global-config:db-config:table-prefix: xx_

    然后將MYSQL中的表做一下修改。但Java實(shí)體類(lèi)保持不變(仍然為User)。

    @Test public void test3() {QueryWrapper<User> wrapper = new QueryWrapper<>();wrapper.like("name", "黃");Integer count = userMapper.selectCount(wrapper);System.out.println(count); }

    可以看到拼接出來(lái)的SQL,在表名前面添加了前綴

    9.代碼生成器

    mp提供一個(gè)生成器,可快速生成Entity實(shí)體類(lèi),Mapper接口,Service,Controller等全套代碼。

    示例如下:

    public class GeneratorTest {@Testpublic void generate() {AutoGenerator generator = new AutoGenerator();// 全局配置GlobalConfig config = new GlobalConfig();String projectPath = System.getProperty("user.dir");// 設(shè)置輸出到的目錄config.setOutputDir(projectPath + "/src/main/java");config.setAuthor("yogurt");// 生成結(jié)束后是否打開(kāi)文件夾config.setOpen(false);// 全局配置添加到 generator 上generator.setGlobalConfig(config);// 數(shù)據(jù)源配置DataSourceConfig dataSourceConfig = new DataSourceConfig();dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/yogurt?serverTimezone=Asia/Shanghai");dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");dataSourceConfig.setUsername("root");dataSourceConfig.setPassword("root");// 數(shù)據(jù)源配置添加到 generatorgenerator.setDataSource(dataSourceConfig);// 包配置, 生成的代碼放在哪個(gè)包下PackageConfig packageConfig = new PackageConfig();packageConfig.setParent("com.example.mp.generator");// 包配置添加到 generatorgenerator.setPackageInfo(packageConfig);// 策略配置StrategyConfig strategyConfig = new StrategyConfig();// 下劃線駝峰命名轉(zhuǎn)換strategyConfig.setNaming(NamingStrategy.underline_to_camel);strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);// 開(kāi)啟lombokstrategyConfig.setEntityLombokModel(true);// 開(kāi)啟RestControllerstrategyConfig.setRestControllerStyle(true);generator.setStrategy(strategyConfig);generator.setTemplateEngine(new FreemarkerTemplateEngine());// 開(kāi)始生成generator.execute();} }

    10.高級(jí)功能

    邏輯刪除

    ????首先,為什么要有邏輯刪除呢?直接刪掉不行嗎?當(dāng)然可以,但日后若想要恢復(fù),或者需要查看這些數(shù)據(jù),就做不到了。邏輯刪除是為了方便數(shù)據(jù)恢復(fù),和保護(hù)數(shù)據(jù)本身價(jià)值的一種方案。
    ????日常中,我們?cè)陔娔X中刪除一個(gè)文件后,也僅僅是把該文件放入了回收站,日后若有需要還能進(jìn)行查看或恢復(fù)。當(dāng)我們確定不再需要某個(gè)文件,可以將其從回收站中徹底刪除。這也是類(lèi)似的道理。
    ????mp提供的邏輯刪除實(shí)現(xiàn)起來(lái)非常簡(jiǎn)單,只需要在application.yml中進(jìn)行邏輯刪除的相關(guān)配置即可。

    mybatis-plus:global-config:db-config:logic-delete-field: deleted # 全局邏輯刪除的實(shí)體字段名logic-delete-value: 1 # 邏輯已刪除值(默認(rèn)為1)logic-not-delete-value: 0 # 邏輯未刪除值(默認(rèn)為0)# 若邏輯已刪除和未刪除的值和默認(rèn)值一樣,則可以不配置這2項(xiàng)

    測(cè)試:

    package com.example.mp; import com.example.mp.mappers.User2Mapper; import com.example.mp.po.User2; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class LogicDeleteTest {@Autowiredprivate User2Mapper mapper;@Testpublic void testLogicDel() {int i = mapper.deleteById(6);System.out.println("rowAffected = " + i);} }

    結(jié)果:

    可以看到,發(fā)出的SQL不再是DELETE,而是UPDATE。

    若想要SELECT的列,不包括邏輯刪除的那一列,則可以在實(shí)體類(lèi)中通過(guò)@TableField進(jìn)行配置

    @TableField(select = false) private Integer deleted;

    可以看到下圖的執(zhí)行結(jié)果中,SELECT中已經(jīng)不包含deleted這一列了

    前面在application.yml中做的配置,是全局的。通常來(lái)說(shuō),對(duì)于多個(gè)表,我們也會(huì)統(tǒng)一邏輯刪除字段的名稱(chēng),統(tǒng)一邏輯已刪除和未刪除的值,所以全局配置即可。當(dāng)然,若要對(duì)某些表進(jìn)行單獨(dú)配置,在實(shí)體類(lèi)的對(duì)應(yīng)字段上使用@TableLogic即可

    @TableLogic(value = "0", delval = "1") private Integer deleted;

    小結(jié)
    開(kāi)啟mp的邏輯刪除后,會(huì)對(duì)SQL產(chǎn)生如下的影響

    • INSERT語(yǔ)句:沒(méi)有影響
    • SELECT語(yǔ)句:追加WHERE條件,過(guò)濾掉已刪除的數(shù)據(jù)
    • UPDATE語(yǔ)句:追加WHERE條件,防止更新到已刪除的數(shù)據(jù)
    • DELETE語(yǔ)句:轉(zhuǎn)變?yōu)閁PDATE語(yǔ)句

    注意,上述的影響,只針對(duì)mp自動(dòng)注入的SQL生效,如果是自己手動(dòng)添加的自定義SQL,則不會(huì)生效。比如

    public interface User2Mapper extends BaseMapper<User2> {@Select("select * from user2")List<User2> selectRaw(); }

    調(diào)用這個(gè)selectRaw,則mp的邏輯刪除不會(huì)生效。

    另,邏輯刪除可在application.yml中進(jìn)行全局配置,也可在實(shí)體類(lèi)中用@TableLogic進(jìn)行局部配置。

    自動(dòng)填充

    ????表中常常會(huì)有“新增時(shí)間”,“修改時(shí)間”,“操作人” 等字段。比較原始的方式,是每次插入或更新時(shí),手動(dòng)進(jìn)行設(shè)置。mp可以通過(guò)配置,對(duì)某些字段進(jìn)行自動(dòng)填充,示例如下

  • 在實(shí)體類(lèi)中的某些字段上,通過(guò)@TableField設(shè)置自動(dòng)填充
  • public class User2 {private Long id;private String name;private Integer age;private String email;private Long managerId;@TableField(fill = FieldFill.INSERT) // 插入時(shí)自動(dòng)填充private LocalDateTime createTime;@TableField(fill = FieldFill.UPDATE) // 更新時(shí)自動(dòng)填充private LocalDateTime updateTime;private Integer version;private Integer deleted; }
  • 實(shí)現(xiàn)自動(dòng)填充處理器
  • package com.example.mp.component; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import org.apache.ibatis.reflection.MetaObject; import org.springframework.stereotype.Component; import java.time.LocalDateTime;@Component //需要注冊(cè)到Spring容器中 public class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {// 插入時(shí)自動(dòng)填充// 注意第二個(gè)參數(shù)要填寫(xiě)實(shí)體類(lèi)中的字段名稱(chēng),而不是表的列名稱(chēng)strictFillStrategy(metaObject, "createTime", LocalDateTime::now);}@Overridepublic void updateFill(MetaObject metaObject) {// 更新時(shí)自動(dòng)填充strictFillStrategy(metaObject, "updateTime", LocalDateTime::now);} }

    測(cè)試:

    @Test public void test() {User2 user = new User2();user.setId(8L);user.setName("王一蛋");user.setAge(29);user.setEmail("yd@baomidou.com");user.setManagerId(2L);mapper.insert(user); }

    根據(jù)下圖結(jié)果,可以看到對(duì)createTime進(jìn)行了自動(dòng)填充

    注意,自動(dòng)填充僅在該字段為空時(shí)會(huì)生效,若該字段不為空,則直接使用已有的值。如下

    @Test public void test() {User2 user = new User2();user.setId(8L);user.setName("王一蛋");user.setAge(29);user.setEmail("yd@baomidou.com");user.setManagerId(2L);user.setCreateTime(LocalDateTime.of(2000,1,1,8,0,0));mapper.insert(user); }


    更新時(shí)的自動(dòng)填充,測(cè)試如下:

    @Test public void test() {User2 user = new User2();user.setId(8L);user.setName("王一蛋");user.setAge(99);mapper.updateById(user); }

    樂(lè)觀鎖插件

    ????當(dāng)出現(xiàn)并發(fā)操作時(shí),需要確保各個(gè)用戶(hù)對(duì)數(shù)據(jù)的操作不產(chǎn)生沖突,此時(shí)需要一種并發(fā)控制手段。悲觀鎖的方法是,在對(duì)數(shù)據(jù)庫(kù)的一條記錄進(jìn)行修改時(shí),先直接加鎖(數(shù)據(jù)庫(kù)的鎖機(jī)制),鎖定這條數(shù)據(jù),然后再進(jìn)行操作;而樂(lè)觀鎖,正如其名,它先假設(shè)不存在沖突情況,而在實(shí)際進(jìn)行數(shù)據(jù)操作時(shí),再檢查是否沖突。樂(lè)觀鎖的一種通常實(shí)現(xiàn)是版本號(hào),在MySQL中也有名為MVCC的基于版本號(hào)的并發(fā)事務(wù)控制。
    ????在讀多寫(xiě)少的場(chǎng)景下,樂(lè)觀鎖比較適用,能夠減少加鎖操作導(dǎo)致的性能開(kāi)銷(xiāo),提高系統(tǒng)吞吐量。
    ????在寫(xiě)多讀少的場(chǎng)景下,悲觀鎖比較使用,否則會(huì)因?yàn)闃?lè)觀鎖不斷失敗重試,反而導(dǎo)致性能下降。

    樂(lè)觀鎖的實(shí)現(xiàn)如下:

  • 取出記錄時(shí),獲取當(dāng)前version
  • 更新時(shí),帶上這個(gè)version
  • 執(zhí)行更新時(shí), set version = newVersion where version = oldVersion
  • 如果oldVersion與數(shù)據(jù)庫(kù)中的version不一致,就更新失敗
  • 這種思想和CAS(Compare And Swap)非常相似。

    樂(lè)觀鎖的實(shí)現(xiàn)步驟如下:

  • 配置樂(lè)觀鎖插件
  • package com.example.mp.config;import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class MybatisPlusConfig {/** 3.4.0以后的mp版本,推薦用如下的配置方式 **/@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return interceptor;}/** 舊版mp可以采用如下方式。注意新舊版本中,新版的類(lèi),名稱(chēng)帶有Inner, 舊版的不帶, 不要配錯(cuò)了 **//*@Beanpublic OptimisticLockerInterceptor opLocker() {return new OptimisticLockerInterceptor();}*/ }
  • 在實(shí)體類(lèi)中表示版本的字段上添加注解@Version
  • @Data public class User2 {private Long id;private String name;private Integer age;private String email;private Long managerId;private LocalDateTime createTime;private LocalDateTime updateTime;@Versionprivate Integer version;private Integer deleted; }

    測(cè)試代碼

    @Test public void testOpLocker() {int version = 1; // 假設(shè)這個(gè)version是先前查詢(xún)時(shí)獲得的User2 user = new User2();user.setId(8L);user.setEmail("version@baomidou.com");user.setVersion(version);int i = mapper.updateById(user); }

    執(zhí)行之前先看一下數(shù)據(jù)庫(kù)的情況

    根據(jù)下圖執(zhí)行結(jié)果,可以看到SQL語(yǔ)句中添加了version相關(guān)的操作

    當(dāng)UPDATE返回了1,表示影響行數(shù)為1,則更新成功。反之,由于WHERE后面的version與數(shù)據(jù)庫(kù)中的不一致,匹配不到任何記錄,則影響行數(shù)為0,表示更新失敗。更新成功后,新的version會(huì)被封裝回實(shí)體對(duì)象中。

    實(shí)體類(lèi)中version字段,類(lèi)型只支持int,long,Date,Timestamp,LocalDateTime

    注意,樂(lè)觀鎖插件僅支持updateById(id)與update(entity, wrapper)方法

    注意:如果使用wrapper,則wrapper不能復(fù)用!

    錯(cuò)誤示例

    @Test public void testOpLocker() {User2 user = new User2();user.setId(8L);user.setVersion(1);user.setAge(2);// 第一次使用LambdaQueryWrapper<User2> wrapper = new LambdaQueryWrapper<>();wrapper.eq(User2::getName, "王一蛋");mapper.update(user, wrapper);// 第二次復(fù)用user.setAge(3);mapper.update(user, wrapper); }


    可以看到在第二次復(fù)用wrapper時(shí),拼接出的SQL中,后面WHERE語(yǔ)句中出現(xiàn)了2次version,是有問(wèn)題的。

    性能分析插件

    該插件會(huì)輸出SQL語(yǔ)句的執(zhí)行時(shí)間,以便做SQL語(yǔ)句的性能分析和調(diào)優(yōu)。
    使用步驟:

  • 引入maven依賴(lài)
  • <dependency><groupId>p6spy</groupId><artifactId>p6spy</artifactId><version>3.9.1</version> </dependency>
  • 修改application.yml
  • spring:datasource:driver-class-name: com.p6spy.engine.spy.P6SpyDriver #換成p6spy的驅(qū)動(dòng)url: jdbc:p6spy:mysql://localhost:3306/yogurt?serverTimezone=Asia/Shanghai #url修改username: rootpassword: root
  • 在src/main/resources資源目錄下添加spy.properties
  • #spy.properties #3.2.1以上使用 modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory,com.p6spy.engine.outage.P6OutageFactory # 真實(shí)JDBC driver , 多個(gè)以逗號(hào)分割,默認(rèn)為空。由于上面設(shè)置了modulelist, 這里可以不用設(shè)置driverlist #driverlist=com.mysql.cj.jdbc.Driver # 自定義日志打印 logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger #日志輸出到控制臺(tái) appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger #若要日志輸出到文件, 把上面的appnder注釋掉, 或者采用下面的appender, 再添加logfile配置 #不配置appender時(shí), 默認(rèn)是往文件進(jìn)行輸出的 #appender=com.p6spy.engine.spy.appender.FileLogger #logfile=log.log # 設(shè)置 p6spy driver 代理 deregisterdrivers=true # 取消JDBC URL前綴 useprefix=true # 配置記錄 Log 例外,可去掉的結(jié)果集有error,info,batch,debug,statement,commit,rollback,result,resultset. excludecategories=info,debug,result,commit,resultset # 日期格式 dateformat=yyyy-MM-dd HH:mm:ss # 是否開(kāi)啟慢SQL記錄 outagedetection=true # 慢SQL記錄標(biāo)準(zhǔn) 2 秒 outagedetectioninterval=2 # 執(zhí)行時(shí)間設(shè)置, 只有超過(guò)這個(gè)執(zhí)行時(shí)間的才進(jìn)行記錄, 默認(rèn)值0, 單位毫秒 executionThreshold=10

    隨便運(yùn)行一個(gè)測(cè)試用例,可以看到該SQL的執(zhí)行時(shí)長(zhǎng)被記錄了下來(lái)

    多租戶(hù)SQL解析器

    多租戶(hù)的概念:多個(gè)用戶(hù)共用一套系統(tǒng),但他們的數(shù)據(jù)有需要相對(duì)的獨(dú)立,保持一定的隔離性。
    多租戶(hù)的數(shù)據(jù)隔離一般有如下的方式:

    • 不同租戶(hù)使用不同的數(shù)據(jù)庫(kù)服務(wù)器
      優(yōu)點(diǎn):不同租戶(hù)有不同的獨(dú)立數(shù)據(jù)庫(kù),有助于擴(kuò)展,以及對(duì)不同租戶(hù)提供更好的個(gè)性化,出現(xiàn)故障時(shí)恢復(fù)數(shù)據(jù)較為簡(jiǎn)單。
      缺點(diǎn):增加了數(shù)據(jù)庫(kù)數(shù)量,購(gòu)置成本,維護(hù)成本更高。
    • 不同租戶(hù)使用相同的數(shù)據(jù)庫(kù)服務(wù)器,但使用不同的數(shù)據(jù)庫(kù)(不同的schema)
      優(yōu)點(diǎn):購(gòu)置和維護(hù)成本低了一些,缺點(diǎn)是數(shù)據(jù)恢復(fù)較為困難,因?yàn)椴煌鈶?hù)的數(shù)據(jù)都放在了一起。
    • 不同租戶(hù)使用相同的數(shù)據(jù)庫(kù)服務(wù)器,使用相同的數(shù)據(jù)庫(kù),共享數(shù)據(jù)表,在表中增加租戶(hù)id來(lái)做區(qū)分
      優(yōu)點(diǎn):購(gòu)置和維護(hù)成本最低,支持用戶(hù)最多,缺點(diǎn)是隔離性最低,安全性最低。

    示例如下:
    添加多租戶(hù)攔截器配置。添加配置后,在執(zhí)行CRUD的時(shí)候,會(huì)自動(dòng)在SQL語(yǔ)句最后拼接租戶(hù)id的條件。

    package com.example.mp.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.LongValue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {@Overridepublic Expression getTenantId() {// 返回租戶(hù)id的值, 這里固定寫(xiě)死為1// 一般是從當(dāng)前上下文中取出一個(gè) 租戶(hù)idreturn new LongValue(1);}/**** 通常會(huì)將表示租戶(hù)id的列名,需要排除租戶(hù)id的表等信息,封裝到一個(gè)配置類(lèi)中(如TenantConfig)**/@Overridepublic String getTenantIdColumn() {// 返回表中的表示租戶(hù)id的列名return "manager_id";}@Overridepublic boolean ignoreTable(String tableName) {// 表名不為 user2 的表, 不拼接多租戶(hù)條件return !"user2".equals(tableName);}}));// 如果用了分頁(yè)插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor// 用了分頁(yè)插件必須設(shè)置 MybatisConfiguration#useDeprecatedExecutor = falsereturn interceptor;}}

    測(cè)試

    @Test public void testTenant() {LambdaQueryWrapper<User2> wrapper = new LambdaQueryWrapper<>();wrapper.likeRight(User2::getName, "王").select(User2::getName, User2::getAge, User2::getEmail, User2::getManagerId);user2Mapper.selectList(wrapper); }

    動(dòng)態(tài)表名SQL解析器

    ????當(dāng)數(shù)據(jù)量特別大的時(shí)候,我們通常會(huì)采用分庫(kù)分表。這時(shí),可能就會(huì)有多張表,其表結(jié)構(gòu)相同,但表名不同。例如order_1,order_2,order_3,查詢(xún)時(shí),我們可能需要?jiǎng)討B(tài)設(shè)置要查的表名。mp提供了動(dòng)態(tài)表名SQL解析器,示例如下:

    配置動(dòng)態(tài)表名攔截器

    package com.example.mp.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler; import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Random;@Configuration public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();HashMap<String, TableNameHandler> map = new HashMap<>();// 對(duì)于user2表,進(jìn)行動(dòng)態(tài)表名設(shè)置map.put("user2", (sql, tableName) -> {String _ = "_";int random = new Random().nextInt(2) + 1;return tableName + _ + random; // 若返回null, 則不會(huì)進(jìn)行動(dòng)態(tài)表名替換, 還是會(huì)使用user2});dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);return interceptor;}}

    測(cè)試

    @Test public void testDynamicTable() {user2Mapper.selectList(null); }

    結(jié)果

    四、總結(jié)

    • 條件構(gòu)造器AbstractWrapper中提供了多個(gè)方法用于構(gòu)造SQL語(yǔ)句中的WHERE條件,而其子類(lèi)QueryWrapper額外提供了select方法,可以只選取特定的列,子類(lèi)UpdateWrapper額外提供了set方法,用于設(shè)置SQL中的SET語(yǔ)句。除了普通的Wrapper,還有基于lambda表達(dá)式的Wrapper,如LambdaQueryWrapperLambdaUpdateWrapper,它們?cè)跇?gòu)造WHERE條件時(shí),直接以方法引用來(lái)指定WHERE條件中的列,比普通Wrapper通過(guò)字符串來(lái)指定要更加優(yōu)雅。另,還有鏈?zhǔn)絎rapper,如LambdaQueryChainWrapper,它封裝了BaseMapper,可以更方便地獲取結(jié)果。
    • 條件構(gòu)造器采用鏈?zhǔn)秸{(diào)用來(lái)拼接多個(gè)條件,條件之間默認(rèn)以AND連接。
    • 當(dāng)ANDOR后面的條件需要被括號(hào)包裹時(shí),將括號(hào)中的條件以lambda表達(dá)式形式,作為參數(shù)傳入and()或or()。
      特別的,當(dāng)()需要放在WHERE語(yǔ)句的最開(kāi)頭時(shí),可以使用**nested()**方法。
    • 條件表達(dá)式時(shí)當(dāng)需要傳入自定義的SQL語(yǔ)句,或者需要調(diào)用數(shù)據(jù)庫(kù)函數(shù)時(shí),可用apply()方法進(jìn)行SQL拼接。
    • 條件構(gòu)造器中的各個(gè)方法可以通過(guò)一個(gè)boolean類(lèi)型的變量condition,來(lái)根據(jù)需要靈活拼接WHERE條件(僅當(dāng)condition為true時(shí)會(huì)拼接SQL語(yǔ)句)。
    • 使用lambda條件構(gòu)造器,可以通過(guò)lambda表達(dá)式,直接使用實(shí)體類(lèi)中的屬性進(jìn)行條件構(gòu)造,比普通的條件構(gòu)造器更加優(yōu)雅。
    • 若mp提供的方法不夠用,可以通過(guò)自定義SQL(原生mybatis)的形式進(jìn)行擴(kuò)展開(kāi)發(fā)。
    • 使用mp進(jìn)行分頁(yè)查詢(xún)時(shí),需要?jiǎng)?chuàng)建一個(gè)分頁(yè)攔截器(Interceptor),注冊(cè)到Spring容器中,隨后查詢(xún)時(shí),通過(guò)傳入一個(gè)分頁(yè)對(duì)象(Page對(duì)象)進(jìn)行查詢(xún)即可。單表查詢(xún)時(shí),可以使用BaseMapper提供的selectPageselectMapsPage方法。復(fù)雜場(chǎng)景下(如多表聯(lián)查),使用自定義SQL。
    • AR模式可以直接通過(guò)操作實(shí)體類(lèi)來(lái)操作數(shù)據(jù)庫(kù)。讓實(shí)體類(lèi)繼承自Model即可。

    總結(jié)

    以上是生活随笔為你收集整理的mybatis plus 使用技巧的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。