时间:2021-05-20
接手了一个新任务,需要修改别人的代码。看看数据库配置 ,连连接池都没,然后引入了 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/*.xmlmybatis-plus.type-aliases-package=com.yishi.entitymybatis-plus.global-config.db-config.table-underline=truemybatis-plus.global-config.banner=falsemybatis-plus.configuration.map-underscore-to-camel-case=truemybatis-plus.configuration.jdbc-type-for-null=NULLmybatis-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.Driverspring.datasource.druid.url=${mysql.url}spring.datasource.druid.username=${mysql.username}spring.datasource.druid.password=${mysql.password}#################################### 连接池配置# 初始化大小,最小,最大spring.datasource.druid.initial-size=10spring.datasource.druid.max-active=500spring.datasource.druid.min-idle=5# 配置获取连接等待超时的时间spring.datasource.druid.max-wait=60000# 打开PSCache,并且指定每个连接上PSCache的大小spring.datasource.druid.pool-prepared-statements=truespring.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=falsespring.datasource.druid.test-on-return=falsespring.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,log4jspring.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,配置_配置WebStatFilterspring.datasource.druid.web-stat-filter.enabled=truespring.datasource.druid.web-stat-filter.url-pattern=/*spring.datasource.druid.web-stat-filter.exclusions=/druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.icospring.datasource.druid.web-stat-filter.session-stat-enable=truespring.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=truespring.datasource.druid.stat-view-servlet.url-pattern=/druid/*spring.datasource.druid.stat-view-servlet.reset-enable=truespring.datasource.druid.stat-view-servlet.login-username=adminspring.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
一开始没打开 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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
node项目中的错误处理node中Error对象的使用使用captureStackTrace方法加入自带的错误信息//Error对象自带的属性Error.cap
PHP中error_reporting函数用法详细介绍PHP中对错误的处理会用到error_reporting函数,看到最多的是error_reporting(
引言with语句是从Python2.5开始引入的一种与异常处理相关的功能(2.5版本中要通过from__future__importwith_statement
Go的error比较灵活.但是自身对error处理的机制有不太好用,我们可以自定义错误输出:只要所有实现了Error()方法的对象都可以,这里给个比较简单的de
with语句是从Python2.5开始引入的一种与异常处理相关的功能(2.5版本中要通过from__future__importwith_statement导入