当前位置:Gxlcms > 数据库问题 > SpringBoot JDBC访问数据库

SpringBoot JDBC访问数据库

时间:2021-07-01 10:21:17 帮助过:9人阅读

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.application.yml配置

spring:
  datasource:
    username: 数据库名
    password: 密码
    url: jdbc:mysql://192.168.0.1:3306/数据库
    driver-class-name: com.mysql.cj.jdbc.Driver

3.数据库连接测试

@SpringBootTest
class DemoApplicationTests {

  @Autowired
  DataSource dataSource;

  @Test
  void contextLoads() throws SQLException {
    System.out.println(dataSource.getClass());
    Connection connection = dataSource.getConnection();
    System.out.println(connection);
    connection.close();//关闭连接
  }
}

SpringBoot JDBC访问数据库

标签:text   print   关闭   cat   etc   system   row   span   except   

人气教程排行