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