spring+ibatis 配置多个数据源,jdbc 和 jndi 实现
来源:互联网 发布:百度地图纠偏算法 编辑:程序博客网 时间:2024/05/17 07:43
需求:一个项目中需要链接多个数据源,(操作多个数据库数据表)
项目实战:项目是采用spring + ibatis,采用jetty容器实现。
针对jdbc和jndi实现:
jdbc我是单元测试需要使用,所以也需要配置。
jndi是web容器中的实现。
1. jdbc
a.配置数据源
b.加载配置文件的时候,需要手动设置AutowireMode类型
c. 配置dao的时候,根据不同数据库操作配置不同的sqlmapclient
<beans default-autowire="byName"><bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="${xx1.jdbc.url}" /><property name="username" value="${xx1.jdbc.username}" /><property name="password" value="${xx1.jdbc.password}" /></bean><bean id="dataSourceCoop" class="org.springframework.jdbc.datasource.SingleConnectionDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="${xxcoop.jdbc.url}" /><property name="username" value="${xxcoop.jdbc.username}" /><property name="password" value="${xxcoop.jdbc.password}" /></bean></beans>
所有单元测试类都继承该类:
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;/** * @author niepeng * * @date 2012-9-4 上午11:08:48 */public class BaseXx extends AbstractTransactionalDataSourceSpringContextTests {@Overrideprotected String[] getConfigLocations() {// 当配置多个dataSource的时候,该行解决:No unique bean of type [javax.sql.DataSource] super.setAutowireMode(AUTOWIRE_BY_NAME);return new String[] {"applicationContext.xml"};}}
单元测试类
public class TestAdminDO extends BaseXx {private AdminDAO adminDAO;public void testCreate() {AdminDO test = new AdminDO();test.setName("name");long id = adminDAO.create(test);Assert.assertTrue(id > 0);}}。。。。
配置了2个DAO类,aDAO 和 bDAO,对应的class是具体实现类
<beans default-autowire="byName"> <bean id="aDAO" class="xxxxxxx.PromotionItemsDAOIbatis"> <!-- 下面sqlmapclient可以不写,因为benas的 default-autowire="byName", 名字相同可以不写 --> <property name="sqlMapClient" ref="sqlMapClient" /> </bean> <bean id="bDAO" class="xxxxxxxxx.CoopShopCodeDAOIbatis" init-method="coop"> <property name="sqlMapClient" ref="sqlMapClientCoop" /> </bean></beans>
sqlMapClient配置:
<beans default-autowire="byName"> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation"><value>classpath:sql-map.xml</value></property><property name="dataSource" ref="dataSource"></property></bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> </bean> <!-- coop库配置 --> <bean id="sqlMapClientCoop" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation"><value>classpath:sql-map.xml</value></property><property name="dataSource" ref="dataSourceCoop"></property></bean> <bean id="transactionManagerCoop" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSourceCoop"/> </bean> </beans>
写了一个BaseCoopIbatsDAO类,所有操作Coop数据库的都继承该类
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;public class BaseCoopIbatisDAO extends SqlMapClientDaoSupport {private DataSource dataSourceCoop;/** * 容器初始化的时候调用 */public void coop() {if (dataSourceCoop != null) {getSqlMapClientTemplate().setDataSource(dataSourceCoop);return;}if (getSqlMapClient().getDataSource() != null) {getSqlMapClientTemplate().setDataSource(getSqlMapClient().getDataSource());}}public void setDataSourceCoop(DataSource dataSourceCoop) {this.dataSourceCoop = dataSourceCoop;}}
具体某个dao实现类
/* * */public class CoopShopCodeDAOIbatis extends BaseCoopIbatisDAO implements CoopShopCodeDAO {@Overridepublic long create(CoopShopCodeDO coopShopCode) {return (Long) this.getSqlMapClientTemplate().insert("CoopShopCodeDAO.create", coopShopCode);}。。。。。
JNDI 实现:
1. 在web.xml中配置resource-ref,多个。
2. 添加jndi数据源
web.xml中配置:
<resource-ref><description>My DataSource Reference</description><res-ref-name>jndi/ds1</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref><resource-ref><description>My DataSource Reference2</description><res-ref-name>jnid/ds2</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref>
jetty-env.xml中配置:
<Configure class="org.eclipse.jetty.webapp.WebAppContext"><New id="ds1" class="org.eclipse.jetty.plus.jndi.Resource"><Arg></Arg><Arg>jdbc/ds1</Arg><Arg><New class="com.mchange.v2.c3p0.ComboPooledDataSource"><Set name="driverClass">com.mysql.jdbc.Driver</Set><Set name="jdbcUrl">${xx1.jdbc.url}</Set><Set name="user">${xx1.jdbc.username}</Set><Set name="password">${xx1.jdbc.password}</Set><Set name="maxPoolSize">20</Set> <Set name="minPoolSize">1</Set> <Set name="initialPoolSize">1</Set> <Set name="maxIdleTime">1800</Set> <Set name="acquireIncrement">1</Set> <Set name="maxStatements">1000</Set> <Set name="acquireRetryAttempts">10</Set> <Set name="idleConnectionTestPeriod">60</Set> <Set name="breakAfterAcquireFailure">true</Set> <Set name="testConnectionOnCheckout">false</Set></New></Arg></New><New id="ds2" class="org.eclipse.jetty.plus.jndi.Resource"><Arg></Arg><Arg>jdbc/ds2</Arg><Arg><New class="com.mchange.v2.c3p0.ComboPooledDataSource"><Set name="driverClass">com.mysql.jdbc.Driver</Set><Set name="jdbcUrl">${xxcoop.jdbc.url}</Set><Set name="user">${xxcoop.jdbc.username}</Set><Set name="password">${xxcoop.jdbc.password}</Set><Set name="maxPoolSize">20</Set> <Set name="minPoolSize">1</Set> <Set name="initialPoolSize">1</Set> <Set name="maxIdleTime">1800</Set> <Set name="acquireIncrement">1</Set> <Set name="maxStatements">1000</Set> <Set name="acquireRetryAttempts">10</Set> <Set name="idleConnectionTestPeriod">60</Set> <Set name="breakAfterAcquireFailure">true</Set> <Set name="testConnectionOnCheckout">false</Set></New></Arg></New></Configure>
内容写的比较粗略,进一步交流:qq:253041869
0 0
- spring+ibatis 配置多个数据源,jdbc 和 jndi 实现
- 配置JNDI JDBC数据源
- 利用spring+ibatiS技术,在spring中配置多个数据源,并实现动态切换。
- 利用spring+ibatiS技术,在spring中配置多个数据源,并实现动态切换。
- ibatis配置多个数据源
- Spring配置JNDI数据源
- spring配置jndi数据源
- JDBC配置JNDI MySQL数据源
- SPRING-JDBC多个数据源
- tomcat配置jndi+spring使用jndi数据源
- tomcat配置jndi+spring使用jndi数据源
- Spring怎样配置JNDI数据源
- 使用spring配置jndi数据源
- Spring中配置JNDI数据源
- spring + tomcat + JNDI 数据源配置
- 通过Spring配置JNDI数据源
- websphere+spring+jndi数据源配置
- Spring数据源配置之jndi
- android开发环境搭建
- 中国大批富人移居海外
- OAUTH协议简介
- ONVIF核心规范中文版链接
- SPI时序详解
- spring+ibatis 配置多个数据源,jdbc 和 jndi 实现
- 坐标转换 搜狗与实际经纬度工具类
- java中String的“==”和equals判断相等性
- JavaWeb_文件上传下载_演示fileupload
- awk用法小结
- SSLVPN3.3.006独立控件安装
- FlexViewer学习资料,希望对初学者有帮助
- 使用Apache的ab工具进行压力测试
- IOS UIButton事件