Springboot--整合JDBC

IDEA新建 Module
在这里插入图片描述
在这里插入图片描述
使用 YML 配置数据库信息
新建 resources/application.yml

spring:
  datasource:
    username: root
    password: xxxxxxxx
    #mysql8版本以上的驱动包,需要指定以下时区
    url: jdbc:mysql://127.0.0.1:3306/jdbc_db?serverTimezone=GMT%2B8
    #mysql8版本以上指定新的驱动包
    driver-class-name: com.mysql.cj.jdbc.Driver

在 如下文件中 添加测试代码
在这里插入图片描述

package com.iris.springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot07JdbcApplicationTests {

    @Autowired
    DataSource dataSource;
    @Test
    public void contextLoads() throws SQLException {
        System.out.println("dataSource:  " + dataSource.getClass());
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
}

测试前 校验一下 Mysql 是否启动
在这里插入图片描述
在这里插入图片描述
测试一下,连接成功
在这里插入图片描述
将需要的数据导入到数据库中

查询数据

新建控制层
com.iris.springboot.controller.ProviderController.java

package com.iris.springboot.controller;

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.RestController;

import java.util.List;
import java.util.Map;

@RestController   //等价于 @Controller 与 @ResponseBody 两个注解
public class ProviderController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/providers")
    public Map<String,Object> list(){
        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from provider");
        System.out.println(maps);
        Map<String, Object> map = maps.get(0);
        return map;
    }
}

在这里插入图片描述

配置 阿里 Druid 连接池

maven 中查找 Druid 依赖
maven repository 官网
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
将依赖添加到 pox.xml

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.18</version>
</dependency>

resources/application.yml 中添加全局变量

spring:
  datasource:
    username: root
    password: 13718407978
    #mysql8版本以上的驱动包,需要指定以下时区
    url: jdbc:mysql://127.0.0.1:3306/jdbc_db?serverTimezone=GMT%2B8
    #mysql8版本以上指定新的驱动包
    driver-class-name: com.mysql.cj.jdbc.Driver
    #引入Druid数据源
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 8
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionMillis: 60000
    minEvicitableIdleTimeMills: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    filters: stat,wall,logback
    maxPoolPreparedStatementPerConnectionSize: 25
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMills=500

测试一下
在这里插入图片描述
使用Debug 查看 上述参数是否绑定
在这里插入图片描述
上述参数未绑定,创建自动配置类实现绑定
com.iris.springboot.config.DruidConfig.java

package com.iris.springboot.config;

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;

@Configuration
public class DruidConfig {

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

在这里插入图片描述

配置Druid监控

com.iris.springboot.config.DruidConfig.java 新增配置监控

/*配置一个duid*/
//1.配置一个druid的后台,管理servlet
//2. 配置一个druid后台管理servlet
@Bean
public ServletRegistrationBean statViewServlet(){
    //注意请求 /druid/*
    ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
    //设置初始化参数值
    Map<String,String> initParam = new HashMap<>();
    initParam.put(StatViewServlet.PARAM_NAME_USERNAME,"root");
    initParam.put(StatViewServlet.PARAM_NAME_PASSWORD,"123");
    //如果不写,默认所有if都允许访问
    initParam.put(StatViewServlet.PARAM_NAME_ALLOW, "");
    //禁止访问的ip
    initParam.put(StatViewServlet.PARAM_NAME_DENY, "192.168.10.1");
    bean.setInitParameters(initParam);
    return bean;
}

@Bean
public FilterRegistrationBean webStatFilter(){
    FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
    bean.setFilter(new WebStatFilter());

    Map<String,String> initParams = new HashMap<>();
    initParams.put(WebStatFilter.PARAM_NAME_EXCLUSIONS,"*.js,*.css,/druid/*");
    bean.setInitParameters(initParams);

    //设置拦截请求
    bean.setUrlPatterns(Arrays.asList("/"));
    return bean;
}

浏览器输入 localhost:8080/druid
在这里插入图片描述
输入代码中的 用户与密码
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值