欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合Druid數(shù)據(jù)源過程詳解

 更新時間:2019年12月02日 10:26:44   作者:Amy清風(fēng)  
這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了SpringBoot整合Druid數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1.數(shù)據(jù)庫結(jié)構(gòu)

2.項(xiàng)目結(jié)構(gòu)

3.pom.xml文件

<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>
 
  <!--引入druid數(shù)據(jù)源 -->
  <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.8</version>
  </dependency>
 
  <!-- https://mvnrepository.com/artifact/log4j/log4j -->
  <!-- 如果 不加入這依賴    配置監(jiān)控統(tǒng)計(jì)攔截的filters時  這個會報錯 filters: stat,wall,log4j  -->
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>
 
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 
 
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
 
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build> 

4.application.yml配置文件

spring:
 datasource:
  username: root
  password: wangqing
  url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
  driver-class-name: com.mysql.jdbc.Driver
  type: com.alibaba.druid.pool.DruidDataSource
 
 
 #  數(shù)據(jù)源其他配置
  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
#  配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
  filters: stat,wall,log4j
  maxPoolPreparedStatementPerConnectionSize: 20
  useGlobalDataSourceStat: true
  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
  # 合并多個DruidDataSource的監(jiān)控?cái)?shù)據(jù)
  #useGlobalDataSourceStat: true
   
mybatis:
 # 指定全局配置文件位置
 #config-location: classpath:mybatis/mybatis-config.xml
 # 指定sql映射文件位置
 mapper-locations: classpath:mapper/*.xml      #如src/main/resources下的mappers文件下的TUserMapper.xml
 
#  schema:
#   - classpath:sql/department.sql     #根據(jù)department.sql 的sql語句創(chuàng)建表
#   - classpath:sql/employee.sql 

5.創(chuàng)建一個DruidConfig的配置類,實(shí)例化Druid Datasource

package com.qingfeng.config;
 
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
 
@Configuration
public class DruidConfig {
   //指定加載appliction.yml文件里面的spring.datasource開頭的
   // DruidDataSource類里面的屬性與appliction.yml文件里面的spring.datasource開頭的對應(yīng)映射
  @ConfigurationProperties(prefix = "spring.datasource")
  @Bean
  public DataSource druid(){
    return new DruidDataSource();
  }
 
  //配置Druid的監(jiān)控
  //1、配置一個管理后臺的Servlet
  @Bean
  public ServletRegistrationBean statViewServlet(){
    ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    Map<String,String> initParams = new HashMap<>();
 
    initParams.put("loginUsername","admin");
    initParams.put("loginPassword","123456");
    initParams.put("allow","");//默認(rèn)就是允許所有訪問
    initParams.put("deny","");
 
    bean.setInitParameters(initParams);
    return bean;
  }
 
 
  //2、配置一個web監(jiān)控的filter
  @Bean
  public FilterRegistrationBean webStatFilter(){
    FilterRegistrationBean bean = new FilterRegistrationBean();
    bean.setFilter(new WebStatFilter());
    Map<String,String> initParams = new HashMap<>();
    initParams.put("exclusions","*.js,*.css,/druid/*");
    bean.setInitParameters(initParams);
    bean.setUrlPatterns(Arrays.asList("/*"));
    return bean;
  }
}

6.創(chuàng)建一個UserController類測試

package com.qingfeng.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.List;
import java.util.Map;
 
@Controller
public class UserController {
 
  @Autowired
  JdbcTemplate jdbcTemplate;
  @ResponseBody
  @GetMapping("/query")
  public Map<String,Object> map(){
    List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM user");
    return list.get(0);
  }
}

7.運(yùn)行項(xiàng)目,通過瀏覽器訪問 http://localhost:8080/query

8.我們DruidConfig類里配置的下面代碼可以幫我們實(shí)現(xiàn)監(jiān)控

//配置Druid的監(jiān)控
  //1、配置一個管理后臺的Servlet
  @Bean
  public ServletRegistrationBean statViewServlet(){
    ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    Map<String,String> initParams = new HashMap<>();
 
    initParams.put("loginUsername","admin");
    initParams.put("loginPassword","123456");
    initParams.put("allow","");//默認(rèn)就是允許所有訪問
    initParams.put("deny","");
 
    bean.setInitParameters(initParams);
    return bean;
  }

9.我們啟動項(xiàng)目,打開網(wǎng)址:http://localhost:8080/druid/login.html 可以通過登錄,查看druid數(shù)據(jù)源狀態(tài)監(jiān)控

我們上面設(shè)置的是用戶名:admin 密碼:123456

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實(shí)現(xiàn)歸并排序的示例代碼

    Java實(shí)現(xiàn)歸并排序的示例代碼

    歸并排序是建立在歸并操作上的一種有效的排序算法。該算法是采用分治法(Divide?and?Conquer)的一個非常典型的應(yīng)用。本文將用Java實(shí)現(xiàn)這一排序,需要的可以參考一下
    2022-08-08
  • Java實(shí)現(xiàn)二叉搜索樹的插入、刪除功能

    Java實(shí)現(xiàn)二叉搜索樹的插入、刪除功能

    這篇文章主要介紹了Java實(shí)現(xiàn)二叉搜索樹的插入、刪除,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • Spring的BeanUtils.copyProperties屬性復(fù)制避坑指南

    Spring的BeanUtils.copyProperties屬性復(fù)制避坑指南

    這篇文章主要介紹了Spring的BeanUtils.copyProperties屬性復(fù)制避坑指南,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Mybatis 級聯(lián)刪除的實(shí)現(xiàn)

    Mybatis 級聯(lián)刪除的實(shí)現(xiàn)

    這篇文章主要介紹了Mybatis 級聯(lián)刪除的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • SpringBoot中JPA實(shí)現(xiàn)Sort排序的三種方式小結(jié)

    SpringBoot中JPA實(shí)現(xiàn)Sort排序的三種方式小結(jié)

    這篇文章主要介紹了SpringBoot中JPA實(shí)現(xiàn)Sort排序的三種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 詳解利用SpringMVC攔截器控制Controller返回值

    詳解利用SpringMVC攔截器控制Controller返回值

    這篇文章主要介紹了詳解利用SpringMVC攔截器控制Controller返回值,通過定義一個StringResult注解,在訪問方法的時候返回StringResult中的內(nèi)容,有興趣的可以了解一下。
    2017-01-01
  • Java參數(shù)校驗(yàn)Validator與@AssertTrue深度解析

    Java參數(shù)校驗(yàn)Validator與@AssertTrue深度解析

    本文詳細(xì)介紹了Java的Validator框架及其@AssertTrue注解的使用,包括環(huán)境準(zhǔn)備、基礎(chǔ)注解介紹、實(shí)戰(zhàn)示例、@AssertTrue的深入解析、高級特性和最佳實(shí)踐建議,感興趣的朋友跟隨小編一起看看吧
    2025-01-01
  • Gradle進(jìn)階使用結(jié)合Sonarqube進(jìn)行代碼審查的方法

    Gradle進(jìn)階使用結(jié)合Sonarqube進(jìn)行代碼審查的方法

    今天小編就為大家分享一篇關(guān)于Gradle進(jìn)階使用結(jié)合Sonarqube進(jìn)行代碼審查的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Springboot如何獲取上下文ApplicationContext

    Springboot如何獲取上下文ApplicationContext

    這篇文章主要介紹了Springboot如何獲取上下文ApplicationContext,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 使用Java代碼實(shí)現(xiàn)RocketMQ的生產(chǎn)與消費(fèi)消息

    使用Java代碼實(shí)現(xiàn)RocketMQ的生產(chǎn)與消費(fèi)消息

    這篇文章介紹一下其他的小組件以及使用Java代碼實(shí)現(xiàn)生產(chǎn)者對消息的生成,消費(fèi)者消費(fèi)消息等知識點(diǎn),并通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07

最新評論