From dc417b547abefeba3758247e29b81c204cbb86cb Mon Sep 17 00:00:00 2001 From: wenshao Date: Wed, 5 Dec 2012 18:53:29 +0800 Subject: [PATCH] add ibatis wrapper --- .gitignore | 3 +- .../druid/filter/config/ConfigFilter.java | 3 +- .../druid/support/console/DruidStat.java | 1 - .../druid/support/ibatis/IbatisUtils.java | 129 ++++++++++++ .../SpringIbatisBeanTypeAutoProxyCreator.java | 128 ++++++++++++ ...ngIbatisBeanTypeAutoProxyCreatorMBean.java | 8 + .../ibatis/SqlMapClientImplWrapper.java | 185 +++++++++++++++++ .../support/ibatis/SqlMapClientWrapper.java | 112 +++++++++++ .../support/ibatis/SqlMapExecutorWrapper.java | 186 ++++++++++++++++++ .../support/ibatis/SqlMapSessionWrapper.java | 56 ++++++ .../pool/mybatis/spring-config-mybatis.xml | 4 + 11 files changed, 812 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java create mode 100644 src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java create mode 100644 src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreatorMBean.java create mode 100644 src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java create mode 100644 src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientWrapper.java create mode 100644 src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java create mode 100644 src/main/java/com/alibaba/druid/support/ibatis/SqlMapSessionWrapper.java diff --git a/.gitignore b/.gitignore index ec609c9ea6..ff29a33604 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ out/ *.ipr *.iws -*.iml \ No newline at end of file +*.iml +/bin diff --git a/src/main/java/com/alibaba/druid/filter/config/ConfigFilter.java b/src/main/java/com/alibaba/druid/filter/config/ConfigFilter.java index 6f54d09769..9fa5e095a7 100644 --- a/src/main/java/com/alibaba/druid/filter/config/ConfigFilter.java +++ b/src/main/java/com/alibaba/druid/filter/config/ConfigFilter.java @@ -279,7 +279,8 @@ public class ConfigFilter extends FilterAdapter { } } - private InputStream getFileAsStream(String filePath) throws FileNotFoundException { + @SuppressWarnings("resource") + private InputStream getFileAsStream(String filePath) throws FileNotFoundException { InputStream inStream = null; File file = new File(filePath); if (file.exists()) { diff --git a/src/main/java/com/alibaba/druid/support/console/DruidStat.java b/src/main/java/com/alibaba/druid/support/console/DruidStat.java index 0ea33930b3..8bf4375617 100644 --- a/src/main/java/com/alibaba/druid/support/console/DruidStat.java +++ b/src/main/java/com/alibaba/druid/support/console/DruidStat.java @@ -36,7 +36,6 @@ import com.sun.tools.attach.AgentLoadException; import com.sun.tools.attach.AttachNotSupportedException; import com.sun.tools.attach.VirtualMachine; -@SuppressWarnings({ "restriction" }) public class DruidStat { private static final String LOCAL_CONNECTOR_ADDRESS_PROP = "com.sun.management.jmxremote.localConnectorAddress"; diff --git a/src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java b/src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java new file mode 100644 index 0000000000..db9d23b7b5 --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/ibatis/IbatisUtils.java @@ -0,0 +1,129 @@ +package com.alibaba.druid.support.ibatis; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +import com.alibaba.druid.support.logging.Log; +import com.alibaba.druid.support.logging.LogFactory; +import com.ibatis.sqlmap.client.SqlMapExecutor; +import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl; +import com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl; +import com.ibatis.sqlmap.engine.scope.SessionScope; + +public class IbatisUtils { + private static Log LOG = LogFactory.getLog(IbatisUtils.class); + + private static boolean VERSION_2_3_4 = false; + + private static Method methodGetId = null; + private static Method methodGetResource = null; + + static { + try { + Class clazz = Thread.currentThread().getContextClassLoader().loadClass("com.ibatis.sqlmap.engine.mapping.result.AutoResultMap"); + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if (method.equals("setResultObjectValues")) { // ibatis 2.3.4 add method 'setResultObjectValues' + VERSION_2_3_4 = true; + break; + } + } + } catch (Throwable e) { + // skip + } + } + + public static boolean isVersion2_3_4() { + return VERSION_2_3_4; + } + + public static SqlMapExecutor setClientImpl(SqlMapExecutor session, SqlMapClientImplWrapper clientImplWrapper) { + if (session == null || clientImplWrapper == null) { + return session; + } + + if (session.getClass() == SqlMapSessionImpl.class) { + SqlMapSessionImpl sessionImpl = (SqlMapSessionImpl) session; + set(sessionImpl, clientImplWrapper); + } + + return session; + } + + /** + * 通过反射的方式得到id,能够兼容2.3.0和2.3.4 + * + * @return + */ + protected static String createId(Object statement) { + try { + if (methodGetId == null) { + Class clazz = statement.getClass(); + methodGetId = clazz.getMethod("getId"); + } + + Object returnValue = methodGetId.invoke(statement); + + if (returnValue == null) { + return null; + } + + return returnValue.toString(); + } catch (Exception ex) { + LOG.error("createIdError", ex); + return null; + } + } + + /** + * 通过反射的方式得到resource,能够兼容2.3.0和2.3.4 + * + * @return + */ + protected static String createResource(Object statement) { + try { + if (methodGetResource == null) { + methodGetResource = statement.getClass().getMethod("getResource"); + } + + return (String) methodGetResource.invoke(statement); + } catch (Exception ex) { + return null; + } + } + + private static Field sessionField; + + public static void set(SqlMapSessionImpl session, SqlMapClientImpl client) { + if (sessionField == null) { + for (Field field : SqlMapSessionImpl.class.getDeclaredFields()) { + if (field.getName().equals("session") || field.getName().equals("sessionScope")) { + sessionField = field; + sessionField.setAccessible(true); + break; + } + } + } + + if (sessionField != null) { + SessionScope sessionScope; + try { + sessionScope = (SessionScope) sessionField.get(session); + + if (sessionScope != null) { + if (sessionScope.getSqlMapClient() != null && sessionScope.getSqlMapClient().getClass() == SqlMapClientImpl.class) { + sessionScope.setSqlMapClient(client); + } + if (sessionScope.getSqlMapExecutor() != null && sessionScope.getSqlMapExecutor().getClass() == SqlMapClientImpl.class) { + sessionScope.setSqlMapExecutor(client); + } + if (sessionScope.getSqlMapTxMgr() != null && sessionScope.getSqlMapTxMgr().getClass() == SqlMapClientImpl.class) { + sessionScope.setSqlMapTxMgr(client); + } + } + } catch (Exception e) { + LOG.error(e.getMessage(), e); + } + } + } +} diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java b/src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java new file mode 100644 index 0000000000..b09458a32c --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreator.java @@ -0,0 +1,128 @@ +package com.alibaba.druid.support.ibatis; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.aop.TargetSource; +import org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.util.Assert; +import org.springframework.util.PatternMatchUtils; + +import com.ibatis.sqlmap.client.SqlMapClient; +import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; + +/** + * 类BeanTypeAutoProxyCreator.java的实现描述:使用配置类型代替Springframework中配置名称的实现 + * + * @author hualiang.lihl 2011-12-31 上午10:48:20 + */ +@SuppressWarnings("deprecation") +public class SpringIbatisBeanTypeAutoProxyCreator extends AbstractAutoProxyCreator implements InitializingBean, ApplicationContextAware, SpringIbatisBeanTypeAutoProxyCreatorMBean { + + private final static Log LOG = LogFactory.getLog(SpringIbatisBeanTypeAutoProxyCreator.class); + + private static final long serialVersionUID = -9094985530794052264L; + + private Class targetBeanType = SqlMapClient.class; + + private ApplicationContext context; + + private List beanNames = new ArrayList(); + private final List proxyBeanNames = new ArrayList(); + + /** + * @param targetClass the targetClass to set + */ + public void setTargetBeanType(Class targetClass) { + this.targetBeanType = targetClass; + } + + public void setApplicationContext(ApplicationContext context) throws BeansException { + this.context = context; + } + + /** + * Identify as bean to proxy if the bean name is in the configured list of names. + */ + @SuppressWarnings("rawtypes") + protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, TargetSource targetSource) { + for (Iterator it = this.beanNames.iterator(); it.hasNext();) { + String mappedName = (String) it.next(); + if (FactoryBean.class.isAssignableFrom(beanClass)) { + if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) { + continue; + } + mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()); + } + if (isMatch(beanName, mappedName)) { + return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS; + } + } + return DO_NOT_PROXY; + } + + @SuppressWarnings({ "rawtypes" }) + protected Object createProxy(Class beanClass, String beanName, Object[] specificInterceptors, + TargetSource targetSource) { + try { + Object target = targetSource.getTarget(); + + if (target instanceof SqlMapClientWrapper) { + proxyBeanNames.add(beanName); + return target; + } + + if (target instanceof SqlMapClient) { + proxyBeanNames.add(beanName); + + return new SqlMapClientWrapper((ExtendedSqlMapClient) target); + } + + return super.createProxy(beanClass, beanName, specificInterceptors, targetSource); + } catch (Throwable ex) { + LOG.error(ex.getMessage(), ex); + return super.createProxy(beanClass, beanName, specificInterceptors, targetSource); + } + } + + /** + * Return if the given bean name matches the mapped name. + *

+ * The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches, as well as direct equality. Can be + * overridden in subclasses. + * + * @param beanName the bean name to check + * @param mappedName the name in the configured list of names + * @return if the names match + * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String) + */ + protected boolean isMatch(String beanName, String mappedName) { + return PatternMatchUtils.simpleMatch(mappedName, beanName); + } + + public void afterPropertiesSet() throws Exception { + Assert.notNull(targetBeanType, "targetType cannot be null"); + String[] beanNames = context.getBeanNamesForType(targetBeanType); + for (String name : beanNames) { + this.beanNames.add(name); + } + } + + public List getBeanNames() { + return beanNames; + } + + public List getProxyBeanNames() { + return proxyBeanNames; + } + +} diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreatorMBean.java b/src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreatorMBean.java new file mode 100644 index 0000000000..6a23d7309d --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/ibatis/SpringIbatisBeanTypeAutoProxyCreatorMBean.java @@ -0,0 +1,8 @@ +package com.alibaba.druid.support.ibatis; + +import java.util.List; + +public interface SpringIbatisBeanTypeAutoProxyCreatorMBean { + + List getProxyBeanNames(); +} diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java new file mode 100644 index 0000000000..a5996ee07b --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientImplWrapper.java @@ -0,0 +1,185 @@ +package com.alibaba.druid.support.ibatis; + +import java.lang.reflect.Method; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +import com.ibatis.common.util.PaginatedList; +import com.ibatis.sqlmap.client.SqlMapSession; +import com.ibatis.sqlmap.client.event.RowHandler; +import com.ibatis.sqlmap.engine.execution.BatchException; +import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl; +import com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl; + +@SuppressWarnings("deprecation") +public class SqlMapClientImplWrapper extends SqlMapClientImpl { + + private SqlMapClientImpl raw; + + private static Method getLocalSqlMapSessionMethod = null; + + public SqlMapClientImplWrapper(SqlMapClientImpl raw){ + super(raw.getDelegate()); + this.raw = raw; + } + + protected SqlMapSessionWrapper getLocalSqlMapSessionWrapper() { + try { + if (getLocalSqlMapSessionMethod == null) { + getLocalSqlMapSessionMethod = raw.getClass().getDeclaredMethod("getLocalSqlMapSession"); + getLocalSqlMapSessionMethod.setAccessible(true); + } + SqlMapSessionImpl sessionImpl = (SqlMapSessionImpl) getLocalSqlMapSessionMethod.invoke(raw); + IbatisUtils.set(sessionImpl, this); + return new SqlMapSessionWrapper(raw, sessionImpl); + } catch (Exception e) { + throw new IllegalStateException(e.getMessage(), e); + } + } + + public SqlMapSession openSession(Connection conn) { + SqlMapSession session = raw.openSession(conn); + IbatisUtils.setClientImpl(session, this); + return new SqlMapSessionWrapper(raw, session); + } + + public SqlMapSession getSession() { + SqlMapSession session = raw.getSession(); + IbatisUtils.setClientImpl(session, this); + return new SqlMapSessionWrapper(raw, session); + } + + // ///// + public Object insert(String id, Object param) throws SQLException { + return getLocalSqlMapSessionWrapper().insert(id, param); + } + + public Object insert(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().insert(id); + } + + public int update(String id, Object param) throws SQLException { + return getLocalSqlMapSessionWrapper().update(id, param); + } + + public int update(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().update(id); + } + + public int delete(String id, Object param) throws SQLException { + return getLocalSqlMapSessionWrapper().delete(id, param); + } + + public int delete(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().delete(id); + } + + public Object queryForObject(String id, Object paramObject) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject); + } + + public Object queryForObject(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForObject(id); + } + + public Object queryForObject(String id, Object paramObject, Object resultObject) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForObject(id, paramObject, resultObject); + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id, Object paramObject) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id, paramObject); + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id); + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id, Object paramObject, int skip, int max) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id, paramObject, skip, max); + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id, int skip, int max) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForList(id, skip, max); + } + + /** + * @deprecated All paginated list features have been deprecated + */ + public PaginatedList queryForPaginatedList(String id, Object paramObject, int pageSize) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForPaginatedList(id, paramObject, pageSize); + } + + /** + * @deprecated All paginated list features have been deprecated + */ + public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForPaginatedList(id, pageSize); + } + + @SuppressWarnings("rawtypes") + public Map queryForMap(String id, Object paramObject, String keyProp) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject, keyProp); + } + + @SuppressWarnings("rawtypes") + public Map queryForMap(String id, Object paramObject, String keyProp, String valueProp) throws SQLException { + return getLocalSqlMapSessionWrapper().queryForMap(id, paramObject, keyProp, valueProp); + } + + public void queryWithRowHandler(String id, Object paramObject, RowHandler rowHandler) throws SQLException { + getLocalSqlMapSessionWrapper().queryWithRowHandler(id, paramObject, rowHandler); + } + + public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException { + getLocalSqlMapSessionWrapper().queryWithRowHandler(id, rowHandler); + } + + public void startTransaction() throws SQLException { + getLocalSqlMapSessionWrapper().startTransaction(); + } + + public void startTransaction(int transactionIsolation) throws SQLException { + getLocalSqlMapSessionWrapper().startTransaction(transactionIsolation); + } + + public void commitTransaction() throws SQLException { + getLocalSqlMapSessionWrapper().commitTransaction(); + } + + public void endTransaction() throws SQLException { + try { + getLocalSqlMapSessionWrapper().endTransaction(); + } finally { + getLocalSqlMapSessionWrapper().close(); + } + } + + public void startBatch() throws SQLException { + getLocalSqlMapSessionWrapper().startBatch(); + } + + public int executeBatch() throws SQLException { + return getLocalSqlMapSessionWrapper().executeBatch(); + } + + @SuppressWarnings("rawtypes") + public List executeBatchDetailed() throws SQLException, BatchException { + return getLocalSqlMapSessionWrapper().executeBatchDetailed(); + } + + public void setUserConnection(Connection connection) throws SQLException { + try { + getLocalSqlMapSessionWrapper().setUserConnection(connection); + } finally { + if (connection == null) { + getLocalSqlMapSessionWrapper().close(); + } + } + } +} diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientWrapper.java b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientWrapper.java new file mode 100644 index 0000000000..21000f2abe --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapClientWrapper.java @@ -0,0 +1,112 @@ +package com.alibaba.druid.support.ibatis; + +import java.sql.Connection; +import java.sql.SQLException; + +import javax.sql.DataSource; + +import com.ibatis.sqlmap.client.SqlMapClient; +import com.ibatis.sqlmap.client.SqlMapSession; +import com.ibatis.sqlmap.engine.execution.SqlExecutor; +import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; +import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate; +import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory; +import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement; + +@SuppressWarnings("deprecation") +public class SqlMapClientWrapper extends SqlMapExecutorWrapper implements SqlMapClient, ExtendedSqlMapClient { + + protected final ExtendedSqlMapClient client; + + public SqlMapClientWrapper(ExtendedSqlMapClient client){ + super(client, client); + this.client = client; + + } + + public ExtendedSqlMapClient getClient() { + return this.client; + } + + public void startTransaction() throws SQLException { + client.startTransaction(); + } + + public void startTransaction(int transactionIsolation) throws SQLException { + client.startTransaction(transactionIsolation); + } + + public void commitTransaction() throws SQLException { + client.commitTransaction(); + } + + public void endTransaction() throws SQLException { + client.endTransaction(); + } + + public void setUserConnection(Connection connnection) throws SQLException { + client.setUserConnection(connnection); + } + + public Connection getUserConnection() throws SQLException { + return client.getUserConnection(); + } + + public Connection getCurrentConnection() throws SQLException { + return client.getCurrentConnection(); + } + + public DataSource getDataSource() { + return client.getDataSource(); + } + + public SqlMapSession openSession() { + SqlMapSession session = client.openSession(); + IbatisUtils.setClientImpl(session, clientImplWrapper); + return new SqlMapSessionWrapper(client, session); + } + + public SqlMapSession openSession(Connection conn) { + SqlMapSession session = client.openSession(conn); + IbatisUtils.setClientImpl(session, clientImplWrapper); + return new SqlMapSessionWrapper(client, session); + } + + public SqlMapSession getSession() { + SqlMapSession session = client.getSession(); + IbatisUtils.setClientImpl(session, clientImplWrapper); + return new SqlMapSessionWrapper(client, session); + } + + public void flushDataCache() { + client.flushDataCache(); + } + + public void flushDataCache(String cacheId) { + client.flushDataCache(cacheId); + } + + public MappedStatement getMappedStatement(String id) { + return client.getMappedStatement(id); + } + + public boolean isLazyLoadingEnabled() { + return client.isLazyLoadingEnabled(); + } + + public boolean isEnhancementEnabled() { + return client.isEnhancementEnabled(); + } + + public SqlExecutor getSqlExecutor() { + return client.getSqlExecutor(); + } + + public SqlMapExecutorDelegate getDelegate() { + return client.getDelegate(); + } + + public ResultObjectFactory getResultObjectFactory() { + return client.getResultObjectFactory(); + } +} diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java new file mode 100644 index 0000000000..f185c17747 --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapExecutorWrapper.java @@ -0,0 +1,186 @@ +package com.alibaba.druid.support.ibatis; + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +import com.ibatis.common.util.PaginatedList; +import com.ibatis.sqlmap.client.SqlMapExecutor; +import com.ibatis.sqlmap.client.event.RowHandler; +import com.ibatis.sqlmap.engine.execution.BatchException; +import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; +import com.ibatis.sqlmap.engine.impl.SqlMapClientImpl; + +@SuppressWarnings("deprecation") +public class SqlMapExecutorWrapper implements SqlMapExecutor { + + private SqlMapExecutor executor; + + protected final SqlMapClientImpl clientImpl; + protected final SqlMapClientImplWrapper clientImplWrapper; + + public SqlMapExecutorWrapper(ExtendedSqlMapClient client, SqlMapExecutor executor){ + this.executor = executor; + + this.clientImpl = client.getClass() == SqlMapClientImpl.class ? (SqlMapClientImpl) client : null; + this.clientImplWrapper = clientImpl != null ? new SqlMapClientImplWrapper(clientImpl) : null; + } + + public Object insert(String id, Object parameterObject) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + return executor.insert(id, parameterObject); + } + + public Object insert(String id) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + return executor.insert(id); + } + + public int update(String id, Object parameterObject) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + int effectedRowCount = executor.update(id, parameterObject); + return effectedRowCount; + } + + public int update(String id) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + int effectedRowCount = executor.update(id); + + return effectedRowCount; + } + + public int delete(String id, Object parameterObject) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + int effectedRowCount = executor.delete(id, parameterObject); + + return effectedRowCount; + } + + public int delete(String id) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + int effectedRowCount = executor.delete(id); + + return effectedRowCount; + } + + public Object queryForObject(String id, Object parameterObject) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + Object object = executor.queryForObject(id, parameterObject); + + return object; + } + + public Object queryForObject(String id) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + Object object = executor.queryForObject(id); + + return object; + } + + public Object queryForObject(String id, Object parameterObject, Object resultObject) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + Object object = executor.queryForObject(id, parameterObject, resultObject); + + return object; + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id, Object parameterObject) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + List list = executor.queryForList(id, parameterObject); + + return list; + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + List list = executor.queryForList(id); + + return list; + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id, Object parameterObject, int skip, int max) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + List list = executor.queryForList(id, parameterObject, skip, max); + + return list; + } + + @SuppressWarnings("rawtypes") + public List queryForList(String id, int skip, int max) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + List list = executor.queryForList(id, skip, max); + + return list; + } + + public void queryWithRowHandler(String id, Object parameterObject, RowHandler rowHandler) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + executor.queryWithRowHandler(id, parameterObject, rowHandler); + } + + public void queryWithRowHandler(String id, RowHandler rowHandler) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + executor.queryWithRowHandler(id, rowHandler); + } + + public PaginatedList queryForPaginatedList(String id, Object parameterObject, int pageSize) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + PaginatedList list = executor.queryForPaginatedList(id, parameterObject, pageSize); + + return list; + } + + public PaginatedList queryForPaginatedList(String id, int pageSize) throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + PaginatedList list = executor.queryForPaginatedList(id, pageSize); + + return list; + } + + @SuppressWarnings("rawtypes") + public Map queryForMap(String id, Object parameterObject, String keyProp) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + Map map = executor.queryForMap(id, parameterObject, keyProp); + + return map; + } + + @SuppressWarnings("rawtypes") + public Map queryForMap(String id, Object parameterObject, String keyProp, String valueProp) throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + Map map = executor.queryForMap(id, parameterObject, keyProp, valueProp); + + return map; + } + + public void startBatch() throws SQLException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + executor.startBatch(); + } + + public int executeBatch() throws SQLException { + + IbatisUtils.setClientImpl(executor, clientImplWrapper); + return executor.executeBatch(); + } + + @SuppressWarnings("rawtypes") + public List executeBatchDetailed() throws SQLException, BatchException { + IbatisUtils.setClientImpl(executor, clientImplWrapper); + return executor.executeBatchDetailed(); + } + +} diff --git a/src/main/java/com/alibaba/druid/support/ibatis/SqlMapSessionWrapper.java b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapSessionWrapper.java new file mode 100644 index 0000000000..d41e465716 --- /dev/null +++ b/src/main/java/com/alibaba/druid/support/ibatis/SqlMapSessionWrapper.java @@ -0,0 +1,56 @@ +package com.alibaba.druid.support.ibatis; + +import java.sql.Connection; +import java.sql.SQLException; + +import javax.sql.DataSource; + +import com.ibatis.sqlmap.client.SqlMapSession; +import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; + +@SuppressWarnings("deprecation") +public class SqlMapSessionWrapper extends SqlMapExecutorWrapper implements SqlMapSession { + private SqlMapSession session; + + public SqlMapSessionWrapper(ExtendedSqlMapClient client, SqlMapSession session){ + super(client, session); + this.session = session; + } + + public void startTransaction() throws SQLException { + session.startTransaction(); + } + + public void startTransaction(int transactionIsolation) throws SQLException { + session.startTransaction(transactionIsolation); + } + + public void commitTransaction() throws SQLException { + session.commitTransaction(); + } + + public void endTransaction() throws SQLException { + session.endTransaction(); + } + + public void setUserConnection(Connection connnection) throws SQLException { + session.setUserConnection(connnection); + } + + @Deprecated + public Connection getUserConnection() throws SQLException { + return session.getUserConnection(); + } + + public Connection getCurrentConnection() throws SQLException { + return session.getCurrentConnection(); + } + + public DataSource getDataSource() { + return session.getDataSource(); + } + + public void close() { + session.close(); + } +} diff --git a/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml b/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml index bc802baddd..efcab2a65d 100644 --- a/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml +++ b/src/test/resources/com/alibaba/druid/pool/mybatis/spring-config-mybatis.xml @@ -18,5 +18,9 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/ + + + +