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

詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案

 更新時間:2021年04月28日 10:34:58   作者:一個抓手  
這篇文章主要介紹了詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案,本文分兩步,通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.定義一個TenantLineHandler的實(shí)現(xiàn)類:

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.google.common.collect.Lists;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
 
import java.util.List;
 
/**
 * 多租戶處理插件
 *
 * @author 向振華
 * @date 2021/04/26 13:37
 */
public class CustomTenantLineHandler implements TenantLineHandler {
 
    /**
     * 忽略添加租戶ID的表
     */
    private final static List<String> IGNORE_TABLE_NAMES = Lists.newArrayList(
            "t_country",
            "t_language"
    );
 
    /**
     * 獲取租戶ID值表達(dá)式(可從cookie、token、緩存中取)
     *
     * @return
     */
    @Override
    public Expression getTenantId() {
        return new LongValue(1L);
    }
 
    /**
     * 獲取租戶字段名(數(shù)據(jù)庫的租戶ID字段名)
     *
     * @return
     */
    @Override
    public String getTenantIdColumn() {
        return "tenant_id";
    }
 
    /**
     * 根據(jù)表名判斷是否忽略拼接多租戶條件
     *
     * @param tableName
     * @return
     */
    @Override
    public boolean ignoreTable(String tableName) {
        return IGNORE_TABLE_NAMES.contains(tableName);
    }
}

2.定義MybatisPlusConfig配置類將多租戶插件生效:

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.bzcst.bop.component.mybatis.config.handler.AutoFillMetaObjectHandler;
import com.bzcst.bop.component.mybatis.config.handler.CustomTenantLineHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @author 向振華
 * @date 2021/04/26 14:45
 */
@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
 
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 多租戶插件(注意:這個一定要放在最上面)
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new CustomTenantLineHandler()));
        // 分頁插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
 
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
 
    @Bean
    public AutoFillMetaObjectHandler fillMetaObjectHandler() {
        return new AutoFillMetaObjectHandler();
    }
}

測試

@Test
    public void select() {
        Role role = roleService.getById(40L);
        System.out.println(role);
    }
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2fedfff1] was not registered for synchronization because synchronization is not active
Original SQL: SELECT id,tenant_id,frame_id,name,type,description,meta_created,meta_updated,meta_logic_flag FROM t_sec_role WHERE id=? 
parser sql: SELECT id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag FROM t_sec_role WHERE id = ? AND tenant_id = 1
2021-04-26 14:58:55.534  INFO 24980 --- [           main] com.zaxxer.hikari.HikariDataSource       : DatebookHikariCP - Starting...
2021-04-26 14:58:55.903  INFO 24980 --- [           main] com.zaxxer.hikari.HikariDataSource       : DatebookHikariCP - Start completed.
JDBC Connection [HikariProxyConnection@1100660981 wrapping com.mysql.cj.jdbc.ConnectionImpl@628fa8ea] will not be managed by Spring
==>  Preparing: SELECT id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag FROM t_sec_role WHERE id = ? AND tenant_id = 1
==> Parameters: 40(Long)
<==    Columns: id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag
<==        Row: 40, 1, 123, 一個角色啊, 2, haha, 2021-04-26 14:07:42, 2021-04-26 14:07:42, 1
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2fedfff1]
Role(id=40, sasTenantId=1, orgFrameId=123, name=一個角色啊, type=2, description=haha, metaCreated=Mon Apr 26 14:07:42 CST 2021, metaUpdated=Mon Apr 26 14:07:42 CST 2021, metaLogicFlag=1)

可以看到查詢語句后面拼接了tenant_id = 1

==>  Preparing: SELECT id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag FROM t_sec_role WHERE id = ? AND tenant_id = 1

注意:如果表中沒有定義tenant_id會報錯,不需要添加多租戶的表配置到CustomTenantLineHandler 中的IGNORE_TABLE_NAMES集合中

到此這篇關(guān)于詳解基于MybatisPlus兩步實(shí)現(xiàn)多租戶方案的文章就介紹到這了,更多相關(guān)MybatisPlus多租戶內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java中線程安全的list詳細(xì)特性和用法

    java中線程安全的list詳細(xì)特性和用法

    這篇文章主要給大家介紹了關(guān)于java中線程安全的list詳細(xì)特性和用法的相關(guān)資料,Java中有多種線程安全的List,其中比較常用的有Vector、Collections.synchronizedList()和CopyOnWriteArrayList三種方式,需要的朋友可以參考下
    2024-03-03
  • idea中使用maven?archetype新建項目時卡住問題解決方案

    idea中使用maven?archetype新建項目時卡住問題解決方案

    這篇文章主要介紹了idea中使用maven?archetype新建項目時卡住,解決本問題的方法,就是在maven的runner加上參數(shù)-DarchetypeCatalog=local就可以了,不需要下載xml文件再放到指定目錄,需要的朋友可以參考下
    2023-08-08
  • 解決spring?data?jpa?saveAll()?保存過慢問題

    解決spring?data?jpa?saveAll()?保存過慢問題

    這篇文章主要介紹了解決spring?data?jpa?saveAll()保存過慢問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 解決使用IDEA時跳轉(zhuǎn)到.class的問題

    解決使用IDEA時跳轉(zhuǎn)到.class的問題

    這篇文章主要介紹了解決使用IDEA時跳轉(zhuǎn)到.class的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java 反射機(jī)制實(shí)例詳解

    Java 反射機(jī)制實(shí)例詳解

    這篇文章主要介紹了Java 反射機(jī)制實(shí)例詳解的相關(guān)資料,這里對java中反射機(jī)制進(jìn)行了詳細(xì)的分析,需要的朋友可以參考下
    2017-09-09
  • feign遠(yuǎn)程調(diào)用無法傳遞對象屬性405的問題

    feign遠(yuǎn)程調(diào)用無法傳遞對象屬性405的問題

    這篇文章主要介紹了feign遠(yuǎn)程調(diào)用無法傳遞對象屬性405的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Eclipse創(chuàng)建JavaWeb工程的完整步驟記錄

    Eclipse創(chuàng)建JavaWeb工程的完整步驟記錄

    很多新手不知道Eclipse怎么創(chuàng)建Java Web項目,一起來看看吧,這篇文章主要給大家介紹了關(guān)于Eclipse創(chuàng)建JavaWeb工程的完整步驟,需要的朋友可以參考下
    2023-10-10
  • Java 類加載機(jī)制詳細(xì)介紹

    Java 類加載機(jī)制詳細(xì)介紹

    這篇文章主要介紹了Java 類加載機(jī)制詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot項目經(jīng)常將連接數(shù)據(jù)庫的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對安全性要求很高,因此我們就考慮如何對密碼進(jìn)行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧
    2019-10-10
  • 解析Oracle數(shù)據(jù)庫中的對象集合schema

    解析Oracle數(shù)據(jù)庫中的對象集合schema

    這篇文章主要介紹了Oracle數(shù)據(jù)庫中的對象集合schema,是Oracle數(shù)據(jù)庫入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11

最新評論