Spring连接数据库问题
今天练习JavaEE中Spring连接数据库时总是报以下错误:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause:org.springframework.jdbc.CannotGetJdbcConnectionException:
Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user '将将'@'localhost' (using password: YES)
### The error may exist in file [E:\IntelliJ IDEA 2021.1.1\Files\Web Test\ssmBoot\target\classes\com\web\mapperOne\CustomerMapper.xml]
### The error may involve com.web.mapperOne.CustomerMapper.selectAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user '将将'@'localhost' (using password: YES)
db.properties配置如下:
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
username = root
password = 123456
数据库连接依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
applicationContext数据库配置:
<!--读取db.properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据库-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
经查找发现将applicationContext.xml配置中的"${username}“改为"root”:
<!--读取db.properties-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置数据库-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="root"/>
<property name="password" value="${password}"/>
</bean>
或者 将properties中的 username 改为 jdbc.username
然后见applicationContext.xml中配置"${username}"中username改为jdbc.username