背景:項目使用mybatis分頁插件不生效,以及多租戶使用時讀取配置異常
分頁插件不細述,網(wǎng)上很多方法試了還是不生效,最后修改到當前版本解決,直接上代碼
多租戶插件使用遇到的問題:
最開始在MyTenantLineHandler中使用 @Value("${tables}"),服務啟動時能從配置中心拉取到配置,但在運行時獲取到的值為空,試了很多方法都不生效,后面將配置中心的配置在調(diào)用MyTenantLineHandler的那一層向下傳遞配置值,問題解決文章來源:http://www.zghlxwxcb.cn/news/detail-738496.html
租戶配置文章來源地址http://www.zghlxwxcb.cn/news/detail-738496.html
@Component
@RefreshScope
@ConfigurationProperties(prefix = "saas.tenant")
public class ConfigProperties {
public void setColumn(String column) {
this.column = column;
}
public void setTables(List<String> tables) {
this.tables = tables;
}
public String getColumn(){
return this.column;
}
public List<String> getTables(){
return this.tables;
}
private String column="";
private List<String> tables= Lists.newArrayList();
}
TenantLineHandler
public class MyTenantLineHandler implements TenantLineHandler {
private final ConfigProperties configProperties;
public MyTenantLineHandler(ConfigProperties configProperties) {
this.configProperties = configProperties;
}
@Override
public LongValue getTenantId() {
Long tenantId = TenantContextHolder.getTenantId();
return tenantId == null ? new LongValue(-1) : new LongValue(tenantId);
}
@Override
public String getTenantIdColumn() {
return configProperties.getColumn();
}
@Override
public boolean ignoreTable(String tableName) {
return !configProperties.getTables().contains(tableName);
// return TenantLineHandler.super.ignoreTable(tableName);
}
}
MybatisPlusConfig
注意:TenantLineInnerInterceptor要添加PaginationInnerInterceptor之前,如果放錯了,會出現(xiàn)分頁count(*)查詢時不會自動加上tenant_id
@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
//@EnableTransactionManagement
public class MybatisPlusConfig {
@Autowired
private List<SqlSessionFactory> sqlSessionFactoryList;
@Autowired
private ConfigProperties configProperties;
@PostConstruct
public void addMyInterceptor() {
MybatisPlusInterceptor interceptor = mybatisPlusInterceptor();
for (SqlSessionFactory factory : sqlSessionFactoryList) {
factory.getConfiguration().addInterceptor(interceptor);
}
}
/**
* mybatisplus 分頁攔截器
* @return
*/
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
TenantLineInnerInterceptor tenantLineInnerInterceptor=new TenantLineInnerInterceptor();
tenantLineInnerInterceptor.setTenantLineHandler(new MyTenantLineHandler(configProperties));
interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
到了這里,關于mybatis-plus分頁total為0,分頁失效,mybatis-plus多租戶插件使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!