搭建ssm运行时报错
在用ssm搭建项目报了如下一系列错误,为远程连接 MySQL 8 时的错误
完整的错误信息
org.springframework.web.util.NestedServletException: 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: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
### The error may exist in com/fj/dao/BookMapper.xml
### The error may involve com.fj.dao.BookMapper.getAllBooks
解决方案
1.驱动配置有误:driver=com.mysql.cj.jdbc.Driver
2.数据库连接地址有误:url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
3.密码或帐号有误:username=root
password=root
4.数据库未启动或无权访问
5.mysql root没有远程访问的权限,需要增加权限,增加权限的步骤如下:
进入mysql数据库:
grant all privileges on . to ‘root’@’%’ identified by ‘root’ with grant option;
flush privileges;
如果上述都没错的话就应该是第6点这个错误,我便是遇到了这个错误,困扰了好几天
6.mysql依赖引入无效,在项目中直接引入mysql-connector-8.0.16.jar包
进入file --> Project Structure -->Artifacts ,若运行的项目下没有 lib 包,就新建一个,添加所需要的所有依赖即可
注意:
在 Spring 代理 Mybatis 配置数据源的时候,会有如下的问题
这里username对应的数据,如果在配置文件中标注的key值为username的话,就不能够在Spring配置中读取到,导致连接数据库失败,这也是困惑我的一个点