专栏首页别先生Spring4.0+Mybatis整合时占位符无法读取jdbc.properties的问题

Spring4.0+Mybatis整合时占位符无法读取jdbc.properties的问题

1、在使用Spring+Mybatis整合时遇到了一个问题,在bean.xml配置文件引用外部jdbc.properties的时候报错,如下所示:

  1 java.lang.ClassNotFoundException: ${jdbc.driver}
  2     at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1358)
  3     at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
  4     at java.lang.Class.forName0(Native Method)
  5     at java.lang.Class.forName(Class.java:264)
  6     at com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded(DriverManagerDataSource.java:100)
  7     at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:132)
  8     at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
  9     at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
 10     at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
 11     at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
 12     at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
 13     at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
 14     at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
 15 [laboratory] 2021-02-24 09:40:42.339 < WARN> [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] BasicResourcePool.run(1841) | com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@398ac721 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 
 16 java.sql.SQLException: No suitable driver
 17     at java.sql.DriverManager.getDriver(DriverManager.java:315)
 18     at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)
 19     at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
 20     at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
 21     at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
 22     at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
 23     at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
 24     at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
 25     at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
 26     at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
 27 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
 28 ### Error querying database.  Cause: java.lang.RuntimeException: java.sql.SQLException: Connections could not be acquired from the underlying database!
 29 ### Cause: java.lang.RuntimeException: java.sql.SQLException: Connections could not be acquired from the underlying database!
 30     at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
 31     at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
 32     at com.sun.proxy.$Proxy11.selectOne(Unknown Source)
 33     at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
 34     at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:82)
 35     at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
 36     at com.sun.proxy.$Proxy16.userInfoLogin(Unknown Source)
 37     at com.flea.service.impl.UserInfoServiceImpl.userInfoLogin(UserInfoServiceImpl.java:23)
 38     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 39     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 40     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 41     at java.lang.reflect.Method.invoke(Method.java:498)
 42     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
 43     at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
 44     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
 45     at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
 46     at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
 47     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
 48     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 49     at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
 50     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
 51     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
 52     at com.sun.proxy.$Proxy25.userInfoLogin(Unknown Source)
 53     at com.flea.action.UserController.login(UserController.java:39)
 54     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 55     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 56     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 57     at java.lang.reflect.Method.invoke(Method.java:498)
 58     at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
 59     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
 60     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
 61     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
 62     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
 63     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
 64     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
 65     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
 66     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
 67     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
 68     at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
 69     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
 70     at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
 71     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
 72     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 73     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
 74     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
 75     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 76     at com.flea.utils.UtfFilter.doFilter(UtfFilter.java:28)
 77     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
 78     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 79     at com.flea.utils.LoginFilter.doFilter(LoginFilter.java:43)
 80     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
 81     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 82     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
 83     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
 84     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
 85     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
 86     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
 87     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
 88     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
 89     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
 90     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
 91     at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
 92     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
 93     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
 94     at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:616)
 95     at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
 96     at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
 97     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1634)
 98     at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
 99     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
100     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
101     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
102     at java.lang.Thread.run(Thread.java:748)
103 Caused by: org.apache.ibatis.exceptions.PersistenceException: 
104 ### Error querying database.  Cause: java.lang.RuntimeException: java.sql.SQLException: Connections could not be acquired from the underlying database!
105 ### Cause: java.lang.RuntimeException: java.sql.SQLException: Connections could not be acquired from the underlying database!
106     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
107     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
108     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
109     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:77)
110     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
111     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
112     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
113     at java.lang.reflect.Method.invoke(Method.java:498)
114     at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
115     ... 71 more
116 Caused by: java.lang.RuntimeException: java.sql.SQLException: Connections could not be acquired from the underlying database!
117     at com.github.pagehelper.PageHelper.getUrl(PageHelper.java:287)
118     at com.github.pagehelper.PageHelper.getSqlUtil(PageHelper.java:310)
119     at com.github.pagehelper.PageHelper.initSqlUtil(PageHelper.java:266)
120     at com.github.pagehelper.PageHelper.intercept(PageHelper.java:253)
121     at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
122     at com.sun.proxy.$Proxy34.query(Unknown Source)
123     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
124     ... 78 more
125 Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
126     at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
127     at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
128     at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
129     at com.github.pagehelper.PageHelper.getUrl(PageHelper.java:284)
130     ... 84 more
131 Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
132     at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
133     at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
134     at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
135     at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
136     ... 86 more

我的bean.xml配置文件,如果直接引用数据库的驱动,账号密码,url就不会出错的,如下所示:

 1 <!-- 引入外部配置文件,加载jdbc.properties里面的值 -->
 2 <context:property-placeholder location="classpath:jdbc.properties" />
 3 
 4 <!-- 配置数据源dataSource,记得去掉mybatils.cfg.xml的数据源相关配置 -->
 5 <bean id="dataSource"
 6     class="com.mchange.v2.c3p0.ComboPooledDataSource">
 7     <property name="jdbcUrl" value="${jdbc.url}" />
 8     <property name="driverClass" value="${jdbc.driver}" />
 9     <property name="user" value="${jdbc.username}" />
10     <property name="password" value="${jdbc.password}" />
11     <!-- <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/laboratory_sys?useUnicode=true&amp;characterEncoding=UTF-8" /> 
12          <property name="driverClass" value="com.mysql.jdbc.Driver" /> 
13          <property name="user" value="root" /> 
14          <property name="password" value="123456" /> 
15     -->
16 </bean>

这个问题是无法识别占位符,就是在加载过程中直接把${jdbc.driver}当做字符串处理了,具体报错问题就是,MapperScannerConfigurer 先于properties文件处理,意思是没进行占位符的变量赋值就被使用了,去掉sqlSessionFactory这个属性,我最开始的配置,如下所示:

 1 <!-- 配置 转换器,对于在basePackage设置的包(包括子包)下的接口类, 
 2      如果接口类的全类名在Mapper.xml文件中和定义过命名空间一致, 
 3      将被转换成spring的bean,在调用 的地方通过@Autowired方式将可以注入接口实例 
 4 -->
 5 <!-- 注意: 1、配置mybatis的代理接口开发。 
 6            2、接口类名和映射文件必须同名。 
 7            3、接口类和映射文件必须在同一个目录下。 
 8            4、接口的映射文件的namespace名称必须是接口的全限定名。 
 9            5、接口的方法名必须和映射的statement的id一致。 -->
10 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
11     <property name="sqlSessionFactory" ref="sqlSessionFactory" />
12     <!-- 扫描所有dao接口的实现,加入到ioc容器中 -->
13     <property name="basePackage" value="com.flea.dao" />
14 </bean>

去掉sqlSessionFactory这个属性,之后,变成下面的样子了,如下所示:

1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
2     <!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
3     <!-- 扫描所有dao接口的实现,加入到ioc容器中 -->
4     <property name="basePackage" value="com.flea.dao" />
5 </bean>

我的session工厂配置,同时记得加上这样的bean,id的值是sqlSessionFactory,因为要使用的时候会去找这个bean去连接数据库。

1 <!-- 配置session工厂 -->
2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
3     <property name="dataSource" ref="dataSource" />
4     <property name="configLocation" value="classpath:mybatils.cfg.xml" />
5     <!--配置扫描式加载SQL映射文件,记得去掉mybatis-config配置 -->
6     <property name="mapperLocations" value="classpath:com/flea/dao/*.xml" />
7 </bean>

重启项目,测试,发现已经正常了。

2、此时,如果操作数据库发现乱码,可以修改url的字符集,我的jdbc.properties配置,如下所示:

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://127.0.0.1:3306/laboratory_sys?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true
3 jdbc.username=root
4 jdbc.password=123456

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • MyBatis框架基础知识(01)

    使用MyBatis框架实现数据库编程时,只需要指定各个功能对应的抽象方法及需要执行的SQL语句即可。

    海拥
  • 浅谈mybatis如何半自动化解耦和ORM实现

    3.核心处理层包括:配置解析、配置映射、SQL解析、SQL执行、结果集映射、插件等

    IT大咖说
  • MyBatis入门案例-XMl

    在resource目录下新建mapper目录,存放BrandMapperInterFace.xml、ProductMapperInterFace.xml

    用户4870038
  • 五种方式让你在java中读取properties文件内容不再是难题

      最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题。就借此机会...

    阿豪聊干货
  • 全网最全、最新MyBatis框架核心知识,收藏这一篇就够了!

    ?大家好!我是你们的老朋友Java学术趴,今天继续给大家分享小趴经过一个星期整理出来的MaBatis框架核心知识。其中包含框架介绍、入门案例、常用接口、动态代理...

    Java学术趴
  • 「2020最新」Spring最易学习教程 4—整合Mybatis 事务控制

    到目前为止web.xml中出现的标签:servlet filter listener context-param。

    鹿老师的Java笔记
  • mybatis与spring整合步骤以及自己遇到的错误

    这是作者已经整理好的链接所需jar包整合 2.创建一个实现类去继承我们之前写过的接口 如下

    萌萌哒的瓤瓤
  • maven常用插件

    http://maven.apache.org/plugins/maven-assembly-plugin/

    编程随笔
  • Spring+SpringMVC+Mybatis整合开发思路及配置详解(二)

    本文承接上一篇文章《Spring+SpringMVC+Mybatis整合开发思路及配置详解(一)》,上一篇文章我们回顾了SSM的相关知识,讲述了一个SSM项目的...

    roobtyan
  • 快速学习-MyBatis全局配置文件

    cwl_java
  • Java面试题 - 03前言:三、框架篇:

    1. JDBC编程有什么不足?mybatis是如何解决的? 答:主要有以下几个方面:

    贪挽懒月
  • 1. JDBC概述以及入门示例

    上面的示例中,我们写死了 mysql 的 jdbc 驱动,但是有些时候可能是不固定的,也就可能是动态的。这时候,我们可以使用反射机制来创建驱动。

    Devops海洋的渔夫
  • MyBatis框架基础知识(02)

    使用@Insert或相关注解配置SQL语句时,SQL语句与抽象方法的对应关系非常直观,但是,却不便于阅读、管理各SQL语句!因为在源代码中,SQL语句的表现就是...

    海拥
  • 2020[最全]-SpringBoot讲解(最优雅-最精美-最详细)

    11 编写controller运行就好了 访问 localhost:8080//hello

    CaesarChang张旭
  • Mybatis系列全解(四):全网最全!Mybatis配置文件XML全貌详解

    做大事和做小事的难度是一样的。两者都会消耗你的时间和精力,所以如果决心做事,就要做大事,要确保你的梦想值得追求,未来的收获可以配得上你的努力。

    潘潘和他的朋友们
  • SpringBoot+SpringMVC+MyBatis快速整合搭建

    SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过...

    IT大咖说
  • MyBatis 源码分析系列文章导读

    本篇文章是我为接下来的 MyBatis 源码分析系列文章写的一个导读文章。本篇文章从 MyBatis 是什么(what),为什么要使用(why),以及如何使用(...

    田小波
  • Redis-20Spring缓存机制整合Redis

    这里用一个示例通过注解的方式整合 Spring 和 Redis,要特别注意 Redis 缓存和数据库一致性的问题。

    小小工匠
  • 「2020最新」Spring最易学习教程—IOC 以及 整合Struts2

    public class ConnectionFactoryBean implements FactoryBean<Connection> {

    鹿老师的Java笔记

扫码关注云+社区

领取腾讯云代金券