java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > MyBatis-Plus使用selectPage不生效

解决MyBatis-Plus使用动态表名selectPage不生效的问题

作者:我知道你都知道u

这篇文章主要介绍了如恶化解决MyBatis-Plus使用动态表名selectPage不生效的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

以下是报错信息:

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
/**  
* 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

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

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

以上就是解决MyBatis-Plus使用动态表名selectPage不生效的问题的详细内容,更多关于MyBatis-Plus使用动态表名selectPage不生效的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文