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>