Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx

上传人:b****5 文档编号:18583754 上传时间:2022-12-28 格式:DOCX 页数:24 大小:290.99KB
下载 相关 举报
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx_第1页
第1页 / 共24页
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx_第2页
第2页 / 共24页
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx_第3页
第3页 / 共24页
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx_第4页
第4页 / 共24页
Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx

《Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx》由会员分享,可在线阅读,更多相关《Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx(24页珍藏版)》请在冰豆网上搜索。

Spring3整合MyBatis3配置多数据源动态选择SqlSessionFactory文档格式.docx

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;

/**

*<

b>

function:

<

/b>

继承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<

Object,SqlSessionFactory>

targetSqlSessionFactorys;

privateSqlSessionFactorydefaultTargetSqlSessionFactory;

publicvoidsetTargetSqlSessionFactorys(Map<

targetSqlSessionFactorys){

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!

returndefaultTargetSqlSessionFactory;

}else{

Assert.notNull(targetSqlSessionFactorys,"

Property'

targetSqlSessionFactorys'

or'

defaultTargetSqlSessionFactory'

arerequired"

);

Assert.notNull(defaultTargetSqlSessionFactory,"

returnthis.sqlSessionFactory;

publicConfigurationgetConfiguration(){

returnthis.getSqlSessionFactory().getConfiguration();

publicExecutorTypegetExecutorType(){

returnthis.executorType;

publicPersistenceExceptionTranslatorgetPersistenceExceptionTranslator(){

returnthis.exceptionTranslator;

/**

*{@inheritDoc}

*/

public<

T>

TselectOne(Stringstatement){

returnthis.sqlSessionProxy.<

selectOne(statement);

}

TselectOne(Stringstatement,Objectparameter){

selectOne(statement,parameter);

K,V>

Map<

selectMap(Stringstatement,StringmapKey){

selectMap(statement,mapKey);

selectMap(Stringstatement,Objectparameter,StringmapKey){

selectMap(statement,parameter,mapKey);

selectMap(Stringstatement,Objectparameter,StringmapKey,RowBoundsrowBounds){

selectMap(statement,parameter,mapKey,rowBounds);

E>

List<

selectList(Stringstatement){

selectList(statement);

*/

selectList(Stringstatement,Objectparameter){

selectList(statement,parameter);

selectList(Stringstatement,Objectparameter,RowBoundsrowBounds){

selectList(statement,parameter,rowBounds);

publicvoidselect(Stringstatement,ResultHandlerhandler){

this.sqlSessionProxy.select(statement,handler);

publicvoidselect(Stringstatement,Objectparameter,ResultHandlerhandler){

this.sqlSessionProxy.select(statement,parameter,handler);

publicvoidselect(Stringstatement,Objectparameter,RowBoundsrowBounds,ResultHandlerhandler){

this.sqlSessionProxy.select(statement,parameter,rowBounds,handler);

publicintinsert(Stringstatement){

returnthis.sqlSessionProxy.insert(statement);

publicintinsert(Stringstatement,Objectparameter){

returnthis.sqlSessionProxy.insert(statement,parameter);

publicintupdate(Stringstatement){

returnthis.sqlSessionProxy.update(statement);

publicintupdate(Stringstatement,Objectparameter){

returnthis.sqlSessionProxy.update(statement,parameter);

publicintdelete(Stringstatement){

returnthis.sqlSessionProxy.delete(statement);

publicintdelete(Stringstatement,Objectparameter){

returnthis.sqlSessionProxy.delete(statement,parameter);

TgetMapper(Class<

type){

returngetConfiguration().getMapper(type,this);

publicvoidcommit(){

thrownewUnsupportedOperationException("

ManualcommitisnotallowedoveraSpringmanagedSqlSession"

publicvoidcommit(booleanforce){

publicvoidrollback(){

ManualrollbackisnotallowedoveraSpringmanagedSqlSession"

publicvoidrollback(booleanforce){

publicvoidclose(){

ManualcloseisnotallowedoveraSpringmanagedSqlSession"

publicvoidclearCache(){

this.sqlSessionProxy.clearCache();

/**

publicConnectiongetConnection(){

returnthis.sqlSessionProxy.getConnection();

*@since1.0.2

publicList<

BatchResult>

flushStatements(){

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 企业管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1