色欲av一区久久精品_久久综合色综合色88_无码在线观看不卡_色黄视频网站_亚洲国产精品久久久久秋霞66

springboot 配置多個數(shù)據(jù)源

時間:2023-05-17

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ù)源java
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;
}

文章標(biāo)簽:

Copyright ? 2016 廣州思洋文化傳播有限公司,保留所有權(quán)利。 粵ICP備09033321號

與項目經(jīng)理交流
掃描二維碼
與項目經(jīng)理交流
掃描二維碼
與項目經(jīng)理交流
ciya68