博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring环境中支持多数据库源连接的配置及使用方法
阅读量:7139 次
发布时间:2019-06-28

本文共 2962 字,大约阅读时间需要 9 分钟。

Spring环境中支持多数据库源连接的配置及使用方法

可加载xml配置文件中包含以下bean内容。

<beans>

<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<!-- Class name for the Connector/J driver -->

<property name="driverClassName">

<value>oracle.jdbc.driver.OracleDriver</value>

</property>

<property name="url">

<value>jdbc:oracle:thin:@(DESCRIPTION =

(ADDRESS = (PROTOCOL = TCP)(HOST = db-ip-hostname)(PORT = 1521))

(CONNECT_DATA =

(SERVER = DEDICATED)

(SERVICE_NAME = service_name)

<!--sid = sid_name -->

)

)</value>

</property>

<property name="username">

<value>user</value>

</property>

<property name="password">

<value>password</value>

</property>

</bean>

<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">

<property name="dataSource" ref="dataSource1"></property> 

</bean>

<bean id="transactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource1"></property>

</bean> 

<!--business dao bean-->

<bean id="businessDao1" class="com.example.BusinessDao1">

<property name="jdbcTemplate" ref="jdbcTemplate1"></property>

</bean>

<bean id="businessDao2" class="com.example.BusinessDao2">

<property name="jdbcTemplate" ref="jdbcTemplate1"></property>

</bean>

<bean id="businessDao3" class="com.example.BusinessDao1">

<property name="jdbcTemplate" ref="jdbcTemplate3"></property>

</bean>

<bean id="businessDao4" class="com.example.BusinessDao4">

<property name="jdbcTemplate" ref="jdbcTemplate1"></property>

</bean>

<bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 

<property name="transactionManager" ref="transactionManager1"/> 

<property name="transactionAttributes"> 

<props>

<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="import*">PROPAGATION_REQUIRED,-Exception</prop>

<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

</props> 

</property> 

</bean> 

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 

<property name="beanNames">

<value>

businessDao1, businessDao2, 

businessDao3, businessDao4

</value>

</property>

<property name="interceptorNames"> 

<list> 

<value>transactionInterceptor1</value>

</list> 

</property> 

</bean> 

<bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"> 

<property name="transactionInterceptor" ref="transactionInterceptor1"/> 

</bean> 

</beans>

通过改变上面的dataSource1, jdbcTemplate1,transactionManager1以及自动代理bean的内容即可复制成其他数据库的一套配置。所有businessbean不需要重新写,java代码也不需要改动,是一个较为省事的多数据库支持方案。如果需要同时支持mysql/oracle等不同类型数据库,也只是sql语句加载的问题,事务管理已经被简单包装进了。(不同数据库类型加载不同sql语句,只需要为businessbean加dbtype属性并在xml里配入即可。)

需要注意的一点,如果business dao 有实现interface,则引用的地方必须也用interface名声明,否则无法cast。这个是动态代理实现机制的原因,因为CGlib机制会cast成一个子接口返回。

本文转自 dannyy1026 51CTO博客,原文链接:

http://blog.51cto.com/dannyyuan/160781

转载地址:http://citrl.baihongyu.com/

你可能感兴趣的文章
Windows完成端口 IOCP模型(一)
查看>>
修改roo的密码 虚拟机vmware8.04 Centos 6.3
查看>>
Struts2 注解
查看>>
有关xerosploit运行报错问题的有效解决方案
查看>>
ABP官方文档翻译 1.4 启动配置
查看>>
js框架简明
查看>>
Java volatile 关键字
查看>>
http 头信息详解
查看>>
文件复制
查看>>
ATS项目更新(4) 更新DLL到远程服务器
查看>>
mac 多显示器焦点快速切换
查看>>
第六周学习进度报告
查看>>
nginx发布静态网页
查看>>
Hadoop 面试题之一
查看>>
有关方法重载的实例(例4-10)
查看>>
用数组模拟邻接表
查看>>
**Git中的AutoCRLF与SafeCRLF换行符问题
查看>>
Android布局文件layout.xml的一些属性值
查看>>
三种new
查看>>
多项式与三角函数求导——BUAA OO 第一单元作业总结
查看>>