SpringBoot数据库配置

这是我参与11月更文挑战的第21天,活动详情查看:2021最后一次更文挑战

连接数据库

首先我们需要导入pom配置

				<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
复制代码

在这个包下面它包含

但是这个下面没有数据库驱动,因为我们操作的数据库是不确定,我们需要自己去导入数据库驱动

				<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
复制代码

官方也给我们做了版本仲裁,所以我们不需要导入版本,但是我们需要知道的就是,我们的数据库版本要和我们导入的版本相符,要不然版本不符合会出现问题

我的数据库是Mysql5,所以我导入的是5的驱动

我们也可以在这里修改

我们重新去引入版本,之后写一下我们的数据库配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springjdbc
    username: root
    password: 666666
    driver-class-name: com.mysql.jdbc.Driver
复制代码

这些工作完成之后我们进行测试

@Slf4j
@SpringBootTest
class SpringBoot01ApplicationTests {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Test
    void contextLoads() {

        Long aLong = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        log.info("记录总数:{}",aLong);
    }

}
复制代码

配置数据源

接下来,配置我们的数据源

我们先使用自定义的方式来配置我们的数据源

				<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>
复制代码

再用配置类去配置数据源,这回我们采用注解的形式,直接将文件中的数据库信息配置到数据源中@ConfigurationProperties("spring.datasource")

@Configuration
public class MyDataSourceConfig {

    @ConfigurationProperties("spring.datasource")
    @Bean
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }
}
复制代码

测试一下

log.info("数据源类型:{}",dataSource.getClass());
复制代码

配置监测功能

 /**
     * 配置druid的监控页功能
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> statViewServletServletRegistrationBean = new ServletRegistrationBean<>(statViewServlet, "/druid/*");
        return statViewServletServletRegistrationBean;


    }
复制代码

此时我们访问http://localhost:8080/druid/index.html

会有如下页面,

	@ResponseBody
    @GetMapping("/sql")
    public String sql(){
        Long aLong = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
        return  aLong.toString();
    }
复制代码

测试一下我们监控是否成功

请求好多次,但是没有我们的监控数据,是因为我们虽然添加的监控,但是我们并没有去打开他,

在我们的数据源配置中打开监控

添加如下代码:druidDataSource.setFilters("stat");

我们将这个打开之后再测试一遍

再打开我们web监控

	@Bean
    public FilterRegistrationBean webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> webStatFilterFilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        webStatFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return webStatFilterFilterRegistrationBean;
    }
复制代码

再打开我们的防火墙

这个就十分简单了,我们只在我们开启检测功能哪里添加一个字段就可以

druidDataSource.setFilters("stat,wall");

我们还可以给我们的监控页设置密码,毕竟不是任何人都看的

		statViewServletServletRegistrationBean.addInitParameter("loginUsername","admin");
        statViewServletServletRegistrationBean.addInitParameter("loginPassword","000000");
复制代码

自动配置数据源

我们刚刚都是自定义配置,官方也给我们打包好了自动配置

				<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
复制代码

这时候我们就不需要写什么配置类,我只需要在配置文件中,开启一下配置就可以了

    druid:
      filters: stat,wall
      stat-view-servlet:
        enabled: true
        login-username: admin
        login-password: admin
        reset-enable: false
      web-stat-filter:
        enabled: true
        url-pattern: /*
      filter:
        stat:
          slow-sql-millis: 1000
          log-slow-sql: true
          enabled: true
        wall:
          enabled: true
          config:
            drop-table-allow: false
复制代码

具体的意思大家都可以去官方文档中找到

猜你喜欢

转载自juejin.im/post/7033034893407813662