SpringBoot学习遇到的问题集合

1.Spring Boot @SpringApplicationConfiguration 不能导入的问题

较新版的Spring Boot取消了@SpringApplicationConfiguration这个注解,用@SpringBootTest就可以了

因此将原来的

import org.springframework.boot.test.SpringApplicationConfiguration;
@SpringApplicationConfiguration(classes = MockServletContext.class)

改为

@SpringBootTest(classes = MockServletContext.class)

2.

3.Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

这是因为添加了数据库组件,所以autoconfig会去读取数据源配置,而新建的项目还没有配置数据源,所以会导致异常出现。

因此需要在application.property文件里添上:

spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = xxx
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

 

4.Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

'com.mysql.jdbc.Driver'驱动类被弃用了,新的驱动类是'com.mysql.cj.jdbc.Driver'。 
这个驱动会通过SPI的方式自动加载,通常不需要人工手动加载

解决方法:jdbc.driver的属性值从com.mysql.jdbc.Driver换为com.mysql.cj.jdbc.Driver

SPI – Service Provider Interface(服务提供接口)

5.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.

服务器的时区值’�й���׼ʱ��’ (就是乱码,可能因为没有设置一个值)不能被识别,或者该值代表了多个时区。 
如果你想获得时区支持的功能,你需要用一个更具体的值来设置服务器或JDBC驱动(通过serverTimezone 属性设置)

连接的URL中需要增加时区信息

增加serverTimezone属性并设置时区值,测试UTC(世界标准时间)和GMT(格林威治时间)都可以。

jdbc.url=jdbc:mysql://localhost:3306/darkbright?serverTimezone=GMT

6.Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package

是因为application.java 文件不能直接放在main/java文件夹下,必须要建一个包把他放进去

7.springboot集成themeleaf报Namespace 'th' is not bound

在html标签中加入命名空间即可,如下

<!DOCTYPE html> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"      
                xmlns:th="http://www.thymeleaf.org">
 <head> 
    <meta charset="UTF-8"> 
        <title>添加用户</title> 
        </head> 
        <body> 
            <form th:action="@{/save}" method="post"> 
            用户姓名:<input type="text" name="name"/><br/> 
            用户密码:<input type="password" name="password" /><br/> 
            用户年龄:<input type="text" name="age" /><br/> 
     <input type="submit" value="OK"/> 
</form> 
</body> 
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值