?一、springboot 簡介
SpringBoot使開發獨立的,產品級別的基于Spring的應用變得非常簡單,你只需"just run"。 我們為Spring平臺及第三方庫提 供開箱即用的設置,這樣你就可以有條不紊地開始。多數Spring Boot應用需要很少的Spring配置。
你可以使用SpringBoot創建Java應用,并使用 java -jar 啟動它或采用傳統的war部署方式。我們也提供了一個運行"spring 腳本"的命令行工具。
二、傳統的DataSource配置
Java的javax.sql.DataSource接口提供了一個標準的使用數據庫連接的方法。傳統做法是,一個DataSource使用一個URL連
同相應的證書去初始化一個數據庫連接。
開發中,一個項目中經常會使用到不知一個數據源,本文主要講解如何在springboot下整合mybatis配置多數據源。主要對比下傳統的xml配置數據源和springboot下的數據源配置。
首先介紹下傳統的xml下如何配置多數據源
1、項目結構
使用maven構建的項目中,所有的數據源配置到DAO層,即圖中 subscribecore.dal module
2、dal的目錄結構
1、數據庫對應的java實體類。
2、每個庫對應的mapper文件。
3、每個mapper文件對應的到的xml文件。
4、生產環境\測試環境對應的數據源配置文件。
5、每個數據庫對應的配置文件。
3、具體的配置文件介紹
以mysql庫為例,詳細展開對mysql數據配置的介紹
1、java實體類
使用的mysql庫中的一張表,通過mybatis自動生成工具,生成了chartconfig類和chartconfigExample類。
2、msyql庫的mapper文件
3、mapper文件對應的到的xml文件
4、mysql測試環境對應的數據源配置文件
5、myssql數據庫對應的配置文件
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 > <!-- 數據源 --> < bean id = "mysqlDataSource" class = "com.alibaba.druid.pool.DruidDataSource" init-method = "init" destroy-method = "close" > <!-- 驅動名稱 --> < property name = "DriverClassName" value = "${mysql.DriverClassName}" /> <!-- JDBC連接串 --> < property name = "url" value = "${mysql.url}" /> <!-- 數據庫用戶名稱 --> < property name = "username" value = "${mysql.username}" /> <!-- 數據庫密碼 --> < property name = "password" value = "${mysql.password}" /> <!-- 連接池最大使用連接數量 --> < 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、注入數據源,即 bean id為mysqlDatasource的配置文件,該配置可以從jdbc-dev.properties文件中讀取到對應到數據庫配置文件。
2、聲明DataSourceTransactionManager 即 bean id為 mysqlTransactionManager 的配置文件,該配置 為事務管理,在spring中是對JdbcTemplate進行事務管理
3、自動掃描包,即 bean class 為 org.mybatis.spring.mapper.MapperScannerConfigurer的配置文件。該配置 將mapper接口生成的代理注入到spring。可以自動掃描該包名下的所有的文件,即 實體類,mapper接口類,和mapper接口對應的xml文件。
4、創建seesion,即bean id為 mysqlSqlSessionFactory 的配置文件,該配置中指定了對應mysql庫的xml文件和對應的實體類的路徑。一個sqlSeesionFactory代表了一個數據源。(就相當于產生連接池)
至此完成了mysql庫的配置。
三、springboot下的datasource配置
通過上面的配置文件,了解到數據源的相關配置,下面描述下如何在springboot下完成多數據源的配置工作。
1、目錄結構
同樣,使用maven構建多module的工程,但是取消了jdbc-properties,數據連接配置卸載寫在application.yml中。dao層的文件
配置的dal module中。
application.yml的內容:
1、注入數據源,使用@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配件文件的路徑,并指定映射關系
@MapperScan 注解,掃描該包名下的所有文件。
@Autowired+@Qualifier 注入 上面已經聲明的 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接口對應的xml文件
4、數據源配置文件
5、指定mybatis下的mapper和xml文件的之間的映射關系。
6、mapper接口(即該庫下的所有表)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/wangqingqi20005/article/details/52613055