?一、springboot 簡介
SpringBoot使開發(fā)獨立的,產(chǎn)品級別的基于Spring的應(yīng)用變得非常簡單,你只需"just run"。 我們?yōu)镾pring平臺及第三方庫提 供開箱即用的設(shè)置,這樣你就可以有條不紊地開始。多數(shù)Spring Boot應(yīng)用需要很少的Spring配置。
你可以使用SpringBoot創(chuàng)建Java應(yīng)用,并使用 java -jar 啟動它或采用傳統(tǒng)的war部署方式。我們也提供了一個運行"spring 腳本"的命令行工具。
二、傳統(tǒng)的DataSource配置
Java的javax.sql.DataSource接口提供了一個標(biāo)準(zhǔn)的使用數(shù)據(jù)庫連接的方法。傳統(tǒng)做法是,一個DataSource使用一個URL連
同相應(yīng)的證書去初始化一個數(shù)據(jù)庫連接。
開發(fā)中,一個項目中經(jīng)常會使用到不知一個數(shù)據(jù)源,本文主要講解如何在springboot下整合mybatis配置多數(shù)據(jù)源。主要對比下傳統(tǒng)的xml配置數(shù)據(jù)源和springboot下的數(shù)據(jù)源配置。
首先介紹下傳統(tǒng)的xml下如何配置多數(shù)據(jù)源
1、項目結(jié)構(gòu)
使用maven構(gòu)建的項目中,所有的數(shù)據(jù)源配置到DAO層,即圖中 subscribecore.dal module
2、dal的目錄結(jié)構(gòu)
1、數(shù)據(jù)庫對應(yīng)的java實體類。
2、每個庫對應(yīng)的mapper文件。
3、每個mapper文件對應(yīng)的到的xml文件。
4、生產(chǎn)環(huán)境\測試環(huán)境對應(yīng)的數(shù)據(jù)源配置文件。
5、每個數(shù)據(jù)庫對應(yīng)的配置文件。
3、具體的配置文件介紹
以mysql庫為例,詳細(xì)展開對mysql數(shù)據(jù)配置的介紹
1、java實體類
使用的mysql庫中的一張表,通過mybatis自動生成工具,生成了chartconfig類和chartconfigExample類。
2、msyql庫的mapper文件
3、mapper文件對應(yīng)的到的xml文件
4、mysql測試環(huán)境對應(yīng)的數(shù)據(jù)源配置文件
5、myssql數(shù)據(jù)庫對應(yīng)的配置文件
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:cache = "http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> < context:component-scan base-package = "com.zto.subscribecore" ></ context:component-scan > <!-- 數(shù)據(jù)源 --> < bean id = "mysqlDataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close" > <!-- 驅(qū)動名稱 --> < property name = "DriverClassName" value = "${mysql.DriverClassName}" /> <!-- JDBC連接串 --> < property name = "url" value = "${mysql.url}" /> <!-- 數(shù)據(jù)庫用戶名稱 --> < property name = "username" value = "${mysql.username}" /> <!-- 數(shù)據(jù)庫密碼 --> < property name = "password" value = "${mysql.password}" /> <!-- 連接池最大使用連接數(shù)量 --> < property name = "maxActive" value = "${mysql.maxActive}" /> <!-- 初始化大小 --> < property name = "initialSize" value = "${mysql.initialSize}" /> <!-- 獲取連接最大等待時間 --> < property name = "maxWait" value = "${mysql.maxWait}" /> <!-- 連接池最小空閑 --> < property name = "minIdle" value = "${mysql.minIdle}" /> <!-- 逐出連接的檢測時間間隔 --> < property name = "timeBetweenEvictionRunsMillis" value = "${mysql.timeBetweenEvictionRunsMillis}" /> <!-- 最小逐出時間 --> < property name = "minEvictableIdleTimeMillis" value = "${mysql.minEvictableIdleTimeMillis}" /> <!-- 測試有效用的SQL Query --> < property name = "validationQuery" value = "${mysql.validationQuery}" /> <!-- 連接空閑時測試是否有效 --> < property name = "testWhileIdle" value = "${mysql.testWhileIdle}" /> <!-- 獲取連接時測試是否有效 --> < property name = "testOnBorrow" value = "${mysql.testOnBorrow}" /> <!-- 歸還連接時是否測試有效 --> < property name = "testOnReturn" value = "${mysql.testOnReturn}" /> </ bean > < bean id = "mysqlTransactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "dataSource" ref = "mysqlDataSource" /> </ bean > < tx:annotation-driven transaction-manager = "mysqlTransactionManager" /> < bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer" > < property name = "basePackage" value = "com.zto.subscribecore.dal.mapper.mysql" /> < property name = "sqlSessionFactoryBeanName" value = "mysqlSqlSessionFactory" /> </ bean > < bean id = "mysqlSqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > < property name = "dataSource" ref = "mysqlDataSource" /> < property name = "mapperLocations" value = "classpath*:com/zto/subscribecore/dal/mapper/mysql/*.xml" ></ property > < property name = "typeAliasesPackage" value = "com.zto.subscribecore.dal.domain" /> </ bean > </ beans > |
配制文件步驟分解:
1、注入數(shù)據(jù)源,即 bean id為mysqlDatasource的配置文件,該配置可以從jdbc-dev.properties文件中讀取到對應(yīng)到數(shù)據(jù)庫配置文件。
2、聲明DataSourceTransactionManager 即 bean id為 mysqlTransactionManager 的配置文件,該配置 為事務(wù)管理,在spring中是對JdbcTemplate進行事務(wù)管理
3、自動掃描包,即 bean class 為 org.mybatis.spring.mapper.MapperScannerConfigurer的配置文件。該配置 將mapper接口生成的代理注入到spring。可以自動掃描該包名下的所有的文件,即 實體類,mapper接口類,和mapper接口對應(yīng)的xml文件。
4、創(chuàng)建seesion,即bean id為 mysqlSqlSessionFactory 的配置文件,該配置中指定了對應(yīng)mysql庫的xml文件和對應(yīng)的實體類的路徑。一個sqlSeesionFactory代表了一個數(shù)據(jù)源。(就相當(dāng)于產(chǎn)生連接池)
至此完成了mysql庫的配置。
三、springboot下的datasource配置
通過上面的配置文件,了解到數(shù)據(jù)源的相關(guān)配置,下面描述下如何在springboot下完成多數(shù)據(jù)源的配置工作。
1、目錄結(jié)構(gòu)
同樣,使用maven構(gòu)建多module的工程,但是取消了jdbc-properties,數(shù)據(jù)連接配置卸載寫在application.yml中。dao層的文件
配置的dal module中。
application.yml的內(nèi)容:
1、注入數(shù)據(jù)源,使用@Configuration注解,springboot在啟動時,會自動加載該類,和xml聲明類似。(同 <beans></beans),@Bean 注入一個類。@Value 注解,使用${} 從application.yml讀取配置。
DruidConfiguration類
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
|
@Configuration public class DruidConfiguration { @Bean (name = "vip" , initMethod = "init" , destroyMethod = "close" ) public DataSource compare1DataSource( @Value ( "${spring.datasource.vip.driver-class-name}" ) String driver, @Value ( "${spring.datasource.vip.url}" ) String url, @Value ( "${spring.datasource.vip.username}" ) String username, @Value ( "${spring.datasource.vip.password}" ) String password, @Value ( "${spring.datasource.vip.minIdle}" ) int minIdle, @Value ( "${spring.datasource.vip.maxActive}" ) int maxActive, @Value ( "${spring.datasource.vip.initialSize}" ) int initialSize, @Value ( "${spring.datasource.vip.timeBetweenEvictionRunsMillis}" ) long timeBetweenEvictionRunsMillis, @Value ( "${spring.datasource.vip.minEvictableIdleTimeMillis}" ) long minEvictableIdleTimeMillis, @Value ( "${spring.datasource.vip.validationQuery}" ) String validationQuery, @Value ( "${spring.datasource.vip.testWhileIdle}" ) boolean testWhileIdle, @Value ( "${spring.datasource.vip.testOnBorrow}" ) boolean testOnBorrow, @Value ( "${spring.datasource.vip.testOnReturn}" ) boolean testOnReturn) { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); druidDataSource.setMinIdle(minIdle); druidDataSource.setMaxActive(maxActive); druidDataSource.setInitialSize(initialSize); druidDataSource .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); druidDataSource .setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); druidDataSource.setValidationQuery(validationQuery); druidDataSource.setTestWhileIdle(testWhileIdle); druidDataSource.setTestOnBorrow(testOnBorrow); druidDataSource.setTestOnReturn(testOnReturn); return druidDataSource; } } |
2、指定domain類、mapper接口,xml配件文件的路徑,并指定映射關(guān)系
@MapperScan 注解,掃描該包名下的所有文件。
@Autowired+@Qualifier 注入 上面已經(jīng)聲明的 datasource 類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Configuration @MapperScan (basePackages = { "com.zto.merchantPlatform.mapper.vip" }, sqlSessionFactoryRef = "vipSqlSessionFactory" ) public class VipMybatisConfiguration { @Autowired @Qualifier ( "vip" ) private DataSource dataSource; @Bean (name = "vipSqlSessionFactory" ) public SqlSessionFactoryBean sqlSessionFactory( @Value ( "${mybatis.vip.mapperLocations}" ) String mapperLocations, @Value ( "${mybatis.vip.typeAliasesPackage}" )String typeAliasesPackage) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(mapperLocations)); sessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage); return sessionFactoryBean; } @Bean (name = "vipTransactionManager" ) public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource); } } |
3、mapper接口對應(yīng)的xml文件
4、數(shù)據(jù)源配置文件
5、指定mybatis下的mapper和xml文件的之間的映射關(guān)系。
6、mapper接口(即該庫下的所有表)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/wangqingqi20005/article/details/52613055