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

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

内容简介:以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 作为数据源。


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

查看所有标签

猜你喜欢:

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

An Introduction to Genetic Algorithms

An Introduction to Genetic Algorithms

Melanie Mitchell / MIT Press / 1998-2-6 / USD 45.00

Genetic algorithms have been used in science and engineering as adaptive algorithms for solving practical problems and as computational models of natural evolutionary systems. This brief, accessible i......一起来看看 《An Introduction to Genetic Algorithms》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具