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

简述

以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:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/sff_test
    driver-class-name: com.mysql.jdbc.Driver

这些配置信息都封装在org.springframework.boot.autoconfigure.jdbc.DataSourceProperties类中

运行测试

@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 的 2.1.2.RELEASE 版本默认使用com.zaxxer.hikari.HikariDataSource 作为数据源。

spring boot 数据源自动配置原理

spring boot 对 jdbc 的自动配置类都封装在org.springframework.boot.autoconfigure.jdbc包下。

DataSourceConfiguration

  • 该配置类中定义了spring boot支持的默认数据源种类

    • org.apache.tomcat.jdbc.pool.DataSource
    • com.zaxxer.hikari.HikariDataSource
    • org.apache.commons.dbcp2.BasicDataSource
    • 通过spring.datasource.type属性指定自定义数据源类型,比如druid、 c3p0等

JdbcTemplate

spring boot 自动配置了 JdbcTemplate 数据操作模板。JdbcTemplate 是 spring 中提供对数据增删改查操的封装模板,可以理解成一个工具类,也就相当于你自己实现一个JdbcUtils工具类一样。
spring boot 中如何使用 JdbcTemplate 呢?直接注入就可以可以使用了。

@Controller
public class JdbcController {

    @Autowired
    private JdbcTemplate jdbcTemplate; //注入 JdbcTemplate 模板

    @ResponseBody
    @RequestMapping("/jdbc")
    public String query() {
        List<Map<String, Object>> deptList = jdbcTemplate.queryForList("SELECT id,dept_name FROM dept");
        return deptList.toString();
    }
}

使用自定义的数据源 Druid

  • 导入依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.12</version>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.1.10</version>
</dependency>
  • 修改 yaml 文件配置
spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/sff_test
    driver-class-name: com.mysql.jdbc.Driver
    # 指定自己使用的数据源
    type: com.alibaba.druid.pool.DruidDataSource

    # DruidDataSource 其他属性配置
    druid:
     initialSize: 5
     minIdle: 5
     maxActive: 20
     maxWait: 60000
     timeBetweenEvictionRunsMillis: 60000
     minEvictableIdleTimeMillis: 300000
     validationQuery: SELECT 1 FROM DUAL
     testWhileIdle: true
     testOnBorrow: false
     testOnReturn: false
     poolPreparedStatements: true
     maxPoolPreparedStatementPerConnectionSize: 20
     useGlobalDataSourceStat: true
     filter:
      stat:
       enabled: true
       log-slow-sql: true
      wall:
       enabled: true

  • 新增druid的配置类
@Configurable
public class DruidConfig {
    
    @Bean
    @ConfigurationProperties("spring.datasource.druid")
    public DataSource dataSourceTwo(){
        return DruidDataSourceBuilder.create().build();
    }
}

然后我们再debug运行 SpringBootJdbcApplicationTests 的 testDataSource 方法发现此时DruidDataSource其他属性有起作用,比如:initialSize等。,如下图展示:
图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值