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

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

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

前言

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

wapper介紹 :

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

條件構(gòu)造器 AbstractWrapper

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

一、什么是AbstractWrapper

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

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

在這里插入圖片描述

二、QueryWrapper(LambdaQueryWrapper)

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

1、QueryWrapper用法示例

查詢id在1 - 30且性別為男、姓名帶有test的用戶:

@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的簡寫,該方法兩個參數(shù),column和value,表示column的值和value要相等。
此處like方法就是查詢name中包含“test”字樣的記錄;between方法三個參數(shù),分別是column、value1、value2,該方法表示column的值要在value1和value2之間。
注意column是數(shù)據(jù)表對應(yīng)的字段,而非實體類屬性字段。

運行日志

在這里插入圖片描述

2、LambdaQueryWrapper用法示例

LambdaQueryWrapper是mybatis plus中的一個條件構(gòu)造器對象,只是是需要使用Lambda 語法使用 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);
    }

運行日志

在這里插入圖片描述

三、UpdateWrapper(LambdaUpdateWrapper)

我們再進行更新或者刪除的時候,有時候where條件復(fù)雜的話,可以使用UpdateWrapper來構(gòu)造條件
他們有部分方法是和上面的QueryWrapper(LambdaQueryWrapper) 相同的。這里演示不同之處

1、UpdateWrapper用法示例

將名字包含test1的用戶名改為test1.1 :

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

運行日志

在這里插入圖片描述

2、LambdaUpdateWrapper用法示例

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

在這里插入圖片描述

mybatis-plus的插件

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

一、分頁插件

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

1、配置分頁插件

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 {
    /**
     * 新的分頁插件,一緩和二緩遵循mybatis的規(guī)則,需要設(shè)置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現(xiàn)問題
     * 3.4.3不用設(shè)置
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}

這個配置摘抄自官方示例

2、測試代碼

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

運行結(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)的分頁信息 ==================
總條數(shù):31
當(dāng)前頁碼:1
總頁數(shù):4
每頁顯示條數(shù):10
是否有上一頁:false
是否有下一頁:true
查詢結(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)

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

3、自定義分頁條件

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

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

測試代碼

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

運行結(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)的分頁信息 ==================
總條數(shù):17
當(dāng)前頁碼:2
總頁數(shù):2
每頁顯示條數(shù):10
是否有上一頁:true
是否有下一頁:false
分頁結(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)

二、性能分析插件

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

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

–防止全表更新與刪除

1、配置插件

和上面的分頁插件類似

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

2、測試代碼

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

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

運行該junit測試,可以看到報如下錯誤,說明該插件生效了。

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

相關(guān)文章

  • idea報錯:java程序包com.github.xiaoymin.knife4j.spring.annotations不存在問題解決

    idea報錯:java程序包com.github.xiaoymin.knife4j.spring.annotations

    這篇文章主要介紹了idea報錯:java程序包com.github.xiaoymin.knife4j.spring.annotations不存在問題解決,需要的朋友可以參考下
    2023-06-06
  • MyBatis中Mapper的注入問題詳解

    MyBatis中Mapper的注入問題詳解

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

    IDEA反編譯出整個jar包源碼

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

    AsyncHttpClient的ConnectionSemaphore方法源碼流程解讀

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

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

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

    十道java華為編程大賽題目

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

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

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

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

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

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

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

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

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

最新評論