今天就跟大家聊聊有關(guān)SpringBoot中怎么整合Druid數(shù)據(jù)源,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)建站是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷策劃、成都微信小程序、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動(dòng)互聯(lián)開(kāi)發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立十多年以來(lái),已經(jīng)為上千多家成都門簾各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)?,F(xiàn)在,服務(wù)的上千多家客戶與我們一路同行,見(jiàn)證我們的成長(zhǎng);未來(lái),我們一起分享成功的喜悅。
1.數(shù)據(jù)庫(kù)結(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時(shí) 這個(gè)會(huì)報(bào)錯(cuò) 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無(wú)法統(tǒng)計(jì),'wall'用于防火墻 filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # 合并多個(gè)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語(yǔ)句創(chuàng)建表 # - classpath:sql/employee.sql
5.創(chuàng)建一個(gè)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開(kāi)頭的 // DruidDataSource類里面的屬性與appliction.yml文件里面的spring.datasource開(kāi)頭的對(duì)應(yīng)映射 @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } //配置Druid的監(jiān)控 //1、配置一個(gè)管理后臺(tái)的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)就是允許所有訪問(wèn) initParams.put("deny",""); bean.setInitParameters(initParams); return bean; } //2、配置一個(gè)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)建一個(gè)UserController類測(cè)試
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)目,通過(guò)瀏覽器訪問(wèn) http://localhost:8080/query
8.我們DruidConfig類里配置的下面代碼可以幫我們實(shí)現(xiàn)監(jiān)控
//配置Druid的監(jiān)控 //1、配置一個(gè)管理后臺(tái)的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)就是允許所有訪問(wèn) initParams.put("deny",""); bean.setInitParameters(initParams); return bean; }
9.我們啟動(dòng)項(xiàng)目,打開(kāi)網(wǎng)址:http://localhost:8080/druid/login.html 可以通過(guò)登錄,查看druid數(shù)據(jù)源狀態(tài)監(jiān)控
我們上面設(shè)置的是用戶名:admin 密碼:123456
看完上述內(nèi)容,你們對(duì)SpringBoot中怎么整合Druid數(shù)據(jù)源有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)頁(yè)標(biāo)題:SpringBoot中怎么整合Druid數(shù)據(jù)源
網(wǎng)頁(yè)URL:http://muchs.cn/article44/igejee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航、電子商務(wù)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)