Spring环境中支持多数据库源连接的配置及使用方法
来源:互联网 发布:java b2b开源电商平台 编辑:程序博客网 时间:2024/06/05 03:20
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成一个子接口返回。
可加载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成一个子接口返回。
0 0
- Spring环境中支持多数据库源连接的配置及使用方法
- Spring中TransactionTemplate 的配置及使用方法
- spring 中多数据源的配置
- spring多数据库源配置
- spring与ibatis的整合及多数据库连接
- spring+mybatis环境下多数据源的配置
- 如何在spring中配置使得mybatis3.1.1中支持vendor方式的multi-db(多数据库)
- spring boot中多环境配置支持
- Spring 多数据源连接配置 解决方案
- 企业项目中Spring多数据源的配置方法
- spring boot 中 Mybatis plus 多数据源的配置
- Spring Boot,Spring Data JPA多数据源支持配置
- spring的多数据源配置(spring+ibatis + oracle环境下)
- Spring的多数据源配置(Spring+iBATIS + Oracle环境下)
- spring的多数据源配置(spring+ibatis + oracle环境下)
- spring中多数据源配置
- spring中多数据配置2
- Spring mybatis jbpm 支持多数据源配置
- php实现时间戳转换在32位系统下超过2038年的自定义处理函
- Spring实现AOP的4种方式
- [iOS] gitHUB 冲突方法 Line XX: StartTag: invalid element name
- 佛祖保佑,永无bug
- div选中效果及鼠标指针变形
- Spring环境中支持多数据库源连接的配置及使用方法
- ReactJS学习系列课程2(React环境搭建)
- 时间与日期的转换
- 2016.06.24回顾
- 在ListView的item项里使用CheckBox或者Button时,OnItemClickListener无响应的问题
- Android5.0中Cardview的简单使用
- sockfd连接后如何获得对方和本机的ip地址和端口
- 第16周小玩文件
- 自定义View之绘制钟表