注意事项:配置mysql-connector-java的版本,
//不同版本的mysql的驱动不一样
//比较低的版本 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
//高版本 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
//配置文档
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
public class Test3ApplicationTests {
@Autowired DataSource dataSource;
[@Test](https://my.oschina.net/azibug)
public void contextLoads() throws SQLException {
System.out.println(dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
}
}
来源:oschina
链接:https://my.oschina.net/u/2511906/blog/3070722