springboot+mybatis连接MySQL和oracle的jndi数据源

闲话少说,上代码。

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;

@Configuration
public class DataSourceConfig {
	
	@Value("${spring.datasource.oracle-jndi-name}")
	private String oracleJndiName;
	
	@Value("${spring.datasource.jndi-name}")
	private String mysqlJndiName;
	
	@Primary
	@Bean("dataSourceFirst")
	public DataSource mysqlJNDISource() {
		JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
		dataSourceLookup.setResourceRef(true);
		return dataSourceLookup.getDataSource(mysqlJndiName);
	}
	
	@Bean("dataSourceSecond")
	public DataSource oracleJNDISource() {
		JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
		dataSourceLookup.setResourceRef(true);
		return dataSourceLookup.getDataSource(oracleJndiName);
	}
}
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(basePackages="com.test.mybatis.mapper.**",sqlSessionFactoryRef="sqlSessionFactoryFirst")
public class MysqlDSConfig {
	
	@Autowired
	@Qualifier("dataSourceFirst")
	private DataSource dsFirst;
	
	@Bean
    @Primary //springboot2.0+的改动点
	public SqlSessionFactory sqlSessionFactoryFirst() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dsFirst);
		return factoryBean.getObject();
	}
	
	@Bean
    @Primary //springboot2.0+的改动点
	public SqlSessionTemplate sqlSessionTemplate() throws Exception {
		return new SqlSessionTemplate(sqlSessionFactoryFirst());
	}
	
}

 

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan(basePackages="com.test.oracle.mapper.**",sqlSessionFactoryRef="sqlSessionFactorySecond")
public class OracleDSConfig {
	
	@Autowired
	@Qualifier("dataSourceSecond")
	private DataSource dataSourceSecond;
	
	@Bean
	public SqlSessionFactory sqlSessionFactorySecond() throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSourceSecond);
		return factoryBean.getObject();
	}
	
	@Bean
	public SqlSessionTemplate sqlSessionTemplate() throws Exception {
		return new SqlSessionTemplate(sqlSessionFactorySecond());
	}
}

最后创建对应的mapper类和service类

使用时注入对应的service类即可。

当使用springboot2.0+版本时,以上方法有些改动,具体如下:

i、application.yml中的数据连接配置spring.datasource.url改为spring.datasource.jdbc-url

ii、参见上面的注释部分

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值