mybatis-plus動態表名的實現示例

背景

在分表的背景下,有時候查詢數據的時候需要跨表查詢,那此時就需要MP在解析的時候,能夠很好的自適應表格名稱

實現

MP中是通過PaginationInterceptor(分頁插件)完成動態表名解析的,配置如下:

數據庫中表

依賴

<dependency>
 <groupId>com.baomidou</groupId>
 <artifactId>mybatis-plus-boot-starter</artifactId>
 <version>3.3.1.tmp</version>
</dependency>

配置類

package com.huanchuang.common.config;
 
import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;
import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.util.Collections;
import java.util.HashMap;
 
/**
 * @Package: com.huanchuang.common.config
 * @Description: <mybatis-plush配置類>
 * @Author: MILLA
 * @CreateDate: 2020/09/04 14:42
 * @UpdateUser: MILLA
 * @UpdateDate: 2020/09/04 14:42
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Configuration
@MapperScan("com.huanchuang.ext.mapper**")
@ConditionalOnProperty(prefix = "spring.config", name = "enableMybatisPlusDynamicTable", havingValue = "true")
public class MybatisPlusDynamicTableConfig {
 
 private static final String DYNAMIC_TABLE_PRE = "common_user";
 
 /**
  * mybatis-plus分頁插件
  */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
  PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
  // 設置請求的頁面大於最大頁後操作, true調回到首頁,false 繼續請求 默認false
  paginationInterceptor.setOverflow(false);
  // 設置最大單頁限制數量,默認 500 條,-1 不受限制
  paginationInterceptor.setLimit(500);
//  // 開啟 count 的 join 優化,隻針對部分 left join
  paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
  DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();
  dynamicTableNameParser.setTableNameHandlerMap(new HashMap<String, ITableNameHandler>(2) {{
   //動態表規則-生成自己需要的動態表名
   put(DYNAMIC_TABLE_PRE, (metaObject, sql, tableName) -> DynamicTableTreadLocal.INSTANCE.getTableName());
  }});
  paginationInterceptor.setSqlParserList(Collections.singletonList(dynamicTableNameParser));
  return paginationInterceptor;
 }
}

動態表名存儲類

package com.huanchuang.common.config;
 
/**
 * @Package: com.huanchuang.common.config
 * @Description: <動態表格存儲類>
 * @Author: MILLA
 * @CreateDate: 2020/09/04 14:42
 * @UpdateUser: MILLA
 * @UpdateDate: 2020/09/04 14:42
 * @UpdateRemark: <>
 * @Version: 1.0
 */
public enum DynamicTableTreadLocal {
 INSTANCE;
 private ThreadLocal<String> tableName = new ThreadLocal<>();
 
 public String getTableName() {
  return tableName.get();
 }
 
 public void setTableName(String tableName) {
  this.tableName.set(tableName);
 }
 
 public void remove() {
  tableName.remove();
 }
 
}

使用

 private void select(int year) {
 
   DynamicTableTreadLocal.INSTANCE.setTableName("user_" + year);
   LambdaQueryWrapper<SparkDownSample> wrapper = Wrappers.lambdaQuery(User.class);
   List<User> userList = userMapper.list(wrapper )
 
 }
@Data
public class User {
 
 private Long id;
 
 private String userName;
 
 private String address;
 
 private char sex;
 
 private Byte age;
 
}

原理

以mybatis的query方法作為入口通過動態代理執行到配置的分頁插件通過分頁插件進行sql解析根據分頁插件中配置的tableNameHandler進行目標表格的替換最後形成一個可執行sql,執行查詢

到此這篇關於mybatis-plus動態表名的實現示例的文章就介紹到這瞭,更多相關mybatis-plus 動態表名內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!