spring+ibatis配置多数据源
2011-06-07 16:08
253 查看
说明:最近因为项目需要,经过多方努力和救助,终于实现了spring+ibatis的动态数据源切换配置,在此贴出,以供大家参考,同时希望大家多提意见……
下面是详细代码:
1. spring的applicationContext.xml配置文件:
a. 属性文件配置:
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value> classpath:resource/DataSourceConfig.properties
</value>
</property>
</bean>
b. DataSource: C3P0连接池:
【注意:由于存在多数据源,故此步按需要多次配置】
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${gpt.driverClassName}" />
<property name="jdbcUrl" value="${gpt.url}" />
<property name="user" value="${gpt.username}" />
<property name="password" value="${gpt.password}" />
<property name="minPoolSize" value="${gpt.minPoolSize}" />
<property name="acquireRetryAttempts"
value="${gpt.acquireRetryAttempts}" />
<property name="acquireIncrement"
value="${gpt.acquireIncrement}" />
<property name="maxIdleTime" value="${gpt.maxIdleTime}" />
<property name="initialPoolSize" value="${gpt.initialPoolSize}" />
<property name="maxPoolSize" value="${gpt.maxPoolSize}" />
<property name="idleConnectionTestPeriod"
value="${gpt.idleConnectionTestPeriod}" />
</bean>
c. spring事务管理配置:
【红色标记处,多个数据源配置多个】
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list><value>transactionInterceptor1</value> <value>transactionInterceptor2</value> <value>transactionInterceptor3</value>
</list>
</property>
</bean>
<!-- 标记段重复配置,具体看自己需要 start -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="do*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- end -->
d. ibatis sql map 配置:
【同理,有多少个数据源就配置多少遍】
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:com/sqlmap/sqlmap-config.xml" />
就这样,spring配置文件就准备好了。
</bean>
[color=blue][/color][size=large]spring+struts+ibatis 动态数据源配置 (二)
经过上一步对spring配置文件的配置,接下来我们新建一个接口文件:
public interface IBaseSqlMapClientDaoSupport {
public void choseSqlClient(String name);
}
同时也新建一个实现类:
public class BaseSqlMapClientDaoSupport extends SqlMapClientDaoSupport implements ApplicationContextAware {
protected ApplicationContext context;
protected ApplicationContext getContext() {
// return WebApplicationContextUtils
// .getWebApplicationContext(ServletActionContext
// .getServletContext());
return context;
}
public void choseSqlClient(String name) {
SqlMapClient client = (SqlMapClient) getContext().getBean(name);
setSqlMapClientTemplate(new SqlMapClientTemplate(client));
afterPropertiesSet();
}
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
}
}
到此为止,值得注意的地方都配置完毕,最后就是我们DAO接口和实现类的配置了,在这里我就简单的贴下代码:
public interface DemoDAO extends IBaseSqlMapClientDaoSupport{
...操作方法...
}
public class DemoDaoImpl extends BaseSqlMapClientDaoSupport implements DemoDAO {
...操作方法...
比如
public void addDemo(DemoDO demoDo) {
choseSqlClient("设置指定的数据源");【这步最好放到server实现类里面,在你要调用具体的Dao的前面设置即可。】
getSqlMapClientTemplate.insert("..",demoDo);
}
}
下面是详细代码:
1. spring的applicationContext.xml配置文件:
a. 属性文件配置:
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value> classpath:resource/DataSourceConfig.properties
</value>
</property>
</bean>
b. DataSource: C3P0连接池:
【注意:由于存在多数据源,故此步按需要多次配置】
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${gpt.driverClassName}" />
<property name="jdbcUrl" value="${gpt.url}" />
<property name="user" value="${gpt.username}" />
<property name="password" value="${gpt.password}" />
<property name="minPoolSize" value="${gpt.minPoolSize}" />
<property name="acquireRetryAttempts"
value="${gpt.acquireRetryAttempts}" />
<property name="acquireIncrement"
value="${gpt.acquireIncrement}" />
<property name="maxIdleTime" value="${gpt.maxIdleTime}" />
<property name="initialPoolSize" value="${gpt.initialPoolSize}" />
<property name="maxPoolSize" value="${gpt.maxPoolSize}" />
<property name="idleConnectionTestPeriod"
value="${gpt.idleConnectionTestPeriod}" />
</bean>
c. spring事务管理配置:
【红色标记处,多个数据源配置多个】
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list><value>transactionInterceptor1</value> <value>transactionInterceptor2</value> <value>transactionInterceptor3</value>
</list>
</property>
</bean>
<!-- 标记段重复配置,具体看自己需要 start -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="transactionInterceptor1" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="do*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- end -->
d. ibatis sql map 配置:
【同理,有多少个数据源就配置多少遍】
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:com/sqlmap/sqlmap-config.xml" />
就这样,spring配置文件就准备好了。
</bean>
[color=blue][/color][size=large]spring+struts+ibatis 动态数据源配置 (二)
经过上一步对spring配置文件的配置,接下来我们新建一个接口文件:
public interface IBaseSqlMapClientDaoSupport {
public void choseSqlClient(String name);
}
同时也新建一个实现类:
public class BaseSqlMapClientDaoSupport extends SqlMapClientDaoSupport implements ApplicationContextAware {
protected ApplicationContext context;
protected ApplicationContext getContext() {
// return WebApplicationContextUtils
// .getWebApplicationContext(ServletActionContext
// .getServletContext());
return context;
}
public void choseSqlClient(String name) {
SqlMapClient client = (SqlMapClient) getContext().getBean(name);
setSqlMapClientTemplate(new SqlMapClientTemplate(client));
afterPropertiesSet();
}
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.context = context;
}
}
到此为止,值得注意的地方都配置完毕,最后就是我们DAO接口和实现类的配置了,在这里我就简单的贴下代码:
public interface DemoDAO extends IBaseSqlMapClientDaoSupport{
...操作方法...
}
public class DemoDaoImpl extends BaseSqlMapClientDaoSupport implements DemoDAO {
...操作方法...
比如
public void addDemo(DemoDO demoDo) {
choseSqlClient("设置指定的数据源");【这步最好放到server实现类里面,在你要调用具体的Dao的前面设置即可。】
getSqlMapClientTemplate.insert("..",demoDo);
}
}
相关文章推荐
- 利用spring+ibatiS技术,在spring中配置多个数据源,并实现动态切换。
- 利用spring+ibatiS技术,在spring中配置多个数据源,并实现动态切换。
- Spring+ibatis 多数据源的配置和使用
- spring+ibatis 双数据源配置
- Spring+iBatis多数据源的动态配置方案
- Spring+iBatis多数据源的动态配置方案
- spring+ibatis 配置多个数据源,jdbc 和 jndi 实现
- Spring+iBatis多数据源的动态配置方案
- Spring+iBatis多数据源的动态配置方案
- Spring配置基于数据源的数据管理器适用于jdbc以及iBatis
- spring+ibatis【基本配置及动态sql】
- spring + ibatis 多数据源事务(分布式事务)管理配置方法(转)
- spring + ibatis 多数据源事务(分布式事务)管理配置方法(转)
- 利用AbstractRoutingDataSource实现动态数据源切换(Spring+Ibatis)
- spring+ibatis事务配置
- spring+ibatis整合之配置文件
- 多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)
- Spring+ibatis动态管理数据源
- spring+ibatis实现DB的动态切换1(如何配置)
- spring + ibatis 多数据源事务(分布式事务)管理配置方法(转)