久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

如何在SpringBoot 中使用 Druid 數(shù)據(jù)庫連接池

瀏覽:84日期:2023-03-20 10:11:05

Druid是阿里開源的一款數(shù)據(jù)庫連接池,除了常規(guī)的連接池功能外,它還提供了強大的監(jiān)控和擴展功能。這對沒有做數(shù)據(jù)庫監(jiān)控的小項目有很大的吸引力。

下列步驟可以讓你無腦式的在SpringBoot2.x中使用Druid。

1.Maven中的pom文件

<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.14</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>

使用 Spring Boot-2.x時,如果未引入log4j,在配置 Druid 時,會遇到Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority的報錯。因為Spring Boot默認使用的是log4j2。

2.SpringBoot 配置文件

下面是一個完整的yml文件的,其中使用mybatis作為數(shù)據(jù)庫訪問框架

server: servlet: context-path: / session: timeout: 60m port: 8080spring: datasource: url: jdbc:mysql://127.0.0.1:9080/hospital?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull username: root password: root # 環(huán)境 dev|test|pro profiles: active: dev driver-class-name: com.mysql.cj.jdbc.Driver ########################## druid配置 ########################## type: com.alibaba.druid.pool.DruidDataSource # 初始化大小,最小,最大 initialSize: 5 minIdle: 1 maxActive: 20 # 配置獲取連接等待超時的時間 maxWait: 60000 # 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個連接在池中最小生存的時間,單位是毫秒 minEvictableIdleTimeMillis: 300000 # 校驗SQL,Oracle配置 validationQuery=SELECT 1 FROM DUAL,如果不配validationQuery項,則下面三項配置無用 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false # 打開PSCache,并且指定每個連接上PSCache的大小 poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,’wall’用于防火墻 filters: stat,wall,log4j # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 合并多個DruidDataSource的監(jiān)控數(shù)據(jù) useGlobalDataSourceStat: true # Mybatis配置mybatis: mapperLocations: classpath:mapper/*.xml,classpath:mapper/extend/*.xml configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl3.配置Druid數(shù)據(jù)源實例

由于Druid暫時不在Spring Boot中的直接支持,故需要進行配置信息的定制:

SpringBoot中的配置信息無法再Druid中直接生效,需要在Spring容器中實現(xiàn)一個DataSource實例。

import java.sql.SQLException;import javax.sql.DataSource;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import com.alibaba.druid.pool.DruidDataSource;@Configurationpublic class DruidDBConfig {private Logger logger = Logger.getLogger(this.getClass());//log4j日志@Value('${spring.datasource.url}')private String dbUrl;@Value('${spring.datasource.username}')private String username;@Value('${spring.datasource.password}')private String password;@Value('${spring.datasource.driver-class-name}')private String driverClassName;@Value('${spring.datasource.initialSize}')private int initialSize;@Value('${spring.datasource.minIdle}')private int minIdle;@Value('${spring.datasource.maxActive}')private int maxActive;@Value('${spring.datasource.maxWait}')private int maxWait;@Value('${spring.datasource.timeBetweenEvictionRunsMillis}')private int timeBetweenEvictionRunsMillis;@Value('${spring.datasource.minEvictableIdleTimeMillis}')private int minEvictableIdleTimeMillis;@Value('${spring.datasource.validationQuery}')private String validationQuery;@Value('${spring.datasource.testWhileIdle}')private boolean testWhileIdle;@Value('${spring.datasource.testOnBorrow}')private boolean testOnBorrow;@Value('${spring.datasource.testOnReturn}')private boolean testOnReturn;@Value('${spring.datasource.poolPreparedStatements}')private boolean poolPreparedStatements;@Value('${spring.datasource.maxPoolPreparedStatementPerConnectionSize}')private int maxPoolPreparedStatementPerConnectionSize;@Value('${spring.datasource.filters}')private String filters;@Value('{spring.datasource.connectionProperties}')private String connectionProperties;@Bean // 聲明其為Bean實例@Primary // 在同樣的DataSource中,首先使用被標注的DataSourcepublic DataSource dataSource() {DruidDataSource datasource = new DruidDataSource();datasource.setUrl(dbUrl);datasource.setUsername(username);datasource.setPassword(password);datasource.setDriverClassName(driverClassName);// configurationdatasource.setInitialSize(initialSize);datasource.setMinIdle(minIdle);datasource.setMaxActive(maxActive);datasource.setMaxWait(maxWait);datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);datasource.setValidationQuery(validationQuery);datasource.setTestWhileIdle(testWhileIdle);datasource.setTestOnBorrow(testOnBorrow);datasource.setTestOnReturn(testOnReturn);datasource.setPoolPreparedStatements(poolPreparedStatements);datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);try {datasource.setFilters(filters);} catch (SQLException e) {//logger.error('druid configuration initialization filter', e);logger.error('druid configuration initialization filter', e);e.printStackTrace();}datasource.setConnectionProperties(connectionProperties);return datasource;}}4.過濾器和Servlet

還需要實現(xiàn)一個過濾器和Servlet,用于訪問統(tǒng)計頁面。

過濾器

import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;import com.alibaba.druid.support.http.WebStatFilter;@WebFilter(filterName='druidWebStatFilter',urlPatterns='/*', initParams={ @WebInitParam(name='exclusions',value='*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*')//忽略資源 })public class DruidStatFilter extends WebStatFilter { }

Servlet

import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet; import com.alibaba.druid.support.http.StatViewServlet; @WebServlet(urlPatterns='/druid/*', initParams={ @WebInitParam(name='allow',value='127.0.0.1,192.168.163.1'),// IP白名單(沒有配置或者為空,則允許所有訪問) @WebInitParam(name='deny',value='192.168.1.73'),// IP黑名單 (存在共同時,deny優(yōu)先于allow) @WebInitParam(name='loginUsername',value='admin'),// 用戶名 @WebInitParam(name='loginPassword',value='admin'),// 密碼 @WebInitParam(name='resetEnable',value='false')// 禁用HTML頁面上的“Reset All”功能})public class DruidStatViewServlet extends StatViewServlet {private static final long serialVersionUID = -2688872071445249539L; }5.使用@ServletComponentScan注解,

使得剛才創(chuàng)建的Servlet,F(xiàn)ilter能被訪問,SpringBoot掃描并注冊。

@SpringBootApplication()@MapperScan('cn.china.mytestproject.dao')//添加servlet組件掃描,使得Spring能夠掃描到我們編寫的servlet和filter@ServletComponentScanpublic class MytestprojectApplication { public static void main(String[] args) { SpringApplication.run(MytestprojectApplication.class, args); } }6.Dao層

接著Dao層代碼的實現(xiàn),可以使用mybatis,或者JdbcTemplate等。此處不舉例。

7.運行

訪問http://localhost:8080/druid/login.html地址即可打開登錄頁面,賬號在之前的Servlet代碼中。

如何在SpringBoot 中使用 Druid 數(shù)據(jù)庫連接池

至此完成。

要了解更多,訪問:https://github.com/alibaba/druid

以上就是SpringBoot 中使用 Druid 數(shù)據(jù)庫連接池的實現(xiàn)步驟的詳細內(nèi)容,更多關(guān)于SpringBoot 中使用 Druid 數(shù)據(jù)庫連接池的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产综合亚洲精品一区二 | 仙人掌旅馆在线观看 | 成人精品一区二区三区中文字幕 | 亚洲午夜精品一区二区三区他趣 | 久久久久久亚洲精品 | 毛片91| 久久视频在线 | 欧美一区二区三区精品免费 | 国产做a | 欧美一区二区三区视频 | 97人人草| 欧美极品一区二区 | 国产精品久久久久久久久久久久久久 | 97影院在线午夜 | 免费一级欧美在线观看视频 | 国产亚洲精品精品国产亚洲综合 | 国产一级免费视频 | 精品国产乱码久久久久久久软件 | 国产一区网站 | 亚洲福利一区 | 欧美白人做受xxxx视频 | 久久久www成人免费精品 | 国产成人精品久久 | 日韩电影一区 | 免费黄在线观看 | 日韩精品视频免费专区在线播放 | 欧美一级做a爰片免费视频 亚洲精品一区在线观看 | 黄色av网站在线观看 | 欧洲精品 | 亚洲九九 | 成人免费av | 亚洲精品成人av | 中文字幕第100页 | 99久久久99久久国产片鸭王 | 午夜激情免费在线观看 | 国产一区二区三区色淫影院 | 国产一级一级毛片女人精品 | 精品少妇一区二区三区日产乱码 | www精品美女久久久tv | 亚洲 精品 综合 精品 自拍 | 国产色 |