您好,登錄后才能下訂單哦!
接手了一個新任務,需要修改別人的代碼。看看數據庫配置 ,連連接池都沒,然后引入了 druid,本來一切很順利。后來不知道怎么回事,運行起來后總是報 “Invalid bound statement (not found) ”,而且報錯的都是 MybatisPlus 生成的 BaseMapper 中的方法,自己寫的都能正常運行。
參考了很多其他帖子,都無果,最后弄了很久,終于搞定了,特級錄之。
druid 和 mybatis-plus 配置參數如下:
pom.xml
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> <exclusions> <exclusion> <artifactId>jsqlparser</artifactId> <groupId>com.github.jsqlparser</groupId> </exclusion> <exclusion> <artifactId>mybatis-spring</artifactId> <groupId>org.mybatis</groupId> </exclusion> </exclusions> </dependency>
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=SELECT 1 FROM DUAL # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄 spring.datasource.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #################################### 監控配置 # WebStatFilter配置,說明請參考Druid Wiki,配置_配置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配置,說明請參考Druid Wiki,配置_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
package com.yishi.config; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "spring.datasource.druid") public class DruidDataSourceProperties { private String driverClassName; private String url; private String username; private String password; private int initialSize; private int minIdle; private int maxActive; private long maxWait; private long timeBetweenEvictionRunsMillis; private long minEvictableIdleTimeMillis; private String validationQuery; private boolean testWhileIdle; private boolean testOnBorrow; private boolean testOnReturn; private boolean poolPreparedStatements; private int maxPoolPreparedStatementPerConnectionSize; private String filters; private String connectionProperties; // 省略 get、set 方法 }
MysqlDruidConfiguration
package com.yishi.config; import com.alibaba.druid.pool.DruidDataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; import java.sql.SQLException; @Configuration @EnableConfigurationProperties({DruidDataSourceProperties.class}) @MapperScan(value = MysqlDruidConfiguration.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory") public class MysqlDruidConfiguration { static final String PACKAGE = "com.yishi.dao"; static final String MAPPER_LOCATION = "classpath:mapper/*.xml"; @Autowired private DruidDataSourceProperties properties; @Bean(name = "mysqlDataSource") @Primary public DataSource mysqlDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); 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 (SQLException e) { e.printStackTrace(); } return druidDataSource; } @Bean(name = "mysqlTransactionManager") @Primary public DataSourceTransactionManager mysqlTransactionManager() { return new DataSourceTransactionManager(mysqlDataSource()); } @Bean(name = "mysqlSqlSessionFactory") @Primary public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(mysqlDataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(MysqlDruidConfiguration.MAPPER_LOCATION)); return sessionFactory.getObject(); } @Bean(name = "mysqlJdbcTemplate") public JdbcTemplate hiveJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
IDEA 2020.2
Apache Maven 3.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: Invalid bound statement (not found): com.yishi.dao.UserDao.selectList at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:53) at org.apache.ibatis.binding.MapperProxy.lambda$cachedMapperMethod$0(MapperProxy.java:98) at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660) at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:97) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:92) at com.sun.proxy.$Proxy105.selectList(Unknown Source) at com.yishi.impl.UserServiceImpl.insert(UserServiceImpl.java:104) at com.yishi.impl.UserServiceImpl.insert(UserServiceImpl.java:39) at com.yishi.impl.UserServiceImpl$$FastClassBySpringCGLIB$$ac0e5ea9.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) at com.yishi.impl.UserServiceImpl$$EnhancerBySpringCGLIB$$c9a67874.insert(<generated>) at com.yishi.controller.UserController.add(UserController.java:20) 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.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:791) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)
一開始沒打開 Debug 日志,導致很多 Debug 的信息都沒法及時捕獲,打開 Debug 日志后,WARN 如下:
2020-09-01 10:02:15 [restartedMain] WARN o.m.s.m.ClassPathMapperScanner - Skipping MapperFactoryBean with name 'userDao' and 'com.yishi.dao.UserDao' mapperInterface. Bean already defined with the same name!
...
2020-09-01 10:02:15 [restartedMain] WARN o.m.s.m.ClassPathMapperScanner - No MyBatis mapper was found in '[com.yishi.dao]' package. Please check your configuration.
可以看到,在注入 dao 時說已經存在了,難怪 Mybatis-Plus 生成的 BaseMapper 所有方法報錯,壓根就沒注入。那什么地方先注入了呢?看來看去,原來是在 MysqlDruidConfiguration.java 這個類在自動配置時配置 “mysqlSqlSessionFactory” 這個 bean 時已經先注入 dao 相關的信息!
最后解決方式,刪掉 MysqlDruidConfiguration.java 和 DruidDataSourceProperties.java這兩個類,springboot 都已經幫我們處理了 druid 的配置,其實這兩個類也沒什么用。
總結
到此這篇關于MybatisPlus BaseMapper 中的方法全部 Invalid bound statement (not found Error處理)的文章就介紹到這了,更多相關MybatisPlus BaseMapper內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。