spirngBoot连接msql

风格不统一 提交于 2020-12-12 03:42:08

注意事项:配置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();
	
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!