springboot 如何配置多個jndi數(shù)據(jù)源
springboot 配置多個jndi數(shù)據(jù)源
1.在application.properties中,添加jndi配置
如下圖
2.新建dataSourceConfig類
3.dataSourceConfig類詳細代碼
這里只貼出其中一個,多個數(shù)據(jù)源類似配置,只需更改 basePackages 路徑和 @Value("${spring.datasource.source1.jndi-name}"),以及下面名稱帶test1前綴的地方,不要和其他dataSourceConfig重名
@Configuration //注冊到springboot 容器中 @MapperScan(basePackages = "對應的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(); } //事務管理 @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文件
添加以下配置,多個數(shù)據(jù)源寫多個Resource即可,注意name需要和application.properties配置的名稱一致
<Resource name="jdbc/數(shù)據(jù)庫名稱" 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"/>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 教你使用springboot配置多數(shù)據(jù)源
- SpringBoot整合JDBC、Druid數(shù)據(jù)源的示例代碼
- 詳解SpringBoot+Mybatis實現(xiàn)動態(tài)數(shù)據(jù)源切換
- springboot redis使用lettuce配置多數(shù)據(jù)源的實現(xiàn)
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- 基于注解的springboot+mybatis的多數(shù)據(jù)源組件的實現(xiàn)代碼
- SpringBoot使用Nacos動態(tài)配置數(shù)據(jù)源的方法
- SpringBoot多數(shù)據(jù)源配置詳細教程(JdbcTemplate、mybatis)
相關(guān)文章
idea在工具欄中顯示快速創(chuàng)建包和類的圖標的詳細步驟
點擊需要創(chuàng)建包或者類的位置,在點擊對用的圖標就可以快速創(chuàng)建類或者包了,下面小編給大家介紹idea在工具欄中顯示快速創(chuàng)建包和類的圖標的詳細步驟,感興趣的朋友一起看看吧2024-02-02一文了解Java Log框架徹底搞懂Log4J,Log4J2,LogBack,SLF4J
本文主要介紹了一文了解Java Log框架徹底搞懂Log4J,Log4J2,LogBack,SLF4J,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03Kotlin 語言中調(diào)用 JavaScript 方法實例詳解
這篇文章主要介紹了Kotlin 語言中調(diào)用 JavaScript 方法實例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06使用IntelliJ IDEA2020.2.2 x64 新建java項目并且輸出Hello World
這篇文章主要介紹了使用IntelliJ IDEA2020.2.2 x64 新建java項目并且輸出Hello World,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11SpringBoot?中使用?Validation?校驗參數(shù)的方法詳解
Validation?是用于檢查程序代碼中參數(shù)的有效性的框架,作為?Spring?框架中的一個參數(shù)校驗工具,集成在?spring-context?包中,這篇文章主要介紹了SpringBoot?中使用?Validation?校驗參數(shù),需要的朋友可以參考下2022-05-05