• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

springboot 如何配置多个jndi数据源

springboot 搞代码 4年前 (2022-01-05) 45次浏览 已收录 0个评论
文章目录[隐藏]

这篇文章主要介绍了springboot 如何配置多个jndi数据源的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

springboot 配置多个jndi数据源

1.在application.properties中,添加jndi配置

如下图

2.新建dataSourceConfig类

3.dataSourceConfig类详细代码

这里只贴出其中一个,多个数据源类似配置,只需更改 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 @Conf<strong style="color:transparent">来源gao@daima#com搞(%代@#码@网</strong>igurationProperties(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文件

添加以下配置,多个数据源写多个Resource即可,注意name需要和application.properties配置的名称一致

 

以上就是springboot 如何配置多个jndi数据源的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:springboot 如何配置多个jndi数据源
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址