Spring通过配置文件读取数据库连接时,报下面这个错。
spring Access denied for user 'xx'@'localhost' (using password: YES)
错误原因:
在系统中也有个username属性,这时系统变量覆盖了Properties中的值,使得获取username的值为系统的用户名xx,密码为properties中的password去查询数据库,此时用户名名和密码并不匹配就会报错。在Spring完成注入时是用 “${…}” 方式获取值完成注入的。而通过这种表达式也能直接获取到JVM系统属性…
解决办法:
我们将数据库配置文件中username改为user。
修改前的db.properties配置
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
修改后的db.properties配置
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
user=root
password=123456
成功运行