springboot 如何配置多個(gè)jndi數(shù)據(jù)源
springboot 配置多個(gè)jndi數(shù)據(jù)源
1.在application.properties中,添加jndi配置
如下圖

2.新建dataSourceConfig類

3.dataSourceConfig類詳細(xì)代碼
這里只貼出其中一個(gè),多個(gè)數(shù)據(jù)源類似配置,只需更改 basePackages 路徑和 @Value("${spring.datasource.source1.jndi-name}"),以及下面名稱帶test1前綴的地方,不要和其他dataSourceConfig重名
@Configuration //注冊(cè)到springboot 容器中
@MapperScan(basePackages = "對(duì)應(yīng)的Mapper包路徑",sqlSessionTemplateRef = "test1SqlSessionTemplate")
public class DataSource1Config {
//application.properties中的jndi名稱
@Value("${spring.datasource.source1.jndi-name}")
private String jndiName;
@Bean(name = "test1DataSource",destroyMethod = "") // destroy method is disabled for Weblogic update app ability
@ConfigurationProperties(prefix = "spring.datasource.bigdata")
public DataSource bigdataDs() throws NamingException {
JndiObjectFactoryBean bean=new JndiObjectFactoryBean();
bean.setJndiName(jndiName);
bean.setProxyInterface(DataSource.class);
bean.setLookupOnStartup(false);
bean.afterPropertiesSet();
return (DataSource) bean.getObject();
}
@Bean(name = "test1SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//加載其他文件,如mapper.xml
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
//事務(wù)管理
@Bean(name = "test1TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4.打開tomcat目錄下context.xml文件
添加以下配置,多個(gè)數(shù)據(jù)源寫多個(gè)Resource即可,注意name需要和application.properties配置的名稱一致
<Resource name="jdbc/數(shù)據(jù)庫(kù)名稱" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javatest"/>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 教你使用springboot配置多數(shù)據(jù)源
- SpringBoot整合JDBC、Druid數(shù)據(jù)源的示例代碼
- 詳解SpringBoot+Mybatis實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換
- springboot redis使用lettuce配置多數(shù)據(jù)源的實(shí)現(xiàn)
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- 基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實(shí)現(xiàn)代碼
- SpringBoot使用Nacos動(dòng)態(tài)配置數(shù)據(jù)源的方法
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
相關(guān)文章
導(dǎo)致MyEclipse內(nèi)存不足的原因分析及解決辦法
這篇文章主要介紹了導(dǎo)致MyEclipse內(nèi)存不足的原因分析及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-01-01
Java?8?Stream?處理數(shù)據(jù)方法匯總
這篇文章主要介紹了Java?8?Stream處理數(shù)據(jù),Stream是Java?8?新引入的一個(gè)包它讓我們能用聲明式的方式處理數(shù)據(jù),Stream流式處理相較于傳統(tǒng)方法簡(jiǎn)潔高效,也便于進(jìn)行并發(fā)編程,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章內(nèi)容2022-06-06
SpringBoot 集成 Kettle的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot 集成 Kettle的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
windows下java環(huán)境變量的設(shè)置方法
在“系統(tǒng)變量”中,設(shè)置3項(xiàng)屬性,JAVA_HOME,PATH,CLASSPATH(大小寫無所謂),若已存在則點(diǎn)擊“編輯”,不存在則點(diǎn)擊“新建”2013-09-09

