欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合mybatis-plus進(jìn)階詳細(xì)教程

 更新時(shí)間:2021年09月09日 12:33:46   作者:qq_44737094  
本文主要對(duì)mybatis-plus的條件構(gòu)造器、AR模式、插件、逆向工程、自定義全局操作、公共字段自動(dòng)填充等知識(shí)點(diǎn)進(jìn)行講解,需要的朋友參考下吧

前言

關(guān)于mybatis-plus的簡(jiǎn)介以及基本使用,我在《SpringBoot整合mybatis-plus–入門(mén)超詳細(xì)》一文中已做介紹,此處不再贅述。本文主要對(duì)mybatis-plus的條件構(gòu)造器、AR模式、插件、逆向工程、自定義全局操作、公共字段自動(dòng)填充等知識(shí)點(diǎn)進(jìn)行講解。

wapper介紹 :

Wrapper : 條件構(gòu)造抽象類(lèi),最頂端父類(lèi),抽象類(lèi)中提供4個(gè)方法西面貼源碼展示
AbstractWrapper : 用于查詢(xún)條件封裝,生成 sql 的 where 條件
AbstractLambdaWrapper : Lambda 語(yǔ)法使用 Wrapper統(tǒng)一處理解析 lambda 獲取 column。
LambdaQueryWrapper :看名稱(chēng)也能明白就是用于Lambda語(yǔ)法使用的查詢(xún)Wrapper
LambdaUpdateWrapper : Lambda 更新封裝Wrapper
QueryWrapper : Entity 對(duì)象封裝操作類(lèi),不是用lambda語(yǔ)法
UpdateWrapper : Update 條件封裝,用于Entity對(duì)象更新操作

條件構(gòu)造器 AbstractWrapper

上一節(jié)我們完成了基于mybatis-plus的CRUD操作,這一節(jié)我們來(lái)學(xué)習(xí)一下使用mybatis-plus中的條件構(gòu)造器——AbstractWrapper。

一、什么是AbstractWrapper

QueryWrapper(LambdaQueryWrapper)UpdateWrapper(LambdaUpdateWrapper)的父類(lèi)用于生成 sql 的 where 條件, entity 屬性也用于生成 sql 的 where 條件
注意: entity 生成的 where 條件與 使用各個(gè) api 生成的 where 條件沒(méi)有任何關(guān)聯(lián)行為

首先我們來(lái)介紹一下AbstractWrapper,下圖是AbstractWrapper的一個(gè)繼承結(jié)構(gòu):

在這里插入圖片描述

二、QueryWrapper(LambdaQueryWrapper)

由于其中方法太多就不一一贅述,想看全部方法用法可以看官網(wǎng)介紹或者看我的實(shí)例代碼

1、QueryWrapper用法示例

查詢(xún)id在1 - 30且性別為男、姓名帶有test的用戶(hù):

@SpringBootTest
@SuppressWarnings("unchecked")
public class UserWrapperTest {

    @Autowired
    UserMapper userMapper;
    @Test
    public void TestQueryWrapper(){
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name","test");
        userQueryWrapper.eq("sex","男");
        userQueryWrapper.between("id",1,30);
        userMapper.selectList(userQueryWrapper).forEach(System.out::println);
    }
}

:eq是equals的簡(jiǎn)寫(xiě),該方法兩個(gè)參數(shù),column和value,表示column的值和value要相等。
此處like方法就是查詢(xún)name中包含“test”字樣的記錄;between方法三個(gè)參數(shù),分別是column、value1、value2,該方法表示column的值要在value1和value2之間。
注意column是數(shù)據(jù)表對(duì)應(yīng)的字段,而非實(shí)體類(lèi)屬性字段。

運(yùn)行日志

在這里插入圖片描述

2、LambdaQueryWrapper用法示例

LambdaQueryWrapper是mybatis plus中的一個(gè)條件構(gòu)造器對(duì)象,只是是需要使用Lambda 語(yǔ)法使用 Wrapper

@Test
    public void TestLambdaQueryWrapper() {
        LambdaQueryWrapper<User> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.like(User::getName,"test");
        lambdaWrapper.eq(User::getSex,"男");
        lambdaWrapper.between(User::getId,1,30);
        userMapper.selectList(lambdaWrapper).forEach(System.out::println);
    }

運(yùn)行日志

在這里插入圖片描述

三、UpdateWrapper(LambdaUpdateWrapper)

我們?cè)龠M(jìn)行更新或者刪除的時(shí)候,有時(shí)候where條件復(fù)雜的話(huà),可以使用UpdateWrapper來(lái)構(gòu)造條件
他們有部分方法是和上面的QueryWrapper(LambdaQueryWrapper) 相同的。這里演示不同之處

1、UpdateWrapper用法示例

將名字包含test1的用戶(hù)名改為test1.1 :

 @Test
    public void TestUpdateWrapper() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        //繼承自 AbstractWrapper ,自身的內(nèi)部屬性 entity 也用于生成 where 條件
        //及 LambdaUpdateWrapper, 可以通過(guò) new UpdateWrapper().lambda() 方法獲取!
        User user = User.builder().build();
        //修改語(yǔ)句
        updateWrapper.set("name", "test1.1");
        //條件
        updateWrapper.like("name", "test1");
        userMapper.update(user, updateWrapper);
    }

運(yùn)行日志

在這里插入圖片描述

2、LambdaUpdateWrapper用法示例

 @Test
    public void TestLambdaUpdateWrapper() {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        //LambdaUpdateWrapper<User> updateWrapper = new UpdateWrapper<User>().lambda();
        User user = User.builder().build();
        //修改語(yǔ)句
        updateWrapper.set(User::getName, "test1.1");
        //條件
        updateWrapper.like(User::getName, "test1");
        userMapper.update(user, updateWrapper);
    }

在這里插入圖片描述

mybatis-plus的插件

mybatis-plus提供了很多好用的插件,而且配置簡(jiǎn)單,使用方便。接下來(lái)一起看看MP的插件如何使用。

一、分頁(yè)插件

BaseMapper的selectPage方法和AR提供的selectPage方法都不是物理分頁(yè),需要配置分頁(yè)插件后才是物理分頁(yè),那么現(xiàn)在就來(lái)看看如何配置這個(gè)插件。分頁(yè)查詢(xún)

1、配置分頁(yè)插件

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;

/**
 * @program: springboot
 * @description:
 * @author: King
 * @create: 2021-09-08 13:18
 */
@Configuration
public class MybatisPlusConfig {
    /**
     * 新的分頁(yè)插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問(wèn)題
     * 3.4.3不用設(shè)置
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}

這個(gè)配置摘抄自官方示例

2、測(cè)試代碼

@Autowired
    UserMapper userMapper;
    
   @Test
    public void Test2() {
        //配置了分頁(yè)插件后,使用selectpage方法,
        //但是現(xiàn)在就是真正的物理分頁(yè)了,sql語(yǔ)句中有l(wèi)imit了
        Page<User> page = new Page<>(1, 10);
        IPage<User> selectPage = userMapper.selectPage(page, null);
        System.out.println(selectPage);
        System.out.println("================= 相關(guān)的分頁(yè)信息 ==================");
        System.out.println("總條數(shù):" + selectPage.getTotal());
        System.out.println("當(dāng)前頁(yè)碼:" + selectPage.getCurrent());
        System.out.println("總頁(yè)數(shù):" + selectPage.getPages());
        System.out.println("每頁(yè)顯示條數(shù):" + selectPage.getSize());
        System.out.println("是否有上一頁(yè):" + page.hasPrevious());
        System.out.println("是否有下一頁(yè):" + page.hasNext());
        System.out.println("查詢(xún)結(jié)果:");
        List<User> list = selectPage.getRecords();
        list.forEach(o -> System.out.println(o));
//        page.getRecords().forEach(o -> System.out.println(o));
        //還可以將查詢(xún)到的結(jié)果set進(jìn)page對(duì)象中
        page.setRecords(list);
        //也可以通過(guò)page調(diào)用相關(guān)方法獲取到相關(guān)的分頁(yè)信息,而且還可以把查詢(xún)到的結(jié)果set回page對(duì)象中,方便前端使用。
    }

運(yùn)行結(jié)果

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@17aa8a11] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1963906615 wrapping com.mysql.cj.jdbc.ConnectionImpl@5190010f] will not be managed by Spring
==>  Preparing: SELECT COUNT(*) FROM User
==> Parameters:
<==    Columns: COUNT(*)
<==        Row: 31
<==      Total: 1
==>  Preparing: SELECT id,name,sex,pwd,email FROM User LIMIT ?
==> Parameters: 10(Long)
<==    Columns: id, name, sex, pwd, email
<==        Row: 2, test1.1, 女, aaaa, 1231@qq.com
<==        Row: 3, test2, 男, aaaa, 1232@qq.com
<==        Row: 4, test3, 女, aaaa, 1233@qq.com
<==        Row: 5, test4, 男, aaaa, 1234@qq.com
<==        Row: 6, test5, 女, aaaa, 1235@qq.com
<==        Row: 7, test6, 男, aaaa, 1236@qq.com
<==        Row: 8, test7, 女, aaaa, update@qq.com
<==        Row: 9, test8, 男, aaaaaa, 1238@qq.com
<==        Row: 42, test, 男, aaa, a@qq.com
<==        Row: 43, test, 男, aaa, a@qq.com
<==      Total: 10
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@17aa8a11]
com.baomidou.mybatisplus.extension.plugins.pagination.Page@44286963
================= 相關(guān)的分頁(yè)信息 ==================
總條數(shù):31
當(dāng)前頁(yè)碼:1
總頁(yè)數(shù):4
每頁(yè)顯示條數(shù):10
是否有上一頁(yè):false
是否有下一頁(yè):true
查詢(xún)結(jié)果:
User(id=2, name=test1.1, sex=女, pwd=aaaa, email=1231@qq.com)
User(id=3, name=test2, sex=男, pwd=aaaa, email=1232@qq.com)
User(id=4, name=test3, sex=女, pwd=aaaa, email=1233@qq.com)
User(id=5, name=test4, sex=男, pwd=aaaa, email=1234@qq.com)
User(id=6, name=test5, sex=女, pwd=aaaa, email=1235@qq.com)
User(id=7, name=test6, sex=男, pwd=aaaa, email=1236@qq.com)
User(id=8, name=test7, sex=女, pwd=aaaa, email=update@qq.com)
User(id=9, name=test8, sex=男, pwd=aaaaaa, email=1238@qq.com)
User(id=42, name=test, sex=男, pwd=aaa, email=a@qq.com)
User(id=43, name=test, sex=男, pwd=aaa, email=a@qq.com)

可以看到通過(guò)分頁(yè)插件 sql語(yǔ)句中已經(jīng)有了limit,是物理分頁(yè)了。并且是否有上一頁(yè)下一頁(yè)也有展示非常好用

3、自定義分頁(yè)條件

示例:
只需要在你的 UserMapper.java 中加入你自定義的分頁(yè)方法,也可以按你自定義方法實(shí)現(xiàn)分頁(yè)。UserMapper可以繼承或者不繼承BaseMapper

@Mapper
//表明這是一個(gè)Mapper,也可以在啟動(dòng)類(lèi)上加上包掃描
//Mapper 繼承該接口后,無(wú)需編寫(xiě) mapper.xml 文件,即可獲得CRUD功能
public interface UserMapper extends BaseMapper<User> {
    /**
     * 查詢(xún) : 根據(jù)性別查詢(xún)用戶(hù)列表,分頁(yè)顯示
     * @param page 分頁(yè)對(duì)象,xml中可以從里面進(jìn)行取值,傳遞參數(shù) Page 即自動(dòng)分頁(yè),必須放在第一位(你可以繼承Page實(shí)現(xiàn)自己的分頁(yè)對(duì)象)
     * @param sex 性別
     * @return 分頁(yè)對(duì)象
     */
    @Select("SELECT * FROM user WHERE sex = #{sex}")
    IPage<User> selectPageBySex(Page<?> page, String sex);
}

測(cè)試代碼

 @Test
    public void Test3() {
        Page<User> page = new Page<>(2, 10);
        IPage<User> selectPage = userMapper.selectPageBySex(page, "男");
        System.out.println("================= 相關(guān)的分頁(yè)信息 ==================");
        System.out.println("總條數(shù):" + selectPage.getTotal());
        System.out.println("當(dāng)前頁(yè)碼:" + selectPage.getCurrent());
        System.out.println("總頁(yè)數(shù):" + selectPage.getPages());
        System.out.println("每頁(yè)顯示條數(shù):" + selectPage.getSize());
        System.out.println("是否有上一頁(yè):" + page.hasPrevious());
        System.out.println("是否有下一頁(yè):" + page.hasNext());
        System.out.println("分頁(yè)結(jié)果");
        selectPage.getRecords().forEach(System.out::println);
    }

運(yùn)行結(jié)果

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6a0f2853] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1312762120 wrapping com.mysql.cj.jdbc.ConnectionImpl@7e87ef9e] will not be managed by Spring
==>  Preparing: SELECT COUNT(*) FROM user WHERE sex = ?
==> Parameters: 男(String)
<==    Columns: COUNT(*)
<==        Row: 17
<==      Total: 1
==>  Preparing: SELECT * FROM user WHERE sex = ? LIMIT ? OFFSET ?
==> Parameters: 男(String), 10(Long), 10(Long)
<==    Columns: id, name, sex, pwd, email
<==        Row: 52, test28, 男, aaaa, a28@qq.com
<==        Row: 54, test30, 男, aaaa, a30@qq.com
<==        Row: 56, test32, 男, aaaa, a32@qq.com
<==        Row: 58, test34, 男, aaaa, a34@qq.com
<==        Row: 60, test36, 男, aaaa, a36@qq.com
<==        Row: 62, test38, 男, aaaa, a38@qq.com
<==        Row: 64, Test, 男, aaaaa, 1234@qq.com
<==      Total: 7
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6a0f2853]
================= 相關(guān)的分頁(yè)信息 ==================
總條數(shù):17
當(dāng)前頁(yè)碼:2
總頁(yè)數(shù):2
每頁(yè)顯示條數(shù):10
是否有上一頁(yè):true
是否有下一頁(yè):false
分頁(yè)結(jié)果
User(id=52, name=test28, sex=男, pwd=aaaa, email=a28@qq.com)
User(id=54, name=test30, sex=男, pwd=aaaa, email=a30@qq.com)
User(id=56, name=test32, sex=男, pwd=aaaa, email=a32@qq.com)
User(id=58, name=test34, sex=男, pwd=aaaa, email=a34@qq.com)
User(id=60, name=test36, sex=男, pwd=aaaa, email=a36@qq.com)
User(id=62, name=test38, sex=男, pwd=aaaa, email=a38@qq.com)
User(id=64, name=Test, sex=男, pwd=aaaaa, email=1234@qq.com)

二、性能分析插件

這個(gè)插件PerformanceInterceptor在3.2.0被移除了,如果想進(jìn)行性能分析,用第三方的,官方這樣寫(xiě)的“該插件 3.2.0 以上版本移除推薦使用第三方擴(kuò)展 執(zhí)行 SQL 分析打印 功能

三、執(zhí)行分析插件

–防止全表更新與刪除

1、配置插件

和上面的分頁(yè)插件類(lèi)似

 //BlockAttackInnerInterceptor
    //針對(duì) update 和 delete 語(yǔ)句 作用: 阻止惡意的全表更新刪除
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor3() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 執(zhí)行分析插件
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
        return interceptor;
    }

2、測(cè)試代碼

 @Test
    public void Test4(){
        //條件為null,就是刪除全表,執(zhí)行分析插件會(huì)終止該操作
        userMapper.delete(null);
    }

3、執(zhí)行結(jié)果

運(yùn)行該junit測(cè)試,可以看到報(bào)如下錯(cuò)誤,說(shuō)明該插件生效了。

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e9f2c32] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@480645280 wrapping com.mysql.cj.jdbc.ConnectionImpl@49433c98] will not be managed by Spring
original SQL: DELETE FROM User
SQL to parse, SQL: DELETE FROM User
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e9f2c32]

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database.  Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of full table deletion
### The error may exist in com/king/mybatis_plus/mapper/UserMapper.java (best guess)
### The error may involve com.king.mybatis_plus.mapper.UserMapper.delete
### The error occurred while executing an update
### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of full table deletion

未完待續(xù)。。。

到此這篇關(guān)于SpringBoot整合mybatis-plus進(jìn)階詳細(xì)教程的文章就介紹到這了,更多相關(guān)SpringBoot整合mybatis-plus內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • idea報(bào)錯(cuò):java程序包c(diǎn)om.github.xiaoymin.knife4j.spring.annotations不存在問(wèn)題解決

    idea報(bào)錯(cuò):java程序包c(diǎn)om.github.xiaoymin.knife4j.spring.annotations

    這篇文章主要介紹了idea報(bào)錯(cuò):java程序包c(diǎn)om.github.xiaoymin.knife4j.spring.annotations不存在問(wèn)題解決,需要的朋友可以參考下
    2023-06-06
  • MyBatis中Mapper的注入問(wèn)題詳解

    MyBatis中Mapper的注入問(wèn)題詳解

    這篇文章主要介紹了MyBatis中Mapper的注入問(wèn)題,我知道在 SpringBoot 體系中,MyBatis 對(duì) Mapper 的注入常見(jiàn)的方式有 2 種,具體哪兩種方法跟隨小編一起看看吧
    2021-09-09
  • IDEA反編譯出整個(gè)jar包源碼

    IDEA反編譯出整個(gè)jar包源碼

    InteliJ IDEA默認(rèn)帶反編譯插件,那么如何把反編譯的jar包整體導(dǎo)出java源碼來(lái)?本文就來(lái)介紹一下,感興趣的可以了解下
    2021-05-05
  • AsyncHttpClient的ConnectionSemaphore方法源碼流程解讀

    AsyncHttpClient的ConnectionSemaphore方法源碼流程解讀

    這篇文章主要為大家介紹了AsyncHttpClient的ConnectionSemaphore方法源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Tomcat ClassLoader打破雙親委派源碼解析

    Tomcat ClassLoader打破雙親委派源碼解析

    這篇文章主要為大家介紹了Tomcat ClassLoader打破雙親委派源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 十道java華為編程大賽題目

    十道java華為編程大賽題目

    這篇文章主要為大家分享了十道java華為編程大賽題目,代碼經(jīng)過(guò)調(diào)試,經(jīng)典的java編程題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • SpringBoot2零基礎(chǔ)到精通之JUnit 5與指標(biāo)監(jiān)控

    SpringBoot2零基礎(chǔ)到精通之JUnit 5與指標(biāo)監(jiān)控

    SpringBoot是一種整合Spring技術(shù)棧的方式(或者說(shuō)是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開(kāi)發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)JUnit 5與指標(biāo)監(jiān)控
    2022-03-03
  • Spring中的@EnableScheduling定時(shí)任務(wù)注解

    Spring中的@EnableScheduling定時(shí)任務(wù)注解

    這篇文章主要介紹了Spring中的@EnableScheduling注解,@EnableScheduling是 Spring Framework 提供的一個(gè)注解,用于啟用 Spring 的定時(shí)任務(wù)功能,通過(guò)使用這個(gè)注解,可以在 Spring 應(yīng)用程序中創(chuàng)建定時(shí)任務(wù),需要的朋友可以參考下
    2024-01-01
  • springboot實(shí)現(xiàn)注冊(cè)加密與登錄解密功能(demo)

    springboot實(shí)現(xiàn)注冊(cè)加密與登錄解密功能(demo)

    這篇文章主要介紹了springboot實(shí)現(xiàn)注冊(cè)的加密與登錄的解密功能,本文通過(guò)demo實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-02-02
  • SpringCloud Feign 服務(wù)調(diào)用的實(shí)現(xiàn)

    SpringCloud Feign 服務(wù)調(diào)用的實(shí)現(xiàn)

    Feign是一個(gè)聲明性web服務(wù)客戶(hù)端。本文記錄多個(gè)服務(wù)之間使用Feign調(diào)用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01

最新評(píng)論