Spring Boot 连接数据库时参考的过程:idea建立SpringBoot项目,并连接数据库
以下是过程中出现的bug:
-
在将Service注入Web层时,ServiceImpl 使用如下代码,运行不报错,但是
userService和@Autowired下面会报红。显示 “Could not autowire.No beans of ‘UserMapper’ type found ”。@Autowired UserMapper userMapper;解决方法:将
@Autowired改成@Resource//将Service注入Web层 @Resource UserService userService; -
项目运行不了,显示错误:
DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListe
这个问题出在yml文件或者properties文件上,我的是 application.yml 文件有问题,去掉中文注释之后就解决了。
-
项目可以运行,但是连结数据库失败。
java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
这是 application.yml 文件里的
spring:datasource:的url出错了。修改方法:
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC添加了
?serverTimezone=UTC,即调整了服务器时区。我一开始在 idea 右侧连接 Database 数据库的时候,把
serverTimezone(服务器时区)设置成了Hongkong,然后测试连接成功了。因此以为url里的时区不用再设置了,其实不是的。 -
项目可以运行,但是运行后出现警告信息:
Establishing SSL connection without server’s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn’t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
翻译之后,发现这行警告的意思如下所示:
不建议在没有服务器身份验证的情况下建立SSL连接。根据MySQL 5.5.45+、5.6.26+和5.7.6+的要求,如果没有设置explicit选项,默认情况下必须建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任库。
因此我在
spring:datasource的 url 后面加上了&useSSL=false,之后程序运行,不再出现该警告。这里附上我的
spring:datasource设置:spring: datasource: name: test url: jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver

1948

被折叠的 条评论
为什么被折叠?



