亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2018年05月08日 09:47:21   作者:虛無(wú)境  
這篇文章主要給大家介紹了關(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ò)展性方面,深受開(kāi)發(fā)人員的青睞。

Druid已經(jīng)在阿里巴巴部署了超過(guò)600個(gè)應(yīng)用,經(jīng)過(guò)一年多生產(chǎn)環(huán)境大規(guī)模部署的嚴(yán)苛考驗(yàn)。Druid是阿里巴巴開(kāi)發(fā)的號(hào)稱(chēng)為監(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ō),具體的可以看官方文檔。

那么開(kāi)始介紹Druid如何使用。

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

<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ō)明了。

## 默認(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 
# 打開(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記錄 
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 

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

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

@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è)同類(lèi) 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)控作用,只需要編寫(xiě)StatViewServlet和WebStatFilter類(lèi),實(shí)現(xiàn)注冊(cè)服務(wù)和過(guò)濾規(guī)則。這里我們可以將這兩個(gè)寫(xiě)在一起,使用**@Configuration**和**@Bean**。

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

代碼如下:

@Configuration
public class DruidConfiguration {

 @Bean
 public ServletRegistrationBean druidStatViewServle() {
 //注冊(cè)服務(wù)
 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
 new StatViewServlet(), "/druid/*");
 // 白名單(為空表示,所有的都可以訪問(wèn),多個(gè)IP的時(shí)候用逗號(hào)隔開(kāi))
 servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
 // IP黑名單 (存在共同時(shí),deny優(yōu)先于allow) 
 servletRegistrationBean.addInitParameter("deny", "127.0.0.2");
 // 設(shè)置登錄的用戶(hù)名和密碼
 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;
 }
}

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

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

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

-- 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** 注解和名稱(chēng)不同而已。需要注意的是MasterDataSourceConfig.java對(duì)package和mapper的掃描是精確到目錄的,這里的第二個(gè)數(shù)據(jù)源也是如此。那么代碼如下:

@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();
 }
}

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

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

t_user

POST http://localhost:8084/api/user
{"name":"張三","age":25}
{"name":"李四","age":25}
{"name":"王五","age":25}

t_student

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)行查詢(xún)。

請(qǐng)求:

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

返回:

{
 "id": 2,
 "name": "李四",
 "age": 25
}

請(qǐng)求:

 GET http://localhost:8084/api/student?name=學(xué)生C

返回:

{
 "id": 1,
 "name": "學(xué)生C",
 "age": 16
}

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

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

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

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

 <dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <version>1.2.3</version>
 </dependency>

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

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

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

 pagehelper:
 helperDialect: mysql
 offsetAsPageNum: true
 rowBoundsWithCount: true
 reasonable: false

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

 <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í)候初始化。

 @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查詢(xún)
 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方法中,添加如下代碼。

 @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查詢(xún)
 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ì)查詢(xún)第一頁(yè),pageNum>pages(超過(guò)總數(shù)時(shí)),會(huì)查詢(xún)最后一頁(yè)。默認(rèn)false 時(shí),直接根據(jù)參數(shù)進(jìn)行查詢(xún)。

設(shè)置完P(guān)ageHelper 之后,使用的話(huà),只需要在查詢(xún)的sql前面添加PageHelper.startPage(pageNum,pageSize); ,如果是想知道總數(shù)的話(huà),在查詢(xún)的sql語(yǔ)句后買(mǎi)呢添加 page.getTotal()就可以了。

代碼示例:

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("查詢(xún)"+getClassName(entity)+"失敗!原因是:",e);
 }
 return list;
 }

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

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

請(qǐng)求:

GET http://localhost:8084/api/user

返回:

[
 {
 "id": 1,
 "name": "張三",
 "age": 25
 },
 {
 "id": 2,
 "name": "李四",
 "age": 25
 }
]

控制臺(tái)打印:

開(kāi)始查詢(xún)...
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ù)!

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

請(qǐng)求:

GET http://localhost:8084/api/student

返回:

[
 {
 "id": 1,
 "name": "學(xué)生A",
 "age": 16
 },
 {
 "id": 2,
 "name": "學(xué)生B",
 "age": 17
 }
]

控制臺(tái)打印:

開(kāi)始查詢(xún)...
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ù)!

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


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

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

結(jié)語(yǔ)

這篇終于寫(xiě)完了,在進(jìn)行代碼編寫(xiě)的時(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  ,大家也可以本地下載:點(diǎn)擊這里

總結(jié)

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

相關(guān)文章

  • JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例解析

    JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例解析

    這篇文章主要為大家詳細(xì)介紹了JavaWeb中的文件上傳和下載功能的實(shí)現(xiàn),在Web應(yīng)用系統(tǒng)開(kāi)發(fā)中,文件上傳和下載功能是非常常用的功能,需要的朋友可以參考下
    2015-08-08
  • Java中system.exit(0) 和 system.exit(1)區(qū)別

    Java中system.exit(0) 和 system.exit(1)區(qū)別

    本文主要介紹了Java中system.exit(0) 和 system.exit(1)區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

    Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載

    本文主要介紹了Springboot使用@RefreshScope注解實(shí)現(xiàn)配置文件的動(dòng)態(tài)加載,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 源碼解析springbatch的job運(yùn)行機(jī)制

    源碼解析springbatch的job運(yùn)行機(jī)制

    這篇文章主要介紹了springbatch的job是如何運(yùn)行的,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • Java中IO流解析及代碼實(shí)例詳解

    Java中IO流解析及代碼實(shí)例詳解

    流是一種抽象概念,它代表了數(shù)據(jù)的無(wú)結(jié)構(gòu)化傳遞。。用來(lái)進(jìn)行輸入輸出操作的流就稱(chēng)為IO流。換句話(huà)說(shuō),IO流就是以流的方式進(jìn)行輸入輸出
    2021-08-08
  • 詳解SpringBoot異常處理流程及原理

    詳解SpringBoot異常處理流程及原理

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著SpringBoot異常處理流程及原理展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 解決spring 處理request.getInputStream()輸入流只能讀取一次問(wèn)題

    解決spring 處理request.getInputStream()輸入流只能讀取一次問(wèn)題

    這篇文章主要介紹了解決spring 處理request.getInputStream()輸入流只能讀取一次問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • java后臺(tái)啟動(dòng)jar包的一些命令匯總

    java后臺(tái)啟動(dòng)jar包的一些命令匯總

    這篇文章主要介紹了java后臺(tái)啟動(dòng)jar包的一些命令匯總,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-03-03
  • Java Spring AOP之PointCut案例詳解

    Java Spring AOP之PointCut案例詳解

    這篇文章主要介紹了Java Spring AOP之PointCut案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 分別在Groovy和Java中創(chuàng)建并初始化映射的不同分析

    分別在Groovy和Java中創(chuàng)建并初始化映射的不同分析

    這篇文章主要為大家介紹了分別在Groovy和Java中創(chuàng)建并初始化映射的不同分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

    2022-03-03

最新評(píng)論