首页 热点资讯 义务教育 高等教育 出国留学 考研考公

spring整合myBatis配置数据源老报错,求大神帮忙看看

发布网友 发布时间:2022-04-23 08:20

我来回答

2个回答

懂视网 时间:2022-04-09 09:09

          nested exception is org.apache.commons.dbcp.SQLNestedException

六月 27, 2016 3:13:45 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [SpringMVC] in context with path [/blog] threw exception 
   [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.
   Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;
   nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
   (Access denied for user ‘sks‘@‘localhost‘ (using password: YES)) ### The error may exist in file [F:eclipse_workspace.metadata.pluginsorg.eclipse.wst.server.core mp0wtpwebappslogWEB-INFclassescomlogmappingUserMapper.xml] ### The error may involve com.blog.dao.UserMapper.selectByPrimaryKey ### The error occurred while executing a query   Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;
   nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory
  (Access denied for user ‘sks‘@‘localhost‘ (using password: YES))] with root cause java.sql.SQLException: Access denied for user ‘sks‘@‘localhost‘ (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:41) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1748) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321) at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832) at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:409) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344) at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38) at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:294) at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1247) at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1221) at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880) at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111) at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77) at org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:81) at org.mybatis.spring.transaction.SpringManagedTransaction.getConnection(SpringManagedTransaction.java:67) at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:279) at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:72) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:59) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:137) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:96) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:77) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358) at com.sun.proxy.$Proxy12.selectOne(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:163) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:68) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) at com.sun.proxy.$Proxy13.selectByPrimaryKey(Unknown Source) at com.blog.service.impl.UserServiceImpl.getUserById(UserServiceImpl.java:19) at com.blog.controller.UserController.showUser(UserController.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446) at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852) at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1753) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1712) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

二、配置错误的jdbc.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/blog
username=root
password=root

#定义初始连接数 
initialSize=0 
#定义最大连接 
maxActive=20 
#定义最大空闲 
maxIdle=20 
#定义最小空闲 
minIdle=1 
#定义最长等待时间 
maxWait=60000

配置错误的spring-mybatis.xml:

   <!-- 引入.properties属性文件 -->
 <context:property-placeholder location="classpath:*.properties"/> 
 <!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location" ref="classpath:jdbc.properties"></property>
 </bean> -->
 
 <!-- 配置数据源 -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
 <property name="driverClassName" value="${driver}" /> 
 <property name="url" value="${url}" /> 
 <property name="username" value="${username}" /> 
 <property name="password" value="${password}" /> 
 <!-- 初始化连接大小 --> 
 <property name="initialSize" value="${initialSize}"></property> 
 <!-- 连接池最大数量 --> 
 <property name="maxActive" value="${maxActive}"></property> 
 <!-- 连接池最大空闲 --> 
 <property name="maxIdle" value="${maxIdle}"></property> 
 <!-- 连接池最小空闲 --> 
 <property name="minIdle" value="${minIdle}"></property> 
 <!-- 获取连接最大等待时间 --> 
 <property name="maxWait" value="${maxWait}"></property> 
 </bean>

三、spring官网源码文档:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">

 <!-- Scans within the base package of the application for @Component classes to configure as beans -->
 <context:component-scan base-package="org.springframework.docs.test" />

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
 <property name="driverClassName" value="${jdbc.driverClassName}"/>
 <property name="url" value="${jdbc.url}"/>
 <property name="username" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>
 </bean>

 <context:property-placeholder location="jdbc.properties"/>
</beans>

四、参照官网源码修改后能正确连接:

修改后的jdbc.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/blog
jdbc.username=root
jdbc.password=root
#定义初始连接数 
jdbc.initialSize=0 
#定义最大连接 
jdbc.maxActive=20 
#定义最大空闲 
jdbc.maxIdle=20 
#定义最小空闲 
jdbc.minIdle=1 
#定义最长等待时间 
jdbc.maxWait=60000

修改后的spring-mybatis.xml:

<!-- 引入.properties属性文件 -->
 <context:property-placeholder location="classpath:*.properties"/> 
 <!-- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location" ref="classpath:jdbc.properties"></property>
 </bean> -->
 
 <!-- 配置数据源 -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
 destroy-method="close"> 
 <property name="driverClassName" value="${jdbc.driver}" /> 
 <property name="url" value="${jdbc.url}" /> 
 <property name="username" value="${jdbc.username}" /> 
 <property name="password" value="${jdbc.password}" /> 
 <!-- 初始化连接大小 --> 
 <property name="initialSize" value="${jdbc.initialSize}"></property> 
 <!-- 连接池最大数量 --> 
 <property name="maxActive" value="${jdbc.maxActive}"></property> 
 <!-- 连接池最大空闲 --> 
 <property name="maxIdle" value="${jdbc.maxIdle}"></property> 
 <!-- 连接池最小空闲 --> 
 <property name="minIdle" value="${jdbc.minIdle}"></property> 
 <!-- 获取连接最大等待时间 --> 
 <property name="maxWait" value="${jdbc.maxWait}"></property> 
 </bean>

 

myBatis数据库连接配置错误

标签:

热心网友 时间:2022-04-09 06:17

意思说你的sqlSessionFactoty配置有问题,估计是你的数据源配置错了,给你段代码参考吧

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
    <property name="username" value="xxx" />
    <property name="password" value="xxxxxxx" />
</bean>
<!--hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.test.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
</props>
</property>
</bean>

追问

=================================================

它是说我配置的dateSource有问题,setPropertyValues的时候出问题了,但是我找不出来哪里有问题

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com