【spring boot】第6篇:spring boot 整合 jdbc 数据源

栏目: Java · 发布时间: 7年前

内容简介:以jdbc的形式访问mysql数据库是比较基础的知识,理解spring boot中如何使用jdbc对我们理解spring boot对mybatis等数据框架是很有意义的。配置数据库相关信息运行结果:

简述

以jdbc的形式访问 mysql 数据库是比较基础的知识,理解spring boot中如何使用jdbc对我们理解spring boot对mybatis等数据框架是很有意义的。

spring boot 整合 jdbc 的步骤

引入 starts 启动器

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
        </dependency>
</dependencies>

配置 application.yaml 文件

配置数据库相关信息

spring:
  datasource:
    data-username: root
    data-password: root
    url: jdbc:mysql://127.0.0.1:3306/sff_test
    driver-class-name: com.mysql.jdbc.Driver

运行测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootJdbcApplicationTests {
    @Autowired
    private DataSource dataSource;
    @Test
    public void testDataSource() throws SQLException {

        System.out.println("dataSource类型:" + dataSource.getClass());

        Connection connection = dataSource.getConnection();
        System.out.println("connection连接:" + connection);
        connection.close();
    }
}

运行结果:

【spring boot】第6篇:spring boot 整合 jdbc 数据源

从结果中可以看到 spring boot 的 2.1.2.RELEASE 版本默认使用 com.zaxxer.hikari.HikariDataSource 作为数据源。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Build Your Own Web Site the Right Way Using HTML & CSS

Build Your Own Web Site the Right Way Using HTML & CSS

Ian Lloyd / SitePoint / 2006-05-02 / USD 29.95

Build Your Own Website The Right Way Using HTML & CSS teaches web development from scratch, without assuming any previous knowledge of HTML, CSS or web development techniques. This book introduces you......一起来看看 《Build Your Own Web Site the Right Way Using HTML & CSS》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具