init datasource error / Error creating bean with name 'xxx' / Error creating bean with name 'dataSource' defined in class path /com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException

[2019-12-26 10:06:58.959] boot - ERROR [main] ---  , url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloWorldController': Unsatisfied dependency expressed through field 'orderRecordMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderRecordMapper' defined in file [D:ProjectJava Eclipse
mqpdemo	argetclassescomexample
mqpdemomapperOrderRecordMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/spring-jdbc.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderRecordMapper' defined in file [D:ProjectJava Eclipse
mqpdemo	argetclassescomexample
mqpdemomapperOrderRecordMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/spring-jdbc.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/spring-jdbc.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring/spring-jdbc.xml]: Invocation of init method failed; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Caused by: java.lang.NullPointerException: null

1. 修改mysql依赖驱动版本, 之前用的是5.x的版本

<mysql.version>8.0.13</mysql.version>

2. url的问题: 修改参数为characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC(参考https://www.cnblogs.com/chenxi-mxj/p/11967850.html

 

3. 检查druid配置中是否缺少driverClassName

<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>

4. Mapper文件中添加@Mapper注解

5. 启动类需要添加扫描注解

@ImportResource(locations = "classpath:spring/spring-jdbc.xml")
@MapperScan(basePackages = {"com.example.jay.mapper"})

原文地址:https://www.cnblogs.com/chenxi-mxj/p/12100664.html