本篇文章主要講述的是SpringBoot整合Mybatis筒捺、Druid和PageHelper 并實(shí)現(xiàn)多數(shù)據(jù)源和分頁柏腻。其中SpringBoot整合Mybatis這塊,在之前的的一篇文章中已經(jīng)講述了系吭,這里就不過多說明了五嫂。重點(diǎn)是講述在多數(shù)據(jù)源下的如何配置使用Druid和PageHelper 。
http://www.cnblogs.com/xuwujing/p/8260935.html
Druid介紹和使用
在使用Druid之前肯尺,先來簡(jiǎn)單的了解下Druid沃缘。
Druid是一個(gè)數(shù)據(jù)庫連接池。Druid可以說是目前最好的數(shù)據(jù)庫連接池则吟!因其優(yōu)秀的功能槐臀、性能和擴(kuò)展性方面,深受開發(fā)人員的青睞氓仲。
Druid已經(jīng)在阿里巴巴部署了超過600個(gè)應(yīng)用水慨,經(jīng)過一年多生產(chǎn)環(huán)境大規(guī)模部署的嚴(yán)苛考驗(yàn)。Druid是阿里巴巴開發(fā)的號(hào)稱為監(jiān)控而生的數(shù)據(jù)庫連接池敬扛!
同時(shí)Druid不僅僅是一個(gè)數(shù)據(jù)庫連接池讥巡,Druid 核心主要包括三部分:
基于Filter-Chain模式的插件體系。
DruidDataSource 高效可管理的數(shù)據(jù)庫連接池舔哪。
SQLParser
Druid的主要功能如下:
是一個(gè)高效欢顷、功能強(qiáng)大、可擴(kuò)展性好的數(shù)據(jù)庫連接池捉蚤。
可以監(jiān)控?cái)?shù)據(jù)庫訪問性能抬驴。
數(shù)據(jù)庫密碼加密
獲得SQL執(zhí)行日志
擴(kuò)展JDBC
介紹方面這塊就不再多說,具體的可以看官方文檔缆巧。那么開始介紹Druid如何使用布持。
首先是Maven依賴,只需要添加druid這一個(gè)jar就行了陕悬。
<dependency>?????????
<groupId>com.alibaba</groupId>?????????
<artifactId>druid</artifactId>?????????
<version>1.1.8</version>
??</dependency>
配置方面题暖,主要的只需要在application.properties或application.yml添加如下就可以了。
說明:因?yàn)檫@里我是用來兩個(gè)數(shù)據(jù)源,所以稍微有些不同而已胧卤。Druid 配置的說明在下面中已經(jīng)說的很詳細(xì)了唯绍,這里我就不在說明了。
##?默認(rèn)的數(shù)據(jù)源
##?默認(rèn)的數(shù)據(jù)源master.datasource.url=jdbc:mysql://localhost:3306/springBoot?useUnicode=true&characterEncoding=utf8&allowMultiQueries=truemaster.datasource.username=rootmaster.datasource.password=123456master.datasource.driverClassName=com.mysql.jdbc.Driver##?另一個(gè)的數(shù)據(jù)源cluster.datasource.url=jdbc:mysql://localhost:3306/springBoot_test?useUnicode=true&characterEncoding=utf8cluster.datasource.username=rootcluster.datasource.password=123456cluster.datasource.driverClassName=com.mysql.jdbc.Driver#?連接池的配置信息??#?初始化大小枝誊,最小况芒,最大??spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.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無法統(tǒng)計(jì)压汪,'wall'用于防火墻??spring.datasource.filters=stat,wall,log4j??#?通過connectProperties屬性來打開mergeSql功能;慢SQL記錄??spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000??
成功添加了配置文件之后古瓤,我們?cè)賮砭帉慏ruid相關(guān)的類止剖。
首先是MasterDataSourceConfig.java這個(gè)類,這個(gè)是默認(rèn)的數(shù)據(jù)源配置類湿滓。
@Configuration
@MapperScan(basePackages?=?MasterDataSourceConfig.PACKAGE,?sqlSessionFactoryRef?="masterSqlSessionFactory")
publicclassMasterDataSourceConfig{
staticfinalString?PACKAGE?="com.pancm.dao.master";
staticfinalString?MAPPER_LOCATION?="classpath:mapper/master/*.xml";
@Value("${master.datasource.url}")
privateString?url;
@Value("${master.datasource.username}")
privateString?username;
@Value("${master.datasource.password}")
privateString?password;
@Value("${master.datasource.driverClassName}")
privateString?driverClassName;
@Value("${spring.datasource.initialSize}")
privateint?initialSize;
@Value("${spring.datasource.minIdle}")
privateint?minIdle;
@Value("${spring.datasource.maxActive}")
privateint?maxActive;
@Value("${spring.datasource.maxWait}")
privateint?maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
privateint?timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
privateint?minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
privateString?validationQuery;
@Value("${spring.datasource.testWhileIdle}")
privateboolean?testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
privateboolean?testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
privateboolean?testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
privateboolean?poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
privateint?maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
privateString?filters;
@Value("{spring.datasource.connectionProperties}")
privateString?connectionProperties;
@Bean(name?="masterDataSource")
@Primary
publicDataSource?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);
returndataSource;
}
@Bean(name?="masterTransactionManager")
@Primary
publicDataSourceTransactionManager?masterTransactionManager()?{
returnnew?DataSourceTransactionManager(masterDataSource());
}
@Bean(name?="masterSqlSessionFactory")
@Primary
publicSqlSessionFactory?masterSqlSessionFactory(@Qualifier("masterDataSource")DataSource?masterDataSource)
throws?Exception?{
finalSqlSessionFactoryBean?sessionFactory?=?new?SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new?PathMatchingResourcePatternResolver()
.getResources(MasterDataSourceConfig.MAPPER_LOCATION));
returnsessionFactory.getObject();
}
}
其中這兩個(gè)注解說明下:
@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è)連接池,它也可以說是一個(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ī)則。這里我們可以將這兩個(gè)寫在一起印蔗,使用@Configuration和@Bean扒最。
為了方便理解,相關(guān)的配置說明也寫在代碼中了华嘹,這里就不再過多贅述了吧趣。
代碼如下:
@Configuration
publicclassDruidConfiguration{
@Bean
publicServletRegistrationBeandruidStatViewServle(){
//注冊(cè)服務(wù)
ServletRegistrationBean?servletRegistrationBean?=newServletRegistrationBean(
newStatViewServlet(),"/druid/*");
//?白名單(為空表示,所有的都可以訪問,多個(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");
returnservletRegistrationBean;
}
@Bean
publicFilterRegistrationBeandruidStatFilter(){
FilterRegistrationBean?filterRegistrationBean?=newFilterRegistrationBean(
newWebStatFilter());
//?添加過濾規(guī)則
filterRegistrationBean.addUrlPatterns("/*");
//?添加不需要忽略的格式信息
filterRegistrationBean.addInitParameter("exclusions",
"*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
System.out.println("druid初始化成功!");
returnfilterRegistrationBean;
}
}
編寫完之后,啟動(dòng)程序强挫,在瀏覽器輸入:http://127.0.0.1:8084/druid/index.html 岔霸,然后輸入設(shè)置的用戶名和密碼,便可以訪問Web界面了纠拔。
多數(shù)據(jù)源配置
在進(jìn)行多數(shù)據(jù)源配置之前秉剑,先分別在springBoot和springBoot_test的mysql數(shù)據(jù)庫中執(zhí)行如下腳本。
--?springBoot庫的腳本
CREATETABLE`t_user`(
`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'自增id',
`name`varchar(10)DEFAULTNULLCOMMENT'姓名',
`age`int(2)DEFAULTNULLCOMMENT'年齡',
PRIMARYKEY(`id`)
)ENGINE=InnoDBAUTO_INCREMENT=15DEFAULTCHARSET=utf8
--?springBoot_test庫的腳本
CREATETABLE`t_student`(
`id`int(11)NOTNULLAUTO_INCREMENT,
`name`varchar(16)DEFAULTNULL,
`age`int(11)DEFAULTNULL,
PRIMARYKEY(`id`)
)ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=utf8
注:為了偷懶稠诲,將兩張表的結(jié)構(gòu)弄成一樣了侦鹏!不過不影響測(cè)試!
在application.properties中已經(jīng)配置這兩個(gè)數(shù)據(jù)源的信息,上面已經(jīng)貼出了一次配置臀叙,這里就不再貼了略水。
這里重點(diǎn)說下?第二個(gè)數(shù)據(jù)源的配置。和上面的MasterDataSourceConfig.java差不多劝萤,區(qū)別在與沒有使用@Primary 注解和名稱不同而已渊涝。需要注意的是MasterDataSourceConfig.java對(duì)package和mapper的掃描是精確到目錄的,這里的第二個(gè)數(shù)據(jù)源也是如此床嫌。
那么代碼如下:
@Configuration
@MapperScan(basePackages?=?ClusterDataSourceConfig.PACKAGE,?sqlSessionFactoryRef?="clusterSqlSessionFactory")
publicclassClusterDataSourceConfig{
staticfinalString?PACKAGE?="com.pancm.dao.cluster";
staticfinalString?MAPPER_LOCATION?="classpath:mapper/cluster/*.xml";
@Value("${cluster.datasource.url}")
privateString?url;
@Value("${cluster.datasource.username}")
privateString?username;
@Value("${cluster.datasource.password}")
privateString?password;
@Value("${cluster.datasource.driverClassName}")
privateString?driverClass;
//?和MasterDataSourceConfig一樣跨释,這里略
@Bean(name?="clusterDataSource")
publicDataSource?clusterDataSource()?{
DruidDataSource?dataSource?=?new?DruidDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
//?和MasterDataSourceConfig一樣,這里略?...
returndataSource;
}
@Bean(name?="clusterTransactionManager")
publicDataSourceTransactionManager?clusterTransactionManager()?{
returnnew?DataSourceTransactionManager(clusterDataSource());
}
@Bean(name?="clusterSqlSessionFactory")
publicSqlSessionFactory?clusterSqlSessionFactory(@Qualifier("clusterDataSource")DataSource?clusterDataSource)
throws?Exception?{
finalSqlSessionFactoryBean?sessionFactory?=?new?SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource);
sessionFactory.setMapperLocations(new?PathMatchingResourcePatternResolver().getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
returnsessionFactory.getObject();
}
}
成功寫完配置之后厌处,啟動(dòng)程序鳖谈,進(jìn)行測(cè)試。
分別在springBoot和springBoot_test庫中使用接口進(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)行查詢。
請(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
}
通過數(shù)據(jù)可以看出瑰排,成功配置了多數(shù)據(jù)源了贯要。
PageHelper 分頁實(shí)現(xiàn)
PageHelper是Mybatis的一個(gè)分頁插件,非常的好用椭住!這里強(qiáng)烈推薦3缟!京郑!
PageHelper的使用很簡(jiǎn)單宅广,只需要在Maven中添加pagehelper這個(gè)依賴就可以了。
Maven的依賴如下:
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.3
注:這里我是用springBoot版的傻挂!也可以使用其它版本的乘碑。
添加依賴之后,只需要添加如下配置或代碼就可以了金拒。
第一種兽肤,在application.properties或application.yml添加
pagehelper:
helperDialect:?mysql
offsetAsPageNum:true
rowBoundsWithCount:true
reasonable:false
第二種套腹,在mybatis.xml配置中添加
<!--?掃描mapping.xml文件?-->
<!--?配置分頁插件?-->
helperDialect=mysql
offsetAsPageNum=true
rowBoundsWithCount=true
reasonable=false
第三種,在代碼中添加资铡,使用@Bean注解在啟動(dòng)程序的時(shí)候初始化电禀。
@Bean
publicPageHelperpageHelper(){
PageHelper?pageHelper?=newPageHelper();
Properties?properties?=newProperties();
//數(shù)據(jù)庫
properties.setProperty("helperDialect","mysql");
//是否將參數(shù)offset作為PageNum使用
properties.setProperty("offsetAsPageNum","true");
//是否進(jìn)行count查詢
properties.setProperty("rowBoundsWithCount","true");
//是否分頁合理化
properties.setProperty("reasonable","false");
pageHelper.setProperties(properties);
}
因?yàn)檫@里我們使用的是多數(shù)據(jù)源,所以這里的配置稍微有些不同笤休。我們需要在sessionFactory這里配置尖飞。這里就對(duì)MasterDataSourceConfig.java進(jìn)行相應(yīng)的修改。
在masterSqlSessionFactory方法中店雅,添加如下代碼政基。
@Bean(name?="masterSqlSessionFactory")
@Primary
publicSqlSessionFactorymasterSqlSessionFactory(@Qualifier("masterDataSource")DataSource?masterDataSource)
throwsException
{
finalSqlSessionFactoryBean?sessionFactory?=newSqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(newPathMatchingResourcePatternResolver()
.getResources(MasterDataSourceConfig.MAPPER_LOCATION));
//分頁插件
Interceptor?interceptor?=newPageInterceptor();
Properties?properties?=newProperties();
//數(shù)據(jù)庫
properties.setProperty("helperDialect","mysql");
//是否將參數(shù)offset作為PageNum使用
properties.setProperty("offsetAsPageNum","true");
//是否進(jìn)行count查詢
properties.setProperty("rowBoundsWithCount","true");
//是否分頁合理化
properties.setProperty("reasonable","false");
interceptor.setProperties(properties);
sessionFactory.setPlugins(newInterceptor[]?{interceptor});
returnsessionFactory.getObject();
}
注:其它的數(shù)據(jù)源也想進(jìn)行分頁的時(shí)候,參照上面的代碼即可闹啦。
這里需要注意的是reasonable參數(shù)沮明,表示分頁合理化,默認(rèn)值為false窍奋。如果該參數(shù)設(shè)置為 true 時(shí)荐健,pageNum<=0?時(shí)會(huì)查詢第一頁,pageNum>pages(超過總數(shù)時(shí))琳袄,會(huì)查詢最后一頁江场。默認(rèn)false 時(shí),直接根據(jù)參數(shù)進(jìn)行查詢窖逗。
設(shè)置完P(guān)ageHelper 之后址否,使用的話,只需要在查詢的sql前面添加PageHelper.startPage(pageNum,pageSize);滑负,如果是想知道總數(shù)的話在张,在查詢的sql語句后買呢添加 page.getTotal()就可以了用含。
代碼示例:
publicList?findByListEntity(T?entity)?{
Listlist=null;
try{
Page<?>?page?=PageHelper.startPage(1,2);
System.out.println(getClassName(entity)+"設(shè)置第一頁兩條數(shù)據(jù)!");
list=?getMapper().findByListEntity(entity);
System.out.println("總共有:"+page.getTotal()+"條數(shù)據(jù),實(shí)際返回:"+list.size()+"兩條數(shù)據(jù)!");
}catch(Exceptione)?{
logger.error("查詢"+getClassName(entity)+"失敗!原因是:",e);
}
returnlist;
}
代碼編寫完畢之后矮慕,開始進(jìn)行最后的測(cè)試。
查詢t_user表的所有的數(shù)據(jù)啄骇,并進(jìn)行分頁痴鳄。
請(qǐng)求:
GET?http://localhost:8084/api/user
返回:
[
{
"id":1,
"name":"張三",
"age":25
},
{
"id":2,
"name":"李四",
"age":25
}
]
控制臺(tái)打印:
開始查詢...
User設(shè)置第一頁兩條數(shù)據(jù)!
2018-04-2719:55:50.769DEBUG6152---?[io-8084-exec-10]?c.p.d.m.UserDao.findByListEntity_COUNT???:?==>??Preparing:?SELECT?count(0)?FROM?t_user?WHERE1=1
2018-04-2719:55:50.770DEBUG6152---?[io-8084-exec-10]?c.p.d.m.UserDao.findByListEntity_COUNT???:?==>?Parameters:
2018-04-2719:55:50.771DEBUG6152---?[io-8084-exec-10]?c.p.d.m.UserDao.findByListEntity_COUNT???:?<==??????Total:1
2018-04-2719:55:50.772DEBUG6152---?[io-8084-exec-10]?c.p.dao.master.UserDao.findByListEntity??:?==>??Preparing:selectid,?name,?age?from?t_user?where1=1LIMIT??
2018-04-2719:55:50.773DEBUG6152---?[io-8084-exec-10]?c.p.dao.master.UserDao.findByListEntity??:?==>?Parameters:2(Integer)
2018-04-2719:55:50.774DEBUG6152---?[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)行分頁缸夹。
請(qǐng)求:
GET?http://localhost:8084/api/student
返回:
[
{
"id":1,
"name":"學(xué)生A",
"age":16
},
{
"id":2,
"name":"學(xué)生B",
"age":17
}
]
控制臺(tái)打印:
開始查詢...
Studnet設(shè)置第一頁兩條數(shù)據(jù)!
2018-04-2719:54:56.155DEBUG6152---?[nio-8084-exec-8]?c.p.d.c.S.findByListEntity_COUNT?????????:?==>??Preparing:?SELECT?count(0)?FROM?t_student?WHERE1=1
2018-04-2719:54:56.155DEBUG6152---?[nio-8084-exec-8]?c.p.d.c.S.findByListEntity_COUNT?????????:?==>?Parameters:
2018-04-2719:54:56.156DEBUG6152---?[nio-8084-exec-8]?c.p.d.c.S.findByListEntity_COUNT?????????:?<==??????Total:1
2018-04-2719:54:56.157DEBUG6152---?[nio-8084-exec-8]?c.p.d.c.StudentDao.findByListEntity??????:?==>??Preparing:selectid,?name,?age?from?t_student?where1=1LIMIT??
2018-04-2719:54:56.157DEBUG6152---?[nio-8084-exec-8]?c.p.d.c.StudentDao.findByListEntity??????:?==>?Parameters:2(Integer)
2018-04-2719:54:56.157DEBUG6152---?[nio-8084-exec-8]?c.p.d.c.StudentDao.findByListEntity??????:?<==??????Total:2
總共有:3條數(shù)據(jù),實(shí)際返回:2兩條數(shù)據(jù)!
查詢完畢之后痪寻,我們?cè)賮砜碊ruid 的監(jiān)控界面。
##?默認(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無法統(tǒng)計(jì)研侣,'wall'用于防火墻??
spring.datasource.filters=stat,wall,log4j??
#?通過connectProperties屬性來打開mergeSql功能谱邪;慢SQL記錄??
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000??