mybatis-plus分页total为0,分页失效,mybatis-plus多租户插件使用

背景:项目使用mybatis分页插件不生效,以及多租户使用时读取配置异常

复制代码
分页插件不细述,网上很多方法试了还是不生效,最后修改到当前版本解决,直接上代码

多租户插件使用遇到的问题:

最开始在MyTenantLineHandler中使用 @Value("${tables}"),服务启动时能从配置中心拉取到配置,但在运行时获取到的值为空,试了很多方法都不生效,后面将配置中心的配置在调用MyTenantLineHandler的那一层向下传递配置值,问题解决

租户配置

java 复制代码
@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
java 复制代码
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
java 复制代码
@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();
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
		TenantLineInnerInterceptor tenantLineInnerInterceptor=new TenantLineInnerInterceptor();
		tenantLineInnerInterceptor.setTenantLineHandler(new MyTenantLineHandler(configProperties));
		interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
		return interceptor;
	}
}
相关推荐
敲代码的彭于晏1 小时前
2026 K8s 零基础入门:手把手教你一步步搭建 Kubernetes 集群
java·运维·全栈
希洛兔1 小时前
状态模式详解:让对象随内部状态自动改变行为
java·设计模式·状态模式
需要8261 小时前
Spring Boot 启动流程:run() 里到底发生了什么
java·spring boot·后端
云絮.1 小时前
Mybatis(一)
java·数据库·mybatis
希洛兔2 小时前
里氏替换原则详解:子类必须能透明替换基类(含继承风险演示 + 正确重构 + JDK 案例)
java·里氏替换原则
kevien_G13 小时前
微服务之OpenFeign最佳实践
java·微服务·架构
江湖有缘3 小时前
Docker实战 | 使用Docker部署ShipShipShip自托管产品更新与功能投票系统
java·linux·服务器·docker
范什么特西3 小时前
亲和路由11
java
诺NA亚3 小时前
ElasticSearch+RAG初使用
java
SamDeepThinking3 小时前
synchronized 和 lock 有什么区别?
java·后端·面试