Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory.docx
- 文档编号:5596637
- 上传时间:2022-12-28
- 格式:DOCX
- 页数:24
- 大小:290.99KB
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory.docx
《Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory.docx》由会员分享,可在线阅读,更多相关《Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory.docx(24页珍藏版)》请在冰豆网上搜索。
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory
一、摘要
上两篇文章分别介绍了Spring3.3整合Hibernate3、MyBatis3.2配置多数据源/动态切换数据源方法和Spring3整合Hibernate3.5动态切换SessionFactory(切换数据库方言),这篇文章将介绍Spring整合Mybatis如何完成SqlSessionFactory的动态切换的。
并且会简单的介绍下MyBatis整合Spring中的官方的相关代码。
Spring整合MyBatis切换SqlSessionFactory有两种方法,第一、继承SqlSessionDaoSupport,重写获取SqlSessionFactory的方法。
第二、继承SqlSessionTemplate重写getSqlSessionFactory、getConfiguration和SqlSessionInterceptor这个拦截器。
其中最为关键还是继承SqlSessionTemplate并重写里面的方法。
而Spring整合MyBatis也有两种方式,一种是配置MapperFactoryBean,另一种则是利用MapperScannerConfigurer进行扫描接口或包完成对象的自动创建。
相对来说后者更方便些。
MapperFactoryBean继承了SqlSessionDaoSupport也就是动态切换SqlSessionFactory的第一种方法,我们需要重写和实现SqlSessionDaoSupport方法,或者是继承MapperFactoryBean来重写覆盖相关方法。
如果利用MapperScannerConfigurer的配置整合来切换SqlSessionFactory,那么我们就需要继承SqlSessionTemplate,重写上面提到的方法。
在整合的配置中很多地方都是可以注入SqlSessionTemplate代替SqlSessionFactory的注入的。
因为SqlSessionTemplate的创建也是需要注入SqlSessionFactory的。
二、实现代码
1、继承SqlSessionTemplate重写getSqlSessionFactory、getConfiguration和SqlSessionInterceptor
packagecom.hoo.framework.mybatis.support;
importstaticjava.lang.reflect.Proxy.newProxyInstance;
importstaticorg.apache.ibatis.reflection.ExceptionUtil.unwrapThrowable;
importstaticorg.mybatis.spring.SqlSessionUtils.closeSqlSession;
importstaticorg.mybatis.spring.SqlSessionUtils.getSqlSession;
importstaticorg.mybatis.spring.SqlSessionUtils.isSqlSessionTransactional;
importjava.lang.reflect.InvocationHandler;
importjava.lang.reflect.Method;
importjava.sql.Connection;
importjava.util.List;
importjava.util.Map;
importorg.apache.ibatis.exceptions.PersistenceException;
importorg.apache.ibatis.executor.BatchResult;
importorg.apache.ibatis.session.Configuration;
importorg.apache.ibatis.session.ExecutorType;
importorg.apache.ibatis.session.ResultHandler;
importorg.apache.ibatis.session.RowBounds;
importorg.apache.ibatis.session.SqlSession;
importorg.apache.ibatis.session.SqlSessionFactory;
importorg.mybatis.spring.MyBatisExceptionTranslator;
importorg.mybatis.spring.SqlSessionTemplate;
importorg.springframework.dao.support.PersistenceExceptionTranslator;
importorg.springframework.util.Assert;
/**
*function:
继承SqlSessionTemplate重写相关方法
*@authorhoojo
*@createDate2013-10-18下午03:
07:
46
*@fileCustomSqlSessionTemplate.java
*@packagecom.hoo.framework.mybatis.support
*@projectSHMB
*@blogbbb:
//
*@emailhoojo_@126aaa
*@version1.0
*/
publicclassCustomSqlSessionTemplateextendsSqlSessionTemplate{
privatefinalSqlSessionFactorysqlSessionFactory;
privatefinalExecutorTypeexecutorType;
privatefinalSqlSessionsqlSessionProxy;
privatefinalPersistenceExceptionTranslatorexceptionTranslator;
privateMap
privateSqlSessionFactorydefaultTargetSqlSessionFactory;
publicvoidsetTargetSqlSessionFactorys(Map
this.targetSqlSessionFactorys=targetSqlSessionFactorys;
}
publicvoidsetDefaultTargetSqlSessionFactory(SqlSessionFactorydefaultTargetSqlSessionFactory){
this.defaultTargetSqlSessionFactory=defaultTargetSqlSessionFactory;
}
publicCustomSqlSessionTemplate(SqlSessionFactorysqlSessionFactory){
this(sqlSessionFactory,sqlSessionFactory.getConfiguration().getDefaultExecutorType());
}
publicCustomSqlSessionTemplate(SqlSessionFactorysqlSessionFactory,ExecutorTypeexecutorType){
this(sqlSessionFactory,executorType,newMyBatisExceptionTranslator(sqlSessionFactory.getConfiguration()
.getEnvironment().getDataSource(),true));
}
publicCustomSqlSessionTemplate(SqlSessionFactorysqlSessionFactory,ExecutorTypeexecutorType,
PersistenceExceptionTranslatorexceptionTranslator){
super(sqlSessionFactory,executorType,exceptionTranslator);
this.sqlSessionFactory=sqlSessionFactory;
this.executorType=executorType;
this.exceptionTranslator=exceptionTranslator;
this.sqlSessionProxy=(SqlSession)newProxyInstance(
SqlSessionFactory.class.getClassLoader(),
newClass[]{SqlSession.class},
newSqlSessionInterceptor());
this.defaultTargetSqlSessionFactory=sqlSessionFactory;
}
@Override
publicSqlSessionFactorygetSqlSessionFactory(){
SqlSessionFactorytargetSqlSessionFactory=targetSqlSessionFactorys.get(CustomerContextHolder.getContextType());
if(targetSqlSessionFactory!
=null){
returntargetSqlSessionFactory;
}elseif(defaultTargetSqlSessionFactory!
=null){
returndefaultTargetSqlSessionFactory;
}else{
Assert.notNull(targetSqlSessionFactorys,"Property'targetSqlSessionFactorys'or'defaultTargetSqlSessionFactory'arerequired");
Assert.notNull(defaultTargetSqlSessionFactory,"Property'defaultTargetSqlSessionFactory'or'targetSqlSessionFactorys'arerequired");
}
returnthis.sqlSessionFactory;
}
@Override
publicConfigurationgetConfiguration(){
returnthis.getSqlSessionFactory().getConfiguration();
}
publicExecutorTypegetExecutorType(){
returnthis.executorType;
}
publicPersistenceExceptionTranslatorgetPersistenceExceptionTranslator(){
returnthis.exceptionTranslator;
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
public
returnthis.sqlSessionProxy.
}
/**
*{@inheritDoc}
*/
publicvoidselect(Stringstatement,ResultHandlerhandler){
this.sqlSessionProxy.select(statement,handler);
}
/**
*{@inheritDoc}
*/
publicvoidselect(Stringstatement,Objectparameter,ResultHandlerhandler){
this.sqlSessionProxy.select(statement,parameter,handler);
}
/**
*{@inheritDoc}
*/
publicvoidselect(Stringstatement,Objectparameter,RowBoundsrowBounds,ResultHandlerhandler){
this.sqlSessionProxy.select(statement,parameter,rowBounds,handler);
}
/**
*{@inheritDoc}
*/
publicintinsert(Stringstatement){
returnthis.sqlSessionProxy.insert(statement);
}
/**
*{@inheritDoc}
*/
publicintinsert(Stringstatement,Objectparameter){
returnthis.sqlSessionProxy.insert(statement,parameter);
}
/**
*{@inheritDoc}
*/
publicintupdate(Stringstatement){
returnthis.sqlSessionProxy.update(statement);
}
/**
*{@inheritDoc}
*/
publicintupdate(Stringstatement,Objectparameter){
returnthis.sqlSessionProxy.update(statement,parameter);
}
/**
*{@inheritDoc}
*/
publicintdelete(Stringstatement){
returnthis.sqlSessionProxy.delete(statement);
}
/**
*{@inheritDoc}
*/
publicintdelete(Stringstatement,Objectparameter){
returnthis.sqlSessionProxy.delete(statement,parameter);
}
/**
*{@inheritDoc}
*/
public
returngetConfiguration().getMapper(type,this);
}
/**
*{@inheritDoc}
*/
publicvoidcommit(){
thrownewUnsupportedOperationException("ManualcommitisnotallowedoveraSpringmanagedSqlSession");
}
/**
*{@inheritDoc}
*/
publicvoidcommit(booleanforce){
thrownewUnsupportedOperationException("ManualcommitisnotallowedoveraSpringmanagedSqlSession");
}
/**
*{@inheritDoc}
*/
publicvoidrollback(){
thrownewUnsupportedOperationException("ManualrollbackisnotallowedoveraSpringmanagedSqlSession");
}
/**
*{@inheritDoc}
*/
publicvoidrollback(booleanforce){
thrownewUnsupportedOperationException("ManualrollbackisnotallowedoveraSpringmanagedSqlSession");
}
/**
*{@inheritDoc}
*/
publicvoidclose(){
thrownewUnsupportedOperationException("ManualcloseisnotallowedoveraSpringmanagedSqlSession");
}
/**
*{@inheritDoc}
*/
publicvoidclearCache(){
this.sqlSessionProxy.clearCache();
}
/**
*{@inheritDoc}
*/
publicConnectiongetConnection(){
returnthis.sqlSessionProxy.getConnection();
}
/**
*{@inheritDoc}
*@since1.0.2
*/
publicList
returnthis.sqlSessionProxy.flushStatements();
}
/**
*ProxyneededtorouteMyBatismethodcallstotheproperSqlSessiongotfromSpring'sTransactionManagerItalso
*unwrapsexceptionsthrownby{@codeMethod#invoke(Object,Object...)}topassa{@codePersistenceException}to
*the{@codePersistenceExceptionTranslator}.
*/
privateclassSqlSessionInterceptorimplementsInvocationHandler{
publicObjectinvoke(Objectproxy,Methodmethod,Object[]args)throwsThrowable{
finalSqlSessionsqlSession=getSqlSession(
CustomSqlSessionTemplate.this.getSqlSessionFactory(),
CustomSqlSessionTemplate.this.executorType,
CustomSqlSessionTemplate.this.exception
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Spring3 整合 MyBatis3 配置 多数 动态 选择 SqlSessionFactory