您的位置:首页 > 编程语言 > Java开发

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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: