背景:项目使用mybatis分页插件不生效,以及多租户使用时读取配置异常
分页插件不细述,网上很多方法试了还是不生效,最后修改到当前版本解决,直接上代码
多租户插件使用遇到的问题:
最开始在MyTenantLineHandler中使用 @Value("${tables}"),服务启动时能从配置中心拉取到配置,但在运行时获取到的值为空,试了很多方法都不生效,后面将配置中心的配置在调用MyTenantLineHandler的那一层向下传递配置值,问题解决文章来源:https://www.toymoban.com/news/detail-738496.html
租户配置文章来源地址https://www.toymoban.com/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之前,如果放错了,会出现分页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多租户插件使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!