一、springboot 簡(jiǎn)介
SpringBoot使開(kāi)發(fā)獨(dú)立的,產(chǎn)品級(jí)別的基于Spring的應(yīng)用變得非常簡(jiǎn)單,你只需"just run"。 我們?yōu)镾pring平臺(tái)及第三方庫(kù)提 供開(kāi)箱即用的設(shè)置,這樣你就可以有條不紊地開(kāi)始。多數(shù)Spring Boot應(yīng)用需要很少的Spring配置。
你可以使用SpringBoot創(chuàng)建Java應(yīng)用,并使用 java -jar 啟動(dòng)它或采用傳統(tǒng)的war部署方式。我們也提供了一個(gè)運(yùn)行"spring 腳本"的命令行工具。
二、傳統(tǒng)的DataSource配置
Java的javax.sql.DataSource接口提供了一個(gè)標(biāo)準(zhǔn)的使用數(shù)據(jù)庫(kù)連接的方法。傳統(tǒng)做法是,一個(gè)DataSource使用一個(gè)URL連
同相應(yīng)的證書(shū)去初始化一個(gè)數(shù)據(jù)庫(kù)連接。
開(kāi)發(fā)中,一個(gè)項(xiàng)目中經(jīng)常會(huì)使用到不知一個(gè)數(shù)據(jù)源,本文主要講解如何在springboot下整合mybatis配置多數(shù)據(jù)源。主要對(duì)比下傳統(tǒng)的xml配置數(shù)據(jù)源和springboot下的數(shù)據(jù)源配置。
首先介紹下傳統(tǒng)的xml下如何配置多數(shù)據(jù)源
1、項(xiàng)目結(jié)構(gòu)
使用maven構(gòu)建的項(xiàng)目中,所有的數(shù)據(jù)源配置到DAO層,即圖中 subscribecore.dal module
2、dal的目錄結(jié)構(gòu)
1、數(shù)據(jù)庫(kù)對(duì)應(yīng)的java實(shí)體類(lèi)。
2、每個(gè)庫(kù)對(duì)應(yīng)的mapper文件。
3、每個(gè)mapper文件對(duì)應(yīng)的到的xml文件。
4、生產(chǎn)環(huán)境\測(cè)試環(huán)境對(duì)應(yīng)的數(shù)據(jù)源配置文件。
5、每個(gè)數(shù)據(jù)庫(kù)對(duì)應(yīng)的配置文件。
3、具體的配置文件介紹
以mysql庫(kù)為例,詳細(xì)展開(kāi)對(duì)mysql數(shù)據(jù)配置的介紹
1、java實(shí)體類(lèi)
使用的mysql庫(kù)中的一張表,通過(guò)mybatis自動(dòng)生成工具,生成了chartconfig類(lèi)和chartconfigExample類(lèi)。
2、msyql庫(kù)的mapper文件
3、mapper文件對(duì)應(yīng)的到的xml文件
4、mysql測(cè)試環(huán)境對(duì)應(yīng)的數(shù)據(jù)源配置文件
5、myssql數(shù)據(jù)庫(kù)對(duì)應(yīng)的配置文件
<?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ū)動(dòng)名稱(chēng) --> <property name="DriverClassName" value="${mysql.DriverClassName}"/> <!-- JDBC連接串 --> <property name="url" value="${mysql.url}"/> <!-- 數(shù)據(jù)庫(kù)用戶名稱(chēng) --> <property name="username" value="${mysql.username}"/> <!-- 數(shù)據(jù)庫(kù)密碼 --> <property name="password" value="${mysql.password}"/> <!-- 連接池大使用連接數(shù)量 --> <property name="maxActive" value="${mysql.maxActive}"/> <!-- 初始化大小 --> <property name="initialSize" value="${mysql.initialSize}"/> <!-- 獲取連接大等待時(shí)間 --> <property name="maxWait" value="${mysql.maxWait}"/> <!-- 連接池最小空閑 --> <property name="minIdle" value="${mysql.minIdle}"/> <!-- 逐出連接的檢測(cè)時(shí)間間隔 --> <property name="timeBetweenEvictionRunsMillis" value="${mysql.timeBetweenEvictionRunsMillis}"/> <!-- 最小逐出時(shí)間 --> <property name="minEvictableIdleTimeMillis" value="${mysql.minEvictableIdleTimeMillis}"/> <!-- 測(cè)試有效用的SQL Query --> <property name="validationQuery" value="${mysql.validationQuery}"/> <!-- 連接空閑時(shí)測(cè)試是否有效 --> <property name="testWhileIdle" value="${mysql.testWhileIdle}"/> <!-- 獲取連接時(shí)測(cè)試是否有效 --> <property name="testOnBorrow" value="${mysql.testOnBorrow}"/> <!-- 歸還連接時(shí)是否測(cè)試有效 --> <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>
分享題目:springboot下配置多數(shù)據(jù)源的方法-創(chuàng)新互聯(lián)
瀏覽地址:http://aaarwkj.com/article48/isjep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站制作、服務(wù)器托管、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)公司、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容