基于spring+ibatis多数据库配置及访问

最近项目中需要用到两个数据库,找了不少资料,都没有很好的解决方案,于是自己动手搭了个架子。相信今后还是用得到的,在此做个记号。

1、配置两个数据源,

a、dataSourceOne  b、dataSourceTwo

spring-datasource.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
	default-autowire="byName">
	<!-- 数据库1 -->
	<bean id="dataSourceOne" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl" />
		<property name="username" value="admin1" />
		<property name="password" value="111111" />
		<property name="initialSize">
			<value>3</value>
		</property>
		<property name="maxActive">
			<value>30</value>
		</property>
		<property name="maxIdle">
			<value>20</value>
		</property>
		<property name="maxWait">
			<value>6000</value>
		</property>
		<property name="removeAbandoned">
			<value>true</value>
		</property>
		<property name="removeAbandonedTimeout">
			<value>180</value>
		</property>
		<property name="logAbandoned">
			<value>true</value>
		</property>
	</bean>
	<!-- 数据库2 -->
	<bean id="dataSourceTwo" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@192.168.1.20:1521:orcl" />
		<property name="username" value="admin2" />
		<property name="password" value="222222" />
		<property name="initialSize">
			<value>3</value>
		</property>
		<property name="maxActive">
			<value>30</value>
		</property>
		<property name="maxIdle">
			<value>20</value>
		</property>
		<property name="maxWait">
			<value>6000</value>
		</property>
		<property name="removeAbandoned">
			<value>true</value>
		</property>
		<property name="removeAbandonedTimeout">
			<value>180</value>
		</property>
		<property name="logAbandoned">
			<value>true</value>
		</property>
	</bean>
	<!--动态选择数据源       编写spring 配置文件的配置多数源映射关系 -->
	<bean class="com.sendinfo.xspring.util.DynamicDataSource" id="dataSource">
	    <property name="targetDataSources" >
	       <map key-type="java.lang.String">
	          <entry value-ref="dataSourceOne" key="1"></entry>
	          <entry value-ref="dataSourceTwo" key="2"></entry>
	       </map>
	    </property>
             <!--默认连接的数据源-->
            <property name="defaultTargetDataSource" ref="dataSourceOne"></property>
	</bean>
</beans>

2、配置事物管理器

spring-manager.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                     http://www.springframework.org/schema/beans/spring-beans.xsd 
                     http://www.springframework.org/schema/tx 
                     http://www.springframework.org/schema/tx/spring-tx.xsd 
                     http://www.springframework.org/schema/aop 
                     http://www.springframework.org/schema/aop/spring-aop.xsd " 
	default-autowire="byName">
	
 	<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>  
</beans>
3、java的dao层访问技巧
/**
 * 角色
 * @author liang
 *
 */
@Repository
public class RoleDao{
	/**
	 * 查询角色列表
	 * @param queryMap
	 * @return
	 */
	public List<Role> getRoles(Map<String,Object> queryMap){
		DbContextHolder.setDbType("2");//连接数据源databaseTwo
		List<Role> roleList = getObjList(NAMESPACE_ROLE,queryMap,"ROLE");
		DbContextHolder.clearDbtype();//清空本次连接数据源,还原为默认的databaseOne
		return roleList;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值