16-Springboot中JdbcTemplate和数据源的关系

上一篇:15-Springboot其他数据源配置之Druid
https://blog.csdn.net/fsjwin/article/details/109742300

有了数据源也白搭,不管是内置的HikariDataSource还是用的比较多的第三方数据源DruidDataSource,还是ADataSource、BDataSrouce,因为有了数据源,的有人用起来才行呀,有人会说有呀Mybatis就可以呀,当然Mybatis这位大哥当然可以,但是本节,我们先介绍一个小弟,下节在请出大哥。

JdbcTemplate及时对DataSource的封装,使我们可以在Spring框架内部解决数据裤访问的问题。

1. JdbcTemplate和DataSource关系。

在这里插入图片描述

2. JdbcTemplate实例

写一个实例,加深印象

2.1 建表

CREATE TABLE `student`  (
  `id` int(0) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
  `age` int(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

2.2 application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1/yuhl?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    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
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

  server:
    port: 9999

2.3 DruidConfig.java

package com.yuhl;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author yuhl
 * @Date 2020/11/16 11:15
 * @Classname DruidConfig
 * @Description TODO
 */
@Configuration
public class DruidConfig {
    /*
    这样的话才会绑定上下面的属性:
        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
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return new DruidDataSource();
    }
}

2.4 JDBCTemplateController

package com.yuhl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

/**
 * @author yuhl
 * @Date 2020/11/14 15:27
 * @Classname HelloController
 * @Description TODO
 */
@RestController
public class JDBCTemplateController {

	//注入JdbcTemplate  用于操作数据库
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GetMapping("/jdbct")
    public List<Map<String, Object>> hello() {
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from student");
        return maps;
    }
}

3. JdbcTemplate测试

页面访问:
在这里插入图片描述

4. 总结

JdbcTemplate就是对数据源的封装,在不引入其他框架的基础上,在spirng的内部完成对数据库的访问和业务开发,但是在项目上我们更多会使用mybatis等框来做这件事,更少使用jdbcTemplate。

下一篇:17-Springboot整合mybatis注解版Mapper https://blog.csdn.net/fsjwin/article/details/109744677

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值