SpringBoot连接数据库时出现的bug

Spring Boot 连接数据库时参考的过程:idea建立SpringBoot项目,并连接数据库

以下是过程中出现的bug:

  1. navicat 右键点击运行SQL文件时出错

  2. 在将Service注入Web层时,ServiceImpl 使用如下代码,运行不报错,但是 userService@Autowired 下面会报红。显示 “Could not autowire.No beans of ‘UserMapper’ type found ”。

     @Autowired
     UserMapper userMapper;
    

    解决方法:将 @Autowired 改成 @Resource

    //将Service注入Web层
    @Resource
    UserService userService;
    
  3. 项目运行不了,显示错误:

    DEBUG org.springframework.boot.context.logging.ClasspathLoggingApplicationListe

    这个问题出在yml文件或者properties文件上,我的是 application.yml 文件有问题,去掉中文注释之后就解决了。

  4. 项目可以运行,但是连结数据库失败

    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里的时区不用再设置了,其实不是的。

  5. 项目可以运行,但是运行后出现警告信息:

    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
    
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值