<resultMapid="blogResult"type="Blog"><associationproperty="author"column="author_id"javaType="Author"select="selectAuthor"/></resultMap><selectid="selectBlog"resultMap="blogResult"> SELECT * FROM BLOG WHERE ID = #{id}
</select><selectid="selectAuthor"resultType="Author"> SELECT * FROM AUTHOR WHERE ID = #{id}
</select>
importcom.mchange.v2.c3p0.ComboPooledDataSource;importcom.mchange.v2.c3p0.impl.NewProxyConnection;importcom.foooooo.common.util.SpringBeanFactoryUtils;importorg.apache.ibatis.executor.statement.StatementHandler;importorg.apache.ibatis.plugin.*;importorg.apache.ibatis.reflection.MetaObject;importorg.apache.ibatis.reflection.factory.DefaultObjectFactory;importorg.apache.ibatis.reflection.factory.ObjectFactory;importorg.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;importorg.apache.ibatis.reflection.wrapper.ObjectWrapperFactory;importorg.apache.ibatis.session.ResultHandler;importjava.lang.reflect.Field;importjava.sql.Connection;importjava.sql.Statement;importjava.util.Properties;/** * Created by SargerasWang on 15/4/9. */@Intercepts({@Signature(type=StatementHandler.class,method="query",args={Statement.class,ResultHandler.class})})publicclassDynamicDatasourcePluginimplementsInterceptor{privatestaticfinalObjectFactoryDEFAULT_OBJECT_FACTORY=newDefaultObjectFactory();privatestaticfinalObjectWrapperFactoryDEFAULT_OBJECT_WRAPPER_FACTORY=newDefaultObjectWrapperFactory();@OverridepublicObjectintercept(Invocationinvocation)throwsThrowable{Statementstatement=(Statement)invocation.getArgs()[0];StatementHandlerstatementHandler=(StatementHandler)invocation.getTarget();MetaObjectmetaStatement=getRealObj(statement);MetaObjectmetaStatementHandler=getRealObj(statementHandler);Stringindex=(String)metaStatementHandler.getValue("delegate.mappedStatement.id");if(index.startsWith("_formatter_")){StringdataSourceBeanName=index.substring("_formatter_".length()+1,index.indexOf("!"));ComboPooledDataSourcedataSource=(ComboPooledDataSource)SpringBeanFactoryUtils.getBean("ds_"+dataSourceBeanName);NewProxyConnectionconnection=(NewProxyConnection)dataSource.getConnection();Fieldinner=connection.getClass().getDeclaredField("inner");inner.setAccessible(true);metaStatement.setValue("inner.connection",inner.get(connection));metaStatement.setValue("inner.currentCatalog",dataSourceBeanName);Objectresult=invocation.proceed();connection.close();//这里关闭连接returnresult;}returninvocation.proceed();}privateMetaObjectgetRealObj(Objectobj){MetaObjectmetaStatement=MetaObject.forObject(obj,DEFAULT_OBJECT_FACTORY,DEFAULT_OBJECT_WRAPPER_FACTORY);// 分离代理对象链(由于目标类可能被多个拦截器拦截,从而形成多次代理,通过下面的两次循环可以分离出最原始的的目标类)while(metaStatement.hasGetter("h")){Objectobject=metaStatement.getValue("h");metaStatement=MetaObject.forObject(object,DEFAULT_OBJECT_FACTORY,DEFAULT_OBJECT_WRAPPER_FACTORY);}// 分离最后一个代理对象的目标类while(metaStatement.hasGetter("target")){Objectobject=metaStatement.getValue("target");metaStatement=MetaObject.forObject(object,DEFAULT_OBJECT_FACTORY,DEFAULT_OBJECT_WRAPPER_FACTORY);}returnmetaStatement;}@OverridepublicObjectplugin(Objecttarget){returnPlugin.wrap(target,this);}@OverridepublicvoidsetProperties(Propertiesproperties){}}