1、在application.properties文件配置兩個數(shù)據(jù)源
復(fù)制代碼
#默許運(yùn)用tomcat-jdbc
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.data1.url=jdbc:mysql://127.0.0.1:3306/mysql-boot
spring.datasource.data1.username=root
spring.datasource.data1.password=123123
spring.datasource.data1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.data2.url=jdbc:mysql://127.0.0.1:3306/mysql-confswh
spring.datasource.data2.username=root
spring.datasource.data2.password=123123
spring.datasource.data2.driver-class-name=com.mysql.jdbc.Driver
復(fù)制代碼
2、創(chuàng)立一個datasource包,新建DataSource1,DataSource2兩個文件,經(jīng)過注解來配置數(shù)據(jù)源
DataSource1
復(fù)制代碼
packagecom.springboot.datasource;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.mybatis.spring.SqlSessionFactoryBean;
importorg.mybatis.spring.SqlSessionTemplate;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.context.annotation.Primary;
importorg.springframework.jdbc.datasource.DataSourceTransactionManager;
importjavax.sql.DataSource;
@Configuration//注解到spring容器中
@MapperScan(basePackages=”com.springboot.data1.mapper”,sqlSessionFactoryRef=”data1SqlSessionFactory”)
publicclassDataSource1{
/**
*返回data1數(shù)據(jù)庫的數(shù)據(jù)源
*@return
*/
@Bean(name=”data1Source”)
@Primary//主數(shù)據(jù)源
@ConfigurationProperties(prefix=”spring.datasource.data1″)
publicDataSourcedataSource(){
returnDataSourceBuilder.create().build();
}
/**
*返回data1數(shù)據(jù)庫的會話工廠
*@paramds
*@return
*@throwsException
*/
@Bean(name=”data1SqlSessionFactory”)
@Primary
publicSqlSessionFactorysqlSessionFactory(@Qualifier(“data1Source”)DataSourceds)throwsException{
SqlSessionFactoryBeanbean=newSqlSessionFactoryBean();
bean.setDataSource(ds);
returnbean.getObject();
}
/**
*返回data1數(shù)據(jù)庫的會話模板
*@paramsessionFactory
*@return
*@throwsException
*/
@Bean(name=”data1SqlSessionTemplate”)
@Primary
publicSqlSessionTemplatesqlSessionTemplate(@Qualifier(“data1SqlSessionFactory”)SqlSessionFactorysessionFactory)throwsException{
returnnewSqlSessionTemplate(sessionFactory);
}
/**
*返回data1數(shù)據(jù)庫的事務(wù)
*@paramds
*@return
*/
@Bean(name=”data1TransactionManager”)
@Primary
publicDataSourceTransactionManagertransactionManager(@Qualifier(“data1Source”)DataSourceds){
returnnewDataSourceTransactionManager(ds);
}
}
復(fù)制代碼
DataSource2:
復(fù)制代碼
packagecom.springboot.datasource;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.mybatis.spring.SqlSessionFactoryBean;
importorg.mybatis.spring.SqlSessionTemplate;
importorg.mybatis.spring.annotation.MapperScan;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
importorg.springframework.boot.context.properties.ConfigurationProperties;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.jdbc.datasource.DataSourceTransactionManager;
importjavax.sql.DataSource;
@Configuration//注解到spring容器中
@MapperScan(basePackages=”com.springboot.data2.mapper”,sqlSessionFactoryRef=”data2SqlSessionFactory”)
publicclassDataSource2{
/**
*返回data2數(shù)據(jù)庫的數(shù)據(jù)源
*@return
*/
@Bean(name=”data2Source”)
@ConfigurationProperties(prefix=”spring.datasource.data2″)
publicDataSourcedataSource(){
returnDataSourceBuilder.create().build();
}
/**
*返回data2數(shù)據(jù)庫的會話工廠
*@paramds
*@return
*@throwsException
*/
@Bean(name=”data2SqlSessionFactory”)
publicSqlSessionFactorysqlSessionFactory(@Qualifier(“data2Source”)DataSourceds)throwsException{
SqlSessionFactoryBeanbean=newSqlSessionFactoryBean();
bean.setDataSource(ds);
returnbean.getObject();
}
/**
*返回data2數(shù)據(jù)庫的會話模板
*@paramsessionFactory
*@return
*@throwsException
*/
@Bean(name=”data2SqlSessionTemplate”)
publicSqlSessionTemplatesqlSessionTemplate(@Qualifier(“data2SqlSessionFactory”)SqlSessionFactorysessionFactory)throwsException{
returnnewSqlSessionTemplate(sessionFactory);
}
/**
*返回data2數(shù)據(jù)庫的事務(wù)
*@paramds
*@return
*/
@Bean(name=”data2TransactionManager”)
publicDataSourceTransactionManagertransactionManager(@Qualifier(“data2Source”)DataSourceds){
returnnewDataSourceTransactionManager(ds);
}
}
復(fù)制代碼
3、測試Service&Mapper
@RequestMapping(“getTopicList”)
publicList<Map<String,Object>>getTopicList(){
List<Map<String,Object>>list1=userServiceData1.getTopicListData1();
List<Map<String,Object>>list2=userServiceData2.getTopicListData2();
returnlist1;
}
廣州天河區(qū)珠江新城富力盈力大廈北塔2706
020-38013166(網(wǎng)站咨詢專線)
400-001-5281 (售后服務(wù)熱線)
深圳市坂田十二橡樹莊園F1-7棟
Site/ http://www.szciya.com
E-mail/ itciya@vip.163.com
品牌服務(wù)專線:400-001-5281
長沙市天心區(qū)芙蓉中路三段398號新時空大廈5樓
聯(lián)系電話/ (+86 0731)88282200
品牌服務(wù)專線/ 400-966-8830
旗下運(yùn)營網(wǎng)站:
Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號