最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot集成Druid數(shù)據(jù)庫連接池

 更新時間:2017年04月25日 11:44:36   作者:郭尋撫  
這篇文章主要介紹了Spring Boot集成Druid數(shù)據(jù)庫連接池,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1. 前言

Druid數(shù)據(jù)庫連接池由阿里巴巴開源,號稱是java語言中最好的數(shù)據(jù)庫連接池,是為監(jiān)控而生的。Druid的官方地址是:https://github.com/alibaba/druid

通過本文,我們可以看到

  1. Spring Boot 如何配置數(shù)據(jù)源
  2. Spring Boot 如何集成Druid數(shù)據(jù)庫連接池
  3. 如何打開并訪問Druid數(shù)據(jù)庫連接池的監(jiān)控功能
  4. Spring Boot 使用JdbcTemplate操作數(shù)據(jù)庫

2. 配置pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.3.5.RELEASE</version>
</parent>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.0.20</version>
  </dependency>
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
  </dependency>
</dependencies>

3. 在application.properties中配置數(shù)據(jù)源

# 數(shù)據(jù)庫訪問配置,此處使用postgres為例。
# 主數(shù)據(jù)源,默認的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://192.168.1.9/jianshudb
spring.datasource.username=postgres
spring.datasource.password=yourpassword

# 下面為連接池的補充設(shè)置,應用到上面所有數(shù)據(jù)源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置獲取連接等待超時的時間
spring.datasource.maxWait=60000
# 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一個連接在池中最小生存的時間,單位是毫秒 
spring.datasource.minEvictableIdleTimeMillis=300000
# Oracle請使用select 1 from dual
spring.datasource.validationQuery=SELECT 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打開PSCache,并且指定每個連接上PSCache的大小 
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻 
spring.datasource.filters=stat,wall,slf4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
#spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個DruidDataSource的監(jiān)控數(shù)據(jù)
#spring.datasource.useGlobalDataSourceStat=true

DruidDataSource參考配置:
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_DruidDataSource%E5%8F%82%E8%80%83%E9%85%8D%E7%BD%AE

4. 打開Druid的監(jiān)控統(tǒng)計功能

Druid的監(jiān)控統(tǒng)計功能是通過filter-chain擴展實現(xiàn),如果你要打開監(jiān)控統(tǒng)計功能,需要配置StatFilter,相關(guān)代碼如下。

@Configuration
public class DruidConfiguration {

 private static final Logger log = LoggerFactory.getLogger(DruidConfiguration.class);

 @Bean
 public ServletRegistrationBean druidServlet() {
  log.info("init Druid Servlet Configuration ");
  ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
  servletRegistrationBean.setServlet(new StatViewServlet());
  servletRegistrationBean.addUrlMappings("/druid/*");
  Map<String, String> initParameters = new HashMap<String, String>();
  initParameters.put("loginUsername", "admin");// 用戶名
  initParameters.put("loginPassword", "admin");// 密碼
  initParameters.put("resetEnable", "false");// 禁用HTML頁面上的“Reset All”功能
  initParameters.put("allow", ""); // IP白名單 (沒有配置或者為空,則允許所有訪問)
  //initParameters.put("deny", "192.168.20.38");// IP黑名單 (存在共同時,deny優(yōu)先于allow)
  servletRegistrationBean.setInitParameters(initParameters);
  return servletRegistrationBean;
 }

 @Bean
 public FilterRegistrationBean filterRegistrationBean() {
  FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  filterRegistrationBean.setFilter(new WebStatFilter());
  filterRegistrationBean.addUrlPatterns("/*");
  filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  return filterRegistrationBean;
 }

}

等應用啟動后,可以訪問地址:http://localhost:8080/druid/,用戶名和密碼見上述代碼中的設(shè)置,即admin/admin。

5. 使用JdbcTemplate操作數(shù)據(jù)庫

假設(shè)數(shù)據(jù)庫中有表t_user,其中id=1的user的username為ZhangSan。下面的例子演示了通過id查找username的情況。

@RestController
public class DemoController {

  @Autowired
  JdbcTemplate jdbcTemplate;

  @RequestMapping(value = "/hello.do", method = RequestMethod.GET)
  public String hello(@RequestParam(value = "id", required = true) Integer id) {
    String name = getNameById(id);
    return (name == null) ? "Hello World" : ("Hello " + name);
  }

  public String getNameById(Integer id) {
    String sql = "select username from t_user where id = ? ";
    List<String> list = jdbcTemplate.queryForList(sql, new Object[] {id}, String.class);
    return list.isEmpty() ? null : list.get(0);
  }

}

訪問地址:http://localhost:8080/hello.do?id=1

結(jié)果輸出:Hello, ZhangSan

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

相關(guān)文章

  • IntelliJ IDEA 中使用jRebel進行 Java 熱部署教程圖解

    IntelliJ IDEA 中使用jRebel進行 Java 熱部署教程圖解

    Rebel是一款JAVA虛擬機插件,它使得JAVA程序員能在不進行重部署的情況下,即時看到代碼的改變對一個應用程序帶來的影響。本文通過圖文并茂的形式給大家介紹了IntelliJ IDEA 中使用jRebel進行 Java 熱部署教程圖解,需要的朋友參考下吧
    2018-04-04
  • Java基礎(chǔ)篇之對象數(shù)組練習

    Java基礎(chǔ)篇之對象數(shù)組練習

    對象數(shù)組就是數(shù)組里的每個元素都是類的對象,賦值時先定義對象,然后將對象直接賦給數(shù)組就行了,這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)篇之對象數(shù)組練習的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • ZooKeeper集群操作及集群Master選舉搭建啟動

    ZooKeeper集群操作及集群Master選舉搭建啟動

    這篇文章主要為大家介紹了ZooKeeper集群操作及集群Master選舉搭的建啟動詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 使用Spring Boot創(chuàng)建Web應用程序的示例代碼

    使用Spring Boot創(chuàng)建Web應用程序的示例代碼

    本篇文章主要介紹了使用Spring Boot創(chuàng)建Web應用程序的示例代碼,我們將使用Spring Boot構(gòu)建一個簡單的Web應用程序,并為其添加一些有用的服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Sentinel結(jié)合Nacos實現(xiàn)數(shù)據(jù)持久化過程詳解

    Sentinel結(jié)合Nacos實現(xiàn)數(shù)據(jù)持久化過程詳解

    這篇文章主要介紹了Sentinel結(jié)合Nacos實現(xiàn)數(shù)據(jù)持久化過程,要持久化的原因是因為每次啟動Sentinel都會使之前配置的規(guī)則就清空了,這樣每次都要再去設(shè)定規(guī)則顯得非常的麻煩,感興趣想要詳細了解可以參考下文
    2023-05-05
  • Windows下后端如何啟動SpringBoot的Jar項目

    Windows下后端如何啟動SpringBoot的Jar項目

    這篇文章主要介紹了Windows下后端如何啟動SpringBoot的Jar項目問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • springboot中的css樣式顯示不出了的幾種情況

    springboot中的css樣式顯示不出了的幾種情況

    這篇文章主要介紹了springboot中的css樣式顯示不出了的幾種情況,具有很好的的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • springIoc及注解的使用實例詳解

    springIoc及注解的使用實例詳解

    注解(Annotation)是一種在 Java 程序中以元數(shù)據(jù)的形式對代碼進行標記和說明的機制,它可以被添加到類、方法、字段、參數(shù)等程序元素上,用于提供額外的信息和指示,本文給大家介紹springIoc及注解的使用,感興趣的朋友一起看看吧
    2024-02-02
  • SpringCloud?Feign請求頭刪除修改的操作代碼

    SpringCloud?Feign請求頭刪除修改的操作代碼

    這篇文章主要介紹了SpringCloud?Feign請求頭刪除修改,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • 基于Rest的API解決方案(jersey與swagger集成)

    基于Rest的API解決方案(jersey與swagger集成)

    下面小編就為大家?guī)硪黄赗est的API解決方案(jersey與swagger集成)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08

最新評論

昌宁县| 兴和县| 泰宁县| 比如县| 陕西省| 泰宁县| 罗源县| 噶尔县| 伊金霍洛旗| 明光市| 远安县| 石阡县| 渭南市| 乐至县| 米易县| 大英县| 儋州市| 绥滨县| 昌都县| 宜城市| 朝阳县| 思茅市| 都昌县| 永年县| 杭州市| 青田县| 临夏市| 新密市| 东山县| 方正县| 通江县| 凤翔县| 抚顺市| 随州市| 昭觉县| 平泉县| 兴隆县| 乐昌市| 乌兰浩特市| 吐鲁番市| 无棣县|