今天学习SpringSecurity的时候,选用了springboot,因为公司用的不是boot,所以,boot一直不是很熟悉,在此连接数据库的时候,遇到了一个bug。
Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Error: GlobalConfigUtils setMetaData Fail ! Cause:java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
翻译过来就是
创建类路径资源[com/baumidou/mybatisplus/autoconfig/MybatisPlusAutoConfiguration.class]中定义的名为“sqlSessionFactory”的bean时出错:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans。BeanInstanceException:未能实例化[org.apache.ibatis.session.SqlSessionFactory]:工厂方法“SqlSessionFactory”引发了异常;嵌套异常是com.baomidou.mybatisplus.core.exeptions。MybatisPlusException:错误:GlobalConfigUtils setMetaData失败!原因:java.sql。SQLException:服务器时区值'�й���ʱ��' 无法识别或表示多个时区。如果要使用时区支持,必须配置服务器或JDBC驱动程序(通过serverTimezone配置属性),以使用更具体的时区值。
我用的相关依赖是
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--spring-security依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!--junit单元测试-->
<!--MySQL的驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok 用来简化实体类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
我的配置文件是
#spring:
# security:
# user:
# name: zlf
# password: 666
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
# username: root
# password:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springsecurity?serverTimezone=GMT%2B8
username: root
password:
我本地装的MySQL版本是5.5+的,一开始没想到时区的事情,之前做demo的时候,也没有因为时区报错,这次报错让我不明所以,我就试着加上时区操作了,结果成了
分析我错误的原因是,采用的依赖是8.0+版本的MySQL,但是没有采用HikariDataSourc数据源,也没有加时区,所以报错了。