一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法

Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法

2021-04-27 11:50虛無(wú)境 Java教程

這篇文章主要給大家介紹了關(guān)于Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們來(lái)一起看看吧

前言

本篇文章主要講述的是springboot整合mybatis、druid和pagehelper 并實(shí)現(xiàn)多數(shù)據(jù)源和分頁(yè)。其中springboot整合mybatis這塊,在之前的一篇文章的中已經(jīng)講述了,這里就不過(guò)多說(shuō)明了。重點(diǎn)是講述在多數(shù)據(jù)源下的如何配置使用druid和pagehelper 。

druid介紹和使用

在使用druid之前,先來(lái)簡(jiǎn)單的了解下druid。

druid是一個(gè)數(shù)據(jù)庫(kù)連接池。druid可以說(shuō)是目前最好的數(shù)據(jù)庫(kù)連接池!因其優(yōu)秀的功能、性能和擴(kuò)展性方面,深受開發(fā)人員的青睞。

druid已經(jīng)在阿里巴巴部署了超過(guò)600個(gè)應(yīng)用,經(jīng)過(guò)一年多生產(chǎn)環(huán)境大規(guī)模部署的嚴(yán)苛考驗(yàn)。druid是阿里巴巴開發(fā)的號(hào)稱為監(jiān)控而生的數(shù)據(jù)庫(kù)連接池!

同時(shí)druid不僅僅是一個(gè)數(shù)據(jù)庫(kù)連接池,druid 核心主要包括三部分:

  • 基于filter-chain模式的插件體系。
  • druiddatasource 高效可管理的數(shù)據(jù)庫(kù)連接池。
  • sqlparser

druid的主要功能如下:

  1. 是一個(gè)高效、功能強(qiáng)大、可擴(kuò)展性好的數(shù)據(jù)庫(kù)連接池。
  2. 可以監(jiān)控?cái)?shù)據(jù)庫(kù)訪問(wèn)性能。
  3. 數(shù)據(jù)庫(kù)密碼加密
  4. 獲得sql執(zhí)行日志
  5. 擴(kuò)展jdbc

介紹方面這塊就不再多說(shuō),具體的可以看官方文檔。

那么開始介紹druid如何使用。

首先是maven依賴,只需要添加druid這一個(gè)jar就行了。

?
1
2
3
4
5
<dependency>
 <groupid>com.alibaba</groupid>
 <artifactid>druid</artifactid>
 <version>1.1.8</version>
 </dependency>

配置方面,主要的只需要在application.properties或application.yml添加如下就可以了。

說(shuō)明:因?yàn)檫@里我是用來(lái)兩個(gè)數(shù)據(jù)源,所以稍微有些不同而已。druid 配置的說(shuō)明在下面中已經(jīng)說(shuō)的很詳細(xì)了,這里我就不在說(shuō)明了。

?
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
## 默認(rèn)的數(shù)據(jù)源
 
master.datasource.url=jdbc:mysql://localhost:3306/springboot?useunicode=true&characterencoding=utf8&allowmultiqueries=true
master.datasource.username=root
master.datasource.password=123456
master.datasource.driverclassname=com.mysql.jdbc.driver
 
 
## 另一個(gè)的數(shù)據(jù)源
cluster.datasource.url=jdbc:mysql://localhost:3306/springboot_test?useunicode=true&characterencoding=utf8
cluster.datasource.username=root
cluster.datasource.password=123456
cluster.datasource.driverclassname=com.mysql.jdbc.driver
 
# 連接池的配置信息
# 初始化大小,最小,最大
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
# 打開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)打開mergesql功能;慢sql記錄
spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000

成功添加了配置文件之后,我們?cè)賮?lái)編寫druid相關(guān)的類。

首先是masterdatasourceconfig.java這個(gè)類,這個(gè)是默認(rèn)的數(shù)據(jù)源配置類。

?
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
106
107
108
109
110
111
112
113
@configuration
@mapperscan(basepackages = masterdatasourceconfig.package, sqlsessionfactoryref = "mastersqlsessionfactory")
public class masterdatasourceconfig {
 
 static final string package = "com.pancm.dao.master";
 static final string mapper_location = "classpath:mapper/master/*.xml";
 
 @value("${master.datasource.url}")
 private string url;
 
 @value("${master.datasource.username}")
 private string username;
 
 @value("${master.datasource.password}")
 private string password;
 
 @value("${master.datasource.driverclassname}")
 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(name = "masterdatasource")
 @primary
 public datasource masterdatasource() {
 druiddatasource datasource = new druiddatasource();
 datasource.seturl(url);
 datasource.setusername(username);
 datasource.setpassword(password);
 datasource.setdriverclassname(driverclassname);
 
 //具體配置
 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) {
 e.printstacktrace();
 }
 datasource.setconnectionproperties(connectionproperties);
 return datasource;
 }
 
 @bean(name = "mastertransactionmanager")
 @primary
 public datasourcetransactionmanager mastertransactionmanager() {
 return new datasourcetransactionmanager(masterdatasource());
 }
 
 @bean(name = "mastersqlsessionfactory")
 @primary
 public sqlsessionfactory mastersqlsessionfactory(@qualifier("masterdatasource") datasource masterdatasource)
 throws exception {
 final sqlsessionfactorybean sessionfactory = new sqlsessionfactorybean();
 sessionfactory.setdatasource(masterdatasource);
 sessionfactory.setmapperlocations(new pathmatchingresourcepatternresolver()
 .getresources(masterdatasourceconfig.mapper_location));
 return sessionfactory.getobject();
 }
}

其中這兩個(gè)注解說(shuō)明下:

**@primary** :標(biāo)志這個(gè) bean 如果在多個(gè)同類 bean 候選時(shí),該 bean
優(yōu)先被考慮。多數(shù)據(jù)源配置的時(shí)候注意,必須要有一個(gè)主數(shù)據(jù)源,用 @primary 標(biāo)志該 bean。

**@mapperscan**: 掃描 mapper 接口并容器管理。

需要注意的是sqlsessionfactoryref 表示定義一個(gè)唯一 sqlsessionfactory 實(shí)例。

上面的配置完之后,就可以將druid作為連接池使用了。但是druid并不簡(jiǎn)簡(jiǎn)單單的是個(gè)連接池,它也可以說(shuō)是一個(gè)監(jiān)控應(yīng)用,它自帶了web監(jiān)控界面,可以很清晰的看到sql相關(guān)信息。

在springboot中運(yùn)用druid的監(jiān)控作用,只需要編寫statviewservlet和webstatfilter類,實(shí)現(xiàn)注冊(cè)服務(wù)和過(guò)濾規(guī)則。這里我們可以將這兩個(gè)寫在一起,使用**@configuration**和**@bean**。

為了方便理解,相關(guān)的配置說(shuō)明也寫在代碼中了,這里就不再過(guò)多贅述了。

代碼如下:

?
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
@configuration
public class druidconfiguration {
 
 @bean
 public servletregistrationbean druidstatviewservle() {
 //注冊(cè)服務(wù)
 servletregistrationbean servletregistrationbean = new servletregistrationbean(
 new statviewservlet(), "/druid/*");
 // 白名單(為空表示,所有的都可以訪問(wèn),多個(gè)ip的時(shí)候用逗號(hào)隔開)
 servletregistrationbean.addinitparameter("allow", "127.0.0.1");
 // ip黑名單 (存在共同時(shí),deny優(yōu)先于allow)
 servletregistrationbean.addinitparameter("deny", "127.0.0.2");
 // 設(shè)置登錄的用戶名和密碼
 servletregistrationbean.addinitparameter("loginusername", "pancm");
 servletregistrationbean.addinitparameter("loginpassword", "123456");
 // 是否能夠重置數(shù)據(jù).
 servletregistrationbean.addinitparameter("resetenable", "false");
 return servletregistrationbean;
 }
 
 @bean
 public filterregistrationbean druidstatfilter() {
 filterregistrationbean filterregistrationbean = new filterregistrationbean(
 new webstatfilter());
 // 添加過(guò)濾規(guī)則
 filterregistrationbean.addurlpatterns("/*");
 // 添加不需要忽略的格式信息
 filterregistrationbean.addinitparameter("exclusions",
 "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
 system.out.println("druid初始化成功!");
 return filterregistrationbean;
 }
}

編寫完之后,啟動(dòng)程序,在瀏覽器輸入:http://127.0.0.1:8084/druid/index.html ,然后輸入設(shè)置的用戶名和密碼,便可以訪問(wèn)web界面了。

多數(shù)據(jù)源配置

在進(jìn)行多數(shù)據(jù)源配置之前,先分別在springboot和springboot_test的mysql數(shù)據(jù)庫(kù)中執(zhí)行如下腳本。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- springboot庫(kù)的腳本
 
create table `t_user` (
 `id` int(11) not null auto_increment comment '自增id',
 `name` varchar(10) default null comment '姓名',
 `age` int(2) default null comment '年齡',
 primary key (`id`)
) engine=innodb auto_increment=15 default charset=utf8
 
-- springboot_test庫(kù)的腳本
 
create table `t_student` (
 `id` int(11) not null auto_increment,
 `name` varchar(16) default null,
 `age` int(11) default null,
 primary key (`id`)
) engine=innodb auto_increment=2 default charset=utf8

注:為了偷懶,將兩張表的結(jié)構(gòu)弄成一樣了!不過(guò)不影響測(cè)試!

在application.properties中已經(jīng)配置這兩個(gè)數(shù)據(jù)源的信息,上面已經(jīng)貼出了一次配置,這里就不再貼了。

這里重點(diǎn)說(shuō)下 第二個(gè)數(shù)據(jù)源的配置。和上面的masterdatasourceconfig.java差不多,區(qū)別在與沒(méi)有使用**@primary** 注解和名稱不同而已。需要注意的是masterdatasourceconfig.java對(duì)package和mapper的掃描是精確到目錄的,這里的第二個(gè)數(shù)據(jù)源也是如此。那么代碼如下:

?
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
@configuration
@mapperscan(basepackages = clusterdatasourceconfig.package, sqlsessionfactoryref = "clustersqlsessionfactory")
public class clusterdatasourceconfig {
 
 static final string package = "com.pancm.dao.cluster";
 static final string mapper_location = "classpath:mapper/cluster/*.xml";
 
 @value("${cluster.datasource.url}")
 private string url;
 
 @value("${cluster.datasource.username}")
 private string username;
 
 @value("${cluster.datasource.password}")
 private string password;
 
 @value("${cluster.datasource.driverclassname}")
 private string driverclass;
 
 // 和masterdatasourceconfig一樣,這里略
 
 @bean(name = "clusterdatasource")
 public datasource clusterdatasource() {
 druiddatasource datasource = new druiddatasource();
 datasource.seturl(url);
 datasource.setusername(username);
 datasource.setpassword(password);
 datasource.setdriverclassname(driverclass);
 
 // 和masterdatasourceconfig一樣,這里略 ...
 return datasource;
 }
 
 @bean(name = "clustertransactionmanager")
 public datasourcetransactionmanager clustertransactionmanager() {
 return new datasourcetransactionmanager(clusterdatasource());
 }
 
 @bean(name = "clustersqlsessionfactory")
 public sqlsessionfactory clustersqlsessionfactory(@qualifier("clusterdatasource") datasource clusterdatasource)
 throws exception {
 final sqlsessionfactorybean sessionfactory = new sqlsessionfactorybean();
 sessionfactory.setdatasource(clusterdatasource);
 sessionfactory.setmapperlocations(new pathmatchingresourcepatternresolver().getresources(clusterdatasourceconfig.mapper_location));
 return sessionfactory.getobject();
 }
}

成功寫完配置之后,啟動(dòng)程序,進(jìn)行測(cè)試。

分別在springboot和springboot_test庫(kù)中使用接口進(jìn)行添加數(shù)據(jù)。

t_user

?
1
2
3
4
post http://localhost:8084/api/user
{"name":"張三","age":25}
{"name":"李四","age":25}
{"name":"王五","age":25}

t_student

?
1
2
3
4
post http://localhost:8084/api/student
{"name":"學(xué)生a","age":16}
{"name":"學(xué)生b","age":17}
{"name":"學(xué)生c","age":18}

成功添加數(shù)據(jù)之后,然后進(jìn)行調(diào)用不同的接口進(jìn)行查詢。

請(qǐng)求:

?
1
get http://localhost:8084/api/user?name=李四

返回:

?
1
2
3
4
5
{
 "id": 2,
 "name": "李四",
 "age": 25
}

請(qǐng)求:

?
1
get http://localhost:8084/api/student?name=學(xué)生c

返回:

?
1
2
3
4
5
{
 "id": 1,
 "name": "學(xué)生c",
 "age": 16
}

通過(guò)數(shù)據(jù)可以看出,成功配置了多數(shù)據(jù)源了。

pagehelper 分頁(yè)實(shí)現(xiàn)

pagehelper是mybatis的一個(gè)分頁(yè)插件,非常的好用!這里強(qiáng)烈推薦!?。?/p>

pagehelper的使用很簡(jiǎn)單,只需要在maven中添加pagehelper這個(gè)依賴就可以了。
maven的依賴如下:

?
1
2
3
4
5
<dependency>
 <groupid>com.github.pagehelper</groupid>
 <artifactid>pagehelper-spring-boot-starter</artifactid>
 <version>1.2.3</version>
</dependency>

注:這里我是用springboot版的!也可以使用其它版本的。

添加依賴之后,只需要添加如下配置或代碼就可以了。

第一種,在application.propertiesapplication.yml添加

?
1
2
3
4
5
pagehelper:
helperdialect: mysql
offsetaspagenum: true
rowboundswithcount: true
reasonable: false

第二種,在mybatis.xml配置中添加

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<property name="datasource" ref="datasource" />
<!-- 掃描mapping.xml文件 -->
<property name="mapperlocations" value="classpath:mapper/*.xml"></property>
<!-- 配置分頁(yè)插件 -->
<property name="plugins">
<array>
 <bean class="com.github.pagehelper.pagehelper">
 <property name="properties">
 <value>
 helperdialect=mysql
 offsetaspagenum=true
 rowboundswithcount=true
 reasonable=false
 </value>
 </property>
 </bean>
</array>
</property>
</bean>

第三種,在代碼中添加,使用**@bean**注解在啟動(dòng)程序的時(shí)候初始化。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@bean
public pagehelper pagehelper(){
pagehelper pagehelper = new pagehelper();
properties properties = new properties();
//數(shù)據(jù)庫(kù)
properties.setproperty("helperdialect", "mysql");
//是否將參數(shù)offset作為pagenum使用
properties.setproperty("offsetaspagenum", "true");
//是否進(jìn)行count查詢
properties.setproperty("rowboundswithcount", "true");
//是否分頁(yè)合理化
properties.setproperty("reasonable", "false");
pagehelper.setproperties(properties);
}

因?yàn)檫@里我們使用的是多數(shù)據(jù)源,所以這里的配置稍微有些不同。我們需要在sessionfactory這里配置。這里就對(duì)masterdatasourceconfig.java進(jìn)行相應(yīng)的修改。在mastersqlsessionfactory方法中,添加如下代碼。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@bean(name = "mastersqlsessionfactory")
@primary
public sqlsessionfactory mastersqlsessionfactory(@qualifier("masterdatasource") datasource masterdatasource)
 throws exception {
final sqlsessionfactorybean sessionfactory = new sqlsessionfactorybean();
sessionfactory.setdatasource(masterdatasource);
sessionfactory.setmapperlocations(new pathmatchingresourcepatternresolver()
 .getresources(masterdatasourceconfig.mapper_location));
//分頁(yè)插件
interceptor interceptor = new pageinterceptor();
properties properties = new properties();
//數(shù)據(jù)庫(kù)
properties.setproperty("helperdialect", "mysql");
//是否將參數(shù)offset作為pagenum使用
properties.setproperty("offsetaspagenum", "true");
//是否進(jìn)行count查詢
properties.setproperty("rowboundswithcount", "true");
//是否分頁(yè)合理化
properties.setproperty("reasonable", "false");
interceptor.setproperties(properties);
sessionfactory.setplugins(new interceptor[] {interceptor});
 
return sessionfactory.getobject();
}

注:其它的數(shù)據(jù)源也想進(jìn)行分頁(yè)的時(shí)候,參照上面的代碼即可。

這里需要注意的是reasonable參數(shù),表示分頁(yè)合理化,默認(rèn)值為false。如果該參數(shù)設(shè)置為 true 時(shí),pagenum<=0 時(shí)會(huì)查詢第一頁(yè),pagenum>pages(超過(guò)總數(shù)時(shí)),會(huì)查詢最后一頁(yè)。默認(rèn)false 時(shí),直接根據(jù)參數(shù)進(jìn)行查詢。

設(shè)置完pagehelper 之后,使用的話,只需要在查詢的sql前面添加pagehelper.startpage(pagenum,pagesize); ,如果是想知道總數(shù)的話,在查詢的sql語(yǔ)句后買呢添加 page.gettotal()就可以了。

代碼示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
public list<t> findbylistentity(t entity) {
 list<t> list = null;
 try {
  page<?> page =pagehelper.startpage(1,2);
  system.out.println(getclassname(entity)+"設(shè)置第一頁(yè)兩條數(shù)據(jù)!");
  list = getmapper().findbylistentity(entity);
  system.out.println("總共有:"+page.gettotal()+"條數(shù)據(jù),實(shí)際返回:"+list.size()+"兩條數(shù)據(jù)!");
 } catch (exception e) {
  logger.error("查詢"+getclassname(entity)+"失敗!原因是:",e);
 }
 return list;
 }

代碼編寫完畢之后,開始進(jìn)行最后的測(cè)試。

查詢t_user表的所有的數(shù)據(jù),并進(jìn)行分頁(yè)。

請(qǐng)求:

?
1
get http://localhost:8084/api/user

返回:

?
1
2
3
4
5
6
7
8
9
10
11
12
[
 {
 "id": 1,
 "name": "張三",
 "age": 25
 },
 {
 "id": 2,
 "name": "李四",
 "age": 25
 }
]

控制臺(tái)打印:

開始查詢...
user設(shè)置第一頁(yè)兩條數(shù)據(jù)!
2018-04-27 19:55:50.769 debug 6152 --- [io-8084-exec-10] c.p.d.m.userdao.findbylistentity_count   : ==>  preparing: select count(0) from t_user where 1 = 1
2018-04-27 19:55:50.770 debug 6152 --- [io-8084-exec-10] c.p.d.m.userdao.findbylistentity_count   : ==> parameters:
2018-04-27 19:55:50.771 debug 6152 --- [io-8084-exec-10] c.p.d.m.userdao.findbylistentity_count   : <==      total: 1
2018-04-27 19:55:50.772 debug 6152 --- [io-8084-exec-10] c.p.dao.master.userdao.findbylistentity  : ==>  preparing: select id, name, age from t_user where 1=1 limit ?
2018-04-27 19:55:50.773 debug 6152 --- [io-8084-exec-10] c.p.dao.master.userdao.findbylistentity  : ==> parameters: 2(integer)
2018-04-27 19:55:50.774 debug 6152 --- [io-8084-exec-10] c.p.dao.master.userdao.findbylistentity  : <==      total: 2
總共有:3條數(shù)據(jù),實(shí)際返回:2兩條數(shù)據(jù)!

查詢t_student表的所有的數(shù)據(jù),并進(jìn)行分頁(yè)。

請(qǐng)求:

?
1
get http://localhost:8084/api/student

返回:

?
1
2
3
4
5
6
7
8
9
10
11
12
[
 {
 "id": 1,
 "name": "學(xué)生a",
 "age": 16
 },
 {
 "id": 2,
 "name": "學(xué)生b",
 "age": 17
 }
]

控制臺(tái)打印:

開始查詢...
studnet設(shè)置第一頁(yè)兩條數(shù)據(jù)!
2018-04-27 19:54:56.155 debug 6152 --- [nio-8084-exec-8] c.p.d.c.s.findbylistentity_count         : ==>  preparing: select count(0) from t_student where 1 = 1
2018-04-27 19:54:56.155 debug 6152 --- [nio-8084-exec-8] c.p.d.c.s.findbylistentity_count         : ==> parameters:
2018-04-27 19:54:56.156 debug 6152 --- [nio-8084-exec-8] c.p.d.c.s.findbylistentity_count         : <==      total: 1
2018-04-27 19:54:56.157 debug 6152 --- [nio-8084-exec-8] c.p.d.c.studentdao.findbylistentity      : ==>  preparing: select id, name, age from t_student where 1=1 limit ?
2018-04-27 19:54:56.157 debug 6152 --- [nio-8084-exec-8] c.p.d.c.studentdao.findbylistentity      : ==> parameters: 2(integer)
2018-04-27 19:54:56.157 debug 6152 --- [nio-8084-exec-8] c.p.d.c.studentdao.findbylistentity      : <==      total: 2
總共有:3條數(shù)據(jù),實(shí)際返回:2兩條數(shù)據(jù)!

查詢完畢之后,我們?cè)賮?lái)看druid 的監(jiān)控界面。在瀏覽器輸入:http://127.0.0.1:8084/druid/index.html

Spring Boot+Mybatis+Druid+PageHelper實(shí)現(xiàn)多數(shù)據(jù)源并分頁(yè)的方法

可以很清晰的看到操作記錄!

如果想知道更多的druid相關(guān)知識(shí),可以查看官方文檔!

結(jié)語(yǔ)

這篇終于寫完了,在進(jìn)行代碼編寫的時(shí)候,碰到過(guò)很多問(wèn)題,然后慢慢的嘗試和找資料解決了。本篇文章只是很淺的介紹了這些相關(guān)的使用,在實(shí)際的應(yīng)用可能會(huì)更復(fù)雜。

參考文章:https://www.bysocket.com/?p=1712

durid官方地址:https://github.com/alibaba/druid

pagehelper官方地址:https://github.com/pagehelper/mybatis-pagehelper

項(xiàng)目我放到github上面去了: https://github.com/xuwujing/springboot

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。

原文鏈接:https://www.cnblogs.com/xuwujing/p/8964927.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品99国产精品日本 | 日本一卡二卡3卡四卡网站精品 | 村上里沙40分钟在线观看 | 日本黄色大片网站 | 紧缚束缚调教丨vk | 视频在线观看高清免费 | 欧美破苞合集 magnet | 国产一区二区三区四区波多野结衣 | 日本高清免费中文字幕不卡 | 国产亚洲福利一区二区免费看 | 日本国产最新一区二区三区 | 国内久久婷婷综合欲色啪 | 欧美日韩中文字幕久久伊人 | 日韩国产成人精品视频 | 欧美伊人久久久久久久久影院 | 不良网站在线观看 | xxx中国bbbwww| 99久久香蕉国产综合影院 | 午夜影院小视频 | 午夜影院免费观看视频 | 国产精品免费_区二区三区观看 | 五月天婷婷网亚洲综合在线 | gayrb免费漫画入口 | 日本一区二区精品88 | 好看华人华人经典play | 国产高清不卡码一区二区三区 | 精品破处 | 国产精品怡红院永久免费 | 99久久精品免费观看区一 | 午夜伦午夜伦锂电影 | 成人精品一区二区三区中文字幕 | 美女的隐私无遮挡的网页 | 欧美日韩视频一区三区二区 | 亚州精品视频 | 楚乔传第二部免费播放电视连续剧 | 男女交性特一级 | 色综久久天天综合绕视看 | 国产欧美日韩精品在线 | 欧美精品一区二区在线观看播放 | 国产精品免费视频一区一 | 啊好大好粗 |