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;
	}
}
相关推荐
杨二K8 分钟前
认识HertzBeat的第一天
java·hertzbeat
DevilSeagull11 分钟前
JavaScript WebAPI 指南
java·开发语言·javascript·html·ecmascript·html5
期待のcode2 小时前
Spring框架1—Spring的IOC核心技术1
java·后端·spring·架构
葵野寺2 小时前
【RelayMQ】基于 Java 实现轻量级消息队列(七)
java·开发语言·网络·rabbitmq·java-rabbitmq
书院门前细致的苹果3 小时前
JVM 全面详解:深入理解 Java 的核心运行机制
java·jvm
上官浩仁3 小时前
springboot excel 表格入门与实战
java·spring boot·excel
Hello.Reader4 小时前
从零到一上手 Protocol Buffers用 C# 打造可演进的通讯录
java·linux·c#
树码小子4 小时前
Java网络初识(4):网络数据通信的基本流程 -- 封装
java·网络
稻草人想看远方4 小时前
GC垃圾回收
java·开发语言·jvm
en-route5 小时前
如何在 Spring Boot 中指定不同的配置文件?
java·spring boot·后端