1、Spring33 整合 Hibernate3 MyBatis32 配置多数据源动态切换数据源 方法Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法一、开篇这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能。所以在出来数据库方言的时候基本上没有什么问题,但唯一可能出现问题的就是在hibernate做添加操作生成主键策略的时候。因为我们都知道hibernate的数据库本地方言会针对不同的数据库采用不同的主键生成策略。所以针对这一问题不
2、得不采用自定义的主键生成策略,自己写一个主键生成器的表来维护主键生成方式或以及使用其他的方式来生成主键,从而避开利用hibernate默认提供的主键生成方式。所以存在问题有:怎样动态的切换数据库方言?这个问题还没有解决,没有更多时间来研究。不过想想应该可以配置两个SessionFactory来实现,那又存在怎么样动态切换SessionFactory呢?!需要解决这个问题才行,而这里则演示怎么样动态切换DataSource数据源的方法。二、代码演示在演示开始之前你的项目已经成功的整合完成的情况下才行,如果你还不知道怎么使用Spring整合MyBatis和Spring整合Hibernate的话。建
3、议参考之前的文章:MyBatis3整合Spring3、SpringMVC3、Struts2、Spring、Hibernate整合ExtJS这两篇文章结合起来就可以完成整合是几大框架了。这里重点介绍动态切换DataSource数据源的方法!1、datasource的配置 applicationContext-datasource.xml org.hibernate.dialect.OracleDialect after_transaction true true update classpath:com/hoo/framework/mybatis/mybatis-common.xml class
4、path:com/hoo/*/resultmap/*-resultmap.xml classpath:com/hoo/*/mapper/*-mapper.xml classpath:com/hoo/*/mapper/*/*-mapper.xml 上面分配配置了Oracle和MySQL数据源,MultipleDataSource为自定义的DataSource,它是继承AbstractRoutingDataSource实现抽象方法即可。2、MultipleDataSource实现AbstractRoutingDataSource抽象数据源中方法,定义CustomerContextHolder来动态
5、切换数据源。代码如下:package com.hoo.framework.spring.support;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;/* * function: Spring 多数据源实现 * author hoojo * createDate 2013-9-27 上午11:24:53 * file MultipleDataSource.java * package com.hoo.framework.spring.support * project SHMB * blo
6、g * email hoojo_ * version 1.0 */public class MultipleDataSource extends AbstractRoutingDataSource Override protected Object determineCurrentLookupKey() return CustomerContextHolder.getCustomerType(); CustomerContextHolderpackage com.hoo.framework.spring.support;/* * function: 多数据源 * author hoojo *
7、createDate 2013-9-27 上午11:36:57 * file CustomerContextHolder.java * package com.hoo.framework.spring.support * project SHMB * blog * email hoojo_ * version 1.0 */public abstract class CustomerContextHolder public final static String DATA_SOURCE_ORACLE = oracleDataSource; public final static String D
8、ATA_SOURCE_MYSQL = mySqlDataSource; private static final ThreadLocal contextHolder = new ThreadLocal(); public static void setCustomerType(String customerType) contextHolder.set(customerType); public static String getCustomerType() return contextHolder.get(); public static void clearCustomerType() c
9、ontextHolder.remove(); 其中,常量对应的applicationContext-datasource.xml中的multipleDataSource中的targetDataSource的key,这个很关键不要搞错了。3、测试看能否切换数据源package com.hoo.framework.service.impl;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.spri
10、ngframework.beans.factory.annotation.Qualifier;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.hoo.framework.dao.BaseDao;import com.hoo.framework.log.ApplicationLogging;import com.hoo.framework.spring.support.CustomerContextHolder;/* * function:多数据源测试服务接口测试 * author hoojo * createDate 2013-10-10 上午11:18:18 * file MultipleDataSourceServiceImplTest.java * package com.hoo.framework.service.impl * project SHMB * blog
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1