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;
	}
}
相关推荐
曹牧3 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
爬山算法4 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
kfyty7254 小时前
集成 spring-ai 2.x 实践中遇到的一些问题及解决方案
java·人工智能·spring-ai
猫头虎4 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
李少兄4 小时前
在 IntelliJ IDEA 中修改 Git 远程仓库地址
java·git·intellij-idea
忆~遂愿5 小时前
ops-cv 算子库深度解析:面向视觉任务的硬件优化与数据布局(NCHW/NHWC)策略
java·大数据·linux·人工智能
小韩学长yyds5 小时前
Java序列化避坑指南:明确这4种场景,再也不盲目实现Serializable
java·序列化
仟濹5 小时前
【Java基础】多态 | 打卡day2
java·开发语言
Re.不晚5 小时前
JAVA进阶之路——无奖问答挑战2
java·开发语言
Ro Jace6 小时前
计算机专业基础教材
java·开发语言