MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found Error处理)
错误梗概
接手了一个新任务,需要修改别人的代码。看看数据库配置,连连接池都没,然后引入了druid,本来一切很顺利。后来不知道怎么回事,运行起来后总是报“Invalidboundstatement(notfound)”,而且报错的都是MybatisPlus生成的BaseMapper中的方法,自己写的都能正常运行。
参考了很多其他帖子,都无果,最后弄了很久,终于搞定了,特级录之。
druid和mybatis-plus配置参数如下:
pom.xml
com.alibaba druid-spring-boot-starter 1.1.10 com.baomidou mybatis-plus-boot-starter 3.2.0 com.github.pagehelper pagehelper-spring-boot-starter 1.2.13 jsqlparser com.github.jsqlparser mybatis-spring org.mybatis
application.properties
#########################################################MybatisPlus配置 mybatis-plus.mapper-locations=classpath:mapper/*.xml mybatis-plus.type-aliases-package=com.yishi.entity mybatis-plus.global-config.db-config.table-underline=true mybatis-plus.global-config.banner=false mybatis-plus.configuration.map-underscore-to-camel-case=true mybatis-plus.configuration.jdbc-type-for-null=NULL mybatis-plus.configuration.auto-mapping-behavior=FULL #########################################################druid配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource #参考https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter ####################################JDBC配置 spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.druid.url=${mysql.url} spring.datasource.druid.username=${mysql.username} spring.datasource.druid.password=${mysql.password} ####################################连接池配置 #初始化大小,最小,最大 spring.datasource.druid.initial-size=10 spring.datasource.druid.max-active=500 spring.datasource.druid.min-idle=5 #配置获取连接等待超时的时间 spring.datasource.druid.max-wait=60000 #打开PSCache,并且指定每个连接上PSCache的大小 spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 #spring.datasource.druid.max-open-prepared-statements=#和上面的等价 #spring.datasource.druid.validation-query= #spring.datasource.druid.validation-query-timeout= spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.test-while-idle=true #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.druid.time-between-eviction-runs-millis=60000 #配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.druid.min-evictable-idle-time-millis=300000 #spring.datasource.druid.max-evictable-idle-time-millis= #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.druid.filters=stat,wall,log4j spring.datasource.druid.validation-query=SELECT1FROMDUAL #通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ####################################监控配置 #WebStatFilter配置,说明请参考DruidWiki,配置_配置WebStatFilter spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.web-stat-filter.url-pattern=/* spring.datasource.druid.web-stat-filter.exclusions=/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico spring.datasource.druid.web-stat-filter.session-stat-enable=true spring.datasource.druid.web-stat-filter.session-stat-max-count=10 #spring.datasource.druid.web-stat-filter.principal-session-name= #spring.datasource.druid.web-stat-filter.principal-cookie-name= #spring.datasource.druid.web-stat-filter.profile-enable= #StatViewServlet配置,说明请参考DruidWiki,配置_StatViewServlet配置 spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.reset-enable=true spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=admin #spring.datasource.druid.stat-view-servlet.allow= #spring.datasource.druid.stat-view-servlet.deny=
DruidDataSourceProperties
packagecom.yishi.config; importorg.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix="spring.datasource.druid") publicclassDruidDataSourceProperties{ privateStringdriverClassName; privateStringurl; privateStringusername; privateStringpassword; privateintinitialSize; privateintminIdle; privateintmaxActive; privatelongmaxWait; privatelongtimeBetweenEvictionRunsMillis; privatelongminEvictableIdleTimeMillis; privateStringvalidationQuery; privatebooleantestWhileIdle; privatebooleantestOnBorrow; privatebooleantestOnReturn; privatebooleanpoolPreparedStatements; privateintmaxPoolPreparedStatementPerConnectionSize; privateStringfilters; privateStringconnectionProperties; //省略get、set方法 }
MysqlDruidConfiguration
packagecom.yishi.config; importcom.alibaba.druid.pool.DruidDataSource; importorg.apache.ibatis.session.SqlSessionFactory; importorg.mybatis.spring.SqlSessionFactoryBean; importorg.mybatis.spring.annotation.MapperScan; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.beans.factory.annotation.Qualifier; importorg.springframework.boot.context.properties.EnableConfigurationProperties; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importorg.springframework.context.annotation.Primary; importorg.springframework.core.io.support.PathMatchingResourcePatternResolver; importorg.springframework.jdbc.core.JdbcTemplate; importorg.springframework.jdbc.datasource.DataSourceTransactionManager; importjavax.sql.DataSource; importjava.sql.SQLException; @Configuration @EnableConfigurationProperties({DruidDataSourceProperties.class}) @MapperScan(value=MysqlDruidConfiguration.PACKAGE,sqlSessionFactoryRef="mysqlSqlSessionFactory") publicclassMysqlDruidConfiguration{ staticfinalStringPACKAGE="com.yishi.dao"; staticfinalStringMAPPER_LOCATION="classpath:mapper/*.xml"; @Autowired privateDruidDataSourcePropertiesproperties; @Bean(name="mysqlDataSource") @Primary publicDataSourcemysqlDataSource(){ DruidDataSourcedruidDataSource=newDruidDataSource(); druidDataSource.setDriverClassName(properties.getDriverClassName()); druidDataSource.setUrl(properties.getUrl()); druidDataSource.setUsername(properties.getUsername()); druidDataSource.setPassword(properties.getPassword()); druidDataSource.setInitialSize(properties.getInitialSize()); druidDataSource.setMinIdle(properties.getMinIdle()); druidDataSource.setMaxActive(properties.getMaxActive()); druidDataSource.setMaxWait(properties.getMaxWait()); druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis()); druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis()); druidDataSource.setValidationQuery(properties.getValidationQuery()); druidDataSource.setTestWhileIdle(properties.isTestWhileIdle()); druidDataSource.setTestOnBorrow(properties.isTestOnBorrow()); druidDataSource.setTestOnReturn(properties.isTestOnReturn()); druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements()); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize()); druidDataSource.setConnectionProperties(properties.getConnectionProperties()); try{ druidDataSource.setFilters(properties.getFilters()); druidDataSource.init(); }catch(SQLExceptione){ e.printStackTrace(); } returndruidDataSource; } @Bean(name="mysqlTransactionManager") @Primary publicDataSourceTransactionManagermysqlTransactionManager(){ returnnewDataSourceTransactionManager(mysqlDataSource()); } @Bean(name="mysqlSqlSessionFactory") @Primary publicSqlSessionFactorymysqlSqlSessionFactory(@Qualifier("mysqlDataSource")DataSourcemysqlDataSource) throwsException{ finalSqlSessionFactoryBeansessionFactory=newSqlSessionFactoryBean(); sessionFactory.setDataSource(mysqlDataSource); sessionFactory.setMapperLocations( newPathMatchingResourcePatternResolver().getResources(MysqlDruidConfiguration.MAPPER_LOCATION)); returnsessionFactory.getObject(); } @Bean(name="mysqlJdbcTemplate") publicJdbcTemplatehiveJdbcTemplate(@Qualifier("mysqlDataSource")DataSourcedataSource){ returnnewJdbcTemplate(dataSource); } }
开发环境
IDEA2020.2
ApacheMaven3.2.5
主要库版本:
org.springframework.boot.spring-boot-dependencies.2.1.1.RELEASE
com.baomidou.mybatis-plus-boot-starter.3.2.0
com.alibaba.druid-spring-boot-starter.1.1.10
异常信息
org.apache.ibatis.binding.BindingException:Invalidboundstatement(notfound):com.yishi.dao.UserDao.selectList atorg.apache.ibatis.binding.MapperMethod$SqlCommand.(MapperMethod.java:235) atorg.apache.ibatis.binding.MapperMethod. (MapperMethod.java:53) atorg.apache.ibatis.binding.MapperProxy.lambda$cachedMapperMethod$0(MapperProxy.java:98) atjava.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) atorg.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:97) atorg.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:92) atcom.sun.proxy.$Proxy105.selectList(UnknownSource) atcom.yishi.impl.UserServiceImpl.insert(UserServiceImpl.java:104) atcom.yishi.impl.UserServiceImpl.insert(UserServiceImpl.java:39) atcom.yishi.impl.UserServiceImpl$$FastClassBySpringCGLIB$$ac0e5ea9.invoke( ) atorg.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) atorg.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) atorg.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) atorg.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) atorg.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) atcom.yishi.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$c9a67874.insert( ) atcom.yishi.controller.UserController.add(UserController.java:20) atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod) atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) atjava.lang.reflect.Method.invoke(Method.java:498) atorg.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) atorg.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) atorg.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) atorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) atorg.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) atorg.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) atorg.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) atorg.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) atorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) atorg.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) atjavax.servlet.http.HttpServlet.service(HttpServlet.java:660) atorg.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) atjavax.servlet.http.HttpServlet.service(HttpServlet.java:741) atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) atorg.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) atcom.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) atorg.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) atorg.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) atorg.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) atorg.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) atorg.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) atorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) atorg.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) atorg.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) atorg.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) atorg.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) atorg.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) atorg.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) atjava.lang.Thread.run(Thread.java:748)
解决方式
一开始没打开Debug日志,导致很多Debug的信息都没法及时捕获,打开Debug日志后,WARN如下:
2020-09-0110:02:15[restartedMain]WARN o.m.s.m.ClassPathMapperScanner-SkippingMapperFactoryBeanwithname'userDao'and'com.yishi.dao.UserDao'mapperInterface.Beanalreadydefinedwiththesamename!
...
2020-09-0110:02:15[restartedMain]WARN o.m.s.m.ClassPathMapperScanner-NoMyBatismapperwasfoundin'[com.yishi.dao]'package.Pleasecheckyourconfiguration.
可以看到,在注入dao时说已经存在了,难怪Mybatis-Plus生成的BaseMapper所有方法报错,压根就没注入。那什么地方先注入了呢?看来看去,原来是在MysqlDruidConfiguration.java这个类在自动配置时配置“mysqlSqlSessionFactory”这个bean时已经先注入dao相关的信息!
最后解决方式,删掉MysqlDruidConfiguration.java和DruidDataSourceProperties.java这两个类,springboot都已经帮我们处理了druid的配置,其实这两个类也没什么用。
总结
到此这篇关于MybatisPlusBaseMapper中的方法全部Invalidboundstatement(notfoundError处理)的文章就介绍到这了,更多相关MybatisPlusBaseMapper内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。