首次spring boot 配置 ,测试通过jdbc ,连接mysql8.0.1
1:pom.xml
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency>
2:application.yml
spring: datasource: #注意下行比不是mysql8.0多一个cj. driver-class-name: com.mysql.cj.jdbc.Driver username: root password: 123456 url: jdbc:mysql://localhost:3306/recruit_gather?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
3:测试类代码
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @SpringBootTest class SpringDataJdbc06ApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() throws SQLException { System.out.println("*******************"); System.out.println(dataSource.getClass()); Connection connection = dataSource.getConnection(); System.out.println("connection连接:"+connection); } }
4:正确显示结果