MyBaitis-Plus 使用动态表名 selectPage 不生效!

使用MyBatis-Plus 我们需要安装各种插件,比如官网提供的一些分页插件等。插件的安装请参考官网,声明一下我的配置

以下是报错信息:

java 复制代码
org.springframework.jdbc.BadSqlGrammarException: 
### Error querying database.  Cause: java.sql.SQLSyntaxErrorException: Table 'dataBaseName.delivery_file_print_log' doesn't exist
### The error may exist in XXMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT COUNT(*) FROM delivery_file_print_log WHERE (status = ?)
### Cause: java.sql.SQLSyntaxErrorException: Table 'dataBaseName.delivery_file_print_log' doesn't exist
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table 'dataBaseName.delivery_file_print_log' doesn't exist

	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:91)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
	at com.sun.proxy.$Proxy192.selectList(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForIPage(MybatisMapperMethod.java:121)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:85)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
	at com.sun.proxy.$Proxy277.selectPage(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:123)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
	at com.sun.proxy.$Proxy278.selectPage(Unknown Source)
	at com.banksteel.delivery.buy.service.BasicServiceTest.testEmail(BasicServiceTest.java:117)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

以下是我的项目mybaltis---plus的插件配置

java 复制代码
```java
/**  
* MyBatis配置  
*/  
@Configuration(proxyBeanMethods = false)  
public static class MyBatisConfig {  
  
@Bean  
public MybatisPlusInterceptor mybatisPlusInterceptor(DeliveryProperties props) {  
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();  
  
// 增加 SQL阻断解析器,防止全表update、delete  
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());  
  
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();  
// 动态表名  
dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {  
// 获取参数方法  
if ("delivery_file_print_log".equals(tableName)) {  
tableName = tableName + "_" +getYear();  
}  
return tableName;  
});  
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);  
// 乐观锁插件 @version  
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());  
  
// 分页插件  
PaginationInnerInterceptor pagination = new PaginationInnerInterceptor(DbType.MYSQL);  
// 设置最大单页限制数量,默认不限制  
pagination.setMaxLimit(props.getMaxLimitByPage());  
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false  
// pagination.setOverflow(false)  
interceptor.addInnerInterceptor(pagination);  
  
return interceptor;  
}  
  
public static String getYear() {  
Calendar calendar = Calendar.getInstance();  
int yearInt = calendar.get(Calendar.YEAR);  
return String.valueOf(yearInt).substring(2);  
}  
}

其中我需要对delivery_file_print_log的库实现动态表名的查询,比如今年是23年,那么查询的库表应该是delivery_file_print_log_23。 经过多次测试,selectById 或者 selectList方法都是可以正常查询的,但是selectPage会报错如上,具体的原因是拦截器的顺序导致的问题

com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor#intercept 在每执行一次sql查询的时候,都会遍历一次加载的拦截器

因为是List,所以第一个使用的是分页查询拦截器 com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor#willDoQuery

分页查询的拦截器里面会进行一次查询操作,这个地方会查询数据库,导致报错。

解决方案:将拦截器的顺序调整,分页拦截器调整在动态表名拦截器之后,完美解决问题。

相关推荐
夜色呦24 分钟前
现代电商解决方案:Spring Boot框架实践
数据库·spring boot·后端
爱敲代码的小冰32 分钟前
spring boot 请求
java·spring boot·后端
java小吕布2 小时前
Java中的排序算法:探索与比较
java·后端·算法·排序算法
Goboy2 小时前
工欲善其事,必先利其器;小白入门Hadoop必备过程
后端·程序员
李少兄3 小时前
解决 Spring Boot 中 `Ambiguous mapping. Cannot map ‘xxxController‘ method` 错误
java·spring boot·后端
代码小鑫3 小时前
A031-基于SpringBoot的健身房管理系统设计与实现
java·开发语言·数据库·spring boot·后端
Json____3 小时前
学法减分交管12123模拟练习小程序源码前端和后端和搭建教程
前端·后端·学习·小程序·uni-app·学法减分·驾考题库
monkey_meng3 小时前
【Rust类型驱动开发 Type Driven Development】
开发语言·后端·rust
落落落sss3 小时前
MQ集群
java·服务器·开发语言·后端·elasticsearch·adb·ruby
大鲤余4 小时前
Rust,删除cargo安装的可执行文件
开发语言·后端·rust