druid是阿里巴巴開(kāi)源的數(shù)據(jù)庫(kù)連接池,提供了優(yōu)秀的對(duì)數(shù)據(jù)庫(kù)操作的監(jiān)控功能,本文要講解一下springboot項(xiàng)目怎么集成druid。
本文在基于jpa的項(xiàng)目下開(kāi)發(fā),首先在pom文件中額外加入druid依賴(lài),pom文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
<?xml version= "1.0" encoding= "utf-8" ?> <project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion> 4.0 . 0 </modelversion> <groupid>com.dalaoyang</groupid> <artifactid>springboot_druid</artifactid> <version> 0.0 . 1 -snapshot</version> <packaging>jar</packaging> <name>springboot_druid</name> <description>springboot_druid</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version> 1.5 . 12 .release</version> <relativepath/> <!-- lookup parent from repository --> </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-data-jpa</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <version> 1.0 . 28 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project> |
application.properties上半段和整合jpa一點(diǎn)沒(méi)變,下面加入了一些druid的配置,如果對(duì)druid的配置有什么不理解的,可以去網(wǎng)上查一下。(這篇文章我覺(jué)得寫(xiě)的很好,傳送門(mén))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#端口號(hào) server.port= 8888 ##validate 加載hibernate時(shí),驗(yàn)證創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu) ##create 每次加載hibernate,重新創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu),這就是導(dǎo)致數(shù)據(jù)庫(kù)表數(shù)據(jù)丟失的原因。 ##create-drop 加載hibernate時(shí)創(chuàng)建,退出是刪除表結(jié)構(gòu) ##update 加載hibernate自動(dòng)更新數(shù)據(jù)庫(kù)結(jié)構(gòu) ##validate 啟動(dòng)時(shí)驗(yàn)證表的結(jié)構(gòu),不會(huì)創(chuàng)建表 ##none 啟動(dòng)時(shí)不做任何操作 spring.jpa.hibernate.ddl-auto=create ##控制臺(tái)打印sql spring.jpa.show-sql= true ##數(shù)據(jù)庫(kù)配置 ##數(shù)據(jù)庫(kù)地址 spring.datasource.url=jdbc:mysql: //localhost:3306/test?characterencoding=utf8&usessl=false ##數(shù)據(jù)庫(kù)用戶(hù)名 spring.datasource.username=root ##數(shù)據(jù)庫(kù)密碼 spring.datasource.password=root ##數(shù)據(jù)庫(kù)驅(qū)動(dòng) spring.datasource.driver- class -name=com.mysql.jdbc.driver #這里是不同的 #使用druid的話 需要多配置一個(gè)屬性spring.datasource.type spring.datasource.type=com.alibaba.druid.pool.druiddatasource # 連接池的配置信息 # 初始化大小,最小,最大 spring.datasource.initialsize= 5 spring.datasource.minidle= 5 spring.datasource.maxactive= 20 # 配置獲取連接等待超時(shí)的時(shí)間 spring.datasource.maxwait= 60000 # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 spring.datasource.timebetweenevictionrunsmillis= 60000 # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 spring.datasource.minevictableidletimemillis= 300000 spring.datasource.validationquery=select 1 from dual spring.datasource.testwhileidle= true spring.datasource.testonborrow= false spring.datasource.testonreturn= false # 打開(kāi)pscache,并且指定每個(gè)連接上pscache的大小 spring.datasource.poolpreparedstatements= true spring.datasource.maxpoolpreparedstatementperconnectionsize= 20 # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì), 'wall' 用于防火墻 spring.datasource.filters=stat,wall,log4j # 通過(guò)connectproperties屬性來(lái)打開(kāi)mergesql功能;慢sql記錄 |
然后在項(xiàng)目中加入druidconfig,簡(jiǎn)單講解一下,這個(gè)配置文件主要是加載application.properties的配置,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
package com.dalaoyang.config; 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; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.config * @email [email protected] * @date 2018/4/12 */ @configuration public class druidconfig { private logger logger = logger.getlogger( this .getclass()); @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 @primary //主數(shù)據(jù)源 public datasource datasource(){ druiddatasource datasource = new druiddatasource(); datasource.seturl( this .dburl); datasource.setusername(username); datasource.setpassword(password); datasource.setdriverclassname(driverclassname); //configuration datasource.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 exception" , e); } datasource.setconnectionproperties(connectionproperties); return datasource; } } |
然后創(chuàng)建druidfilter,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.dalaoyang.filter; import javax.servlet.annotation.webfilter; import javax.servlet.annotation.webinitparam; import com.alibaba.druid.support.http.webstatfilter; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.filter * @email [email protected] * @date 2018/4/12 */ @webfilter (filtername= "druidwebstatfilter" ,urlpatterns= "/*" , initparams={ @webinitparam (name= "exclusions" ,value= "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*" ) //忽略資源 } ) public class druidfilter extends webstatfilter { } |
新建druidservlet,在類(lèi)上面加注解@webservlet,其中配置了登錄druid監(jiān)控頁(yè)面的賬號(hào)密碼,白名單黑名單之類(lèi)的配置,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.dalaoyang.servlet; import javax.servlet.annotation.webinitparam; import javax.servlet.annotation.webservlet; import com.alibaba.druid.support.http.statviewservlet; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.servlet * @email [email protected] * @date 2018/4/12 */ @webservlet (urlpatterns= "/druid/*" , initparams={ @webinitparam (name= "allow" ,value= "" ), // ip白名單(沒(méi)有配置或者為空,則允許所有訪問(wèn)) @webinitparam (name= "deny" ,value= "" ), // ip黑名單 (deny優(yōu)先于allow) @webinitparam (name= "loginusername" ,value= "admin" ), // 登錄druid管理頁(yè)面用戶(hù)名 @webinitparam (name= "loginpassword" ,value= "admin" ) // 登錄druid管理頁(yè)面密碼 }) public class druidservlet extends statviewservlet { } |
然后在啟動(dòng)類(lèi)加入注解@servletcomponentscan,讓項(xiàng)目掃描到servlet,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.dalaoyang; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.web.servlet.servletcomponentscan; @springbootapplication // 啟動(dòng)類(lèi)必須加入@servletcomponentscan注解,否則無(wú)法掃描到servlet @servletcomponentscan public class springbootdruidapplication { public static void main(string[] args) { springapplication.run(springbootdruidapplication. class , args); } } |
剩余的就是和整合jpa一樣的entity(實(shí)體類(lèi)),repository(數(shù)據(jù)操作層),controller(測(cè)試使用的controller),直接展示代碼。
city
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package com.dalaoyang.entity; import javax.persistence.*; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.entity * @email [email protected] * @date 2018/4/7 */ @entity @table (name= "city" ) public class city { @id @generatedvalue (strategy=generationtype.auto) private int cityid; private string cityname; private string cityintroduce; public city( int cityid, string cityname, string cityintroduce) { this .cityid = cityid; this .cityname = cityname; this .cityintroduce = cityintroduce; } public city(string cityname, string cityintroduce) { this .cityname = cityname; this .cityintroduce = cityintroduce; } public city() { } public int getcityid() { return cityid; } public void setcityid( int cityid) { this .cityid = cityid; } public string getcityname() { return cityname; } public void setcityname(string cityname) { this .cityname = cityname; } public string getcityintroduce() { return cityintroduce; } public void setcityintroduce(string cityintroduce) { this .cityintroduce = cityintroduce; } } |
cityrepository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.dalaoyang.repository; import com.dalaoyang.entity.city; import org.springframework.data.jpa.repository.jparepository; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.repository * @email [email protected] * @date 2018/4/7 */ public interface cityrepository extends jparepository<city,integer> { } |
citycontroller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.dalaoyang.controller; import com.dalaoyang.entity.city; import com.dalaoyang.repository.cityrepository; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; /** * @author dalaoyang * @description * @project springboot_learn * @package com.dalaoyang.controller * @email [email protected] * @date 2018/4/7 */ @restcontroller public class citycontroller { @autowired private cityrepository cityrepository; //http://localhost:8888/savecity?cityname=北京&cityintroduce=中國(guó)首都 @getmapping (value = "savecity" ) public string savecity(string cityname,string cityintroduce){ city city = new city(cityname,cityintroduce); cityrepository.save(city); return "success" ; } //http://localhost:8888/deletecity?cityid=2 @getmapping (value = "deletecity" ) public string deletecity( int cityid){ cityrepository.delete(cityid); return "success" ; } //http://localhost:8888/updatecity?cityid=3&cityname=沈陽(yáng)&cityintroduce=遼寧省省會(huì) @getmapping (value = "updatecity" ) public string updatecity( int cityid,string cityname,string cityintroduce){ city city = new city(cityid,cityname,cityintroduce); cityrepository.save(city); return "success" ; } //http://localhost:8888/getcitybyid?cityid=3 @getmapping (value = "getcitybyid" ) public city getcitybyid( int cityid){ city city = cityrepository.findone(cityid); return city; } } |
然后啟動(dòng)項(xiàng)目,可以看到控制臺(tái)已經(jīng)創(chuàng)建了city表。
然后訪問(wèn)http://localhost:8888/druid,可以看到如下圖:
輸入賬號(hào)密碼admin,admin,如下圖
然后這時(shí)我們可以訪問(wèn)http://localhost:8888/savecity?cityname=北京&cityintroduce=中國(guó)首都
然后點(diǎn)擊導(dǎo)航上面的sql監(jiān)控,如下圖,
從上圖可以看到啟動(dòng)項(xiàng)目創(chuàng)建表的sql已經(jīng)剛剛執(zhí)行的sql。到這里整合已經(jīng)完成了。
源碼下載 :https://gitee.com/dalaoyang/springboot_learn
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://juejin.im/post/5aceec94f265da2395315d68