diff --git a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/MybatisFlexAutoConfiguration.java b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/MybatisFlexAutoConfiguration.java index ab1cac78..363e8351 100644 --- a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/MybatisFlexAutoConfiguration.java +++ b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/MybatisFlexAutoConfiguration.java @@ -6,8 +6,8 @@ import com.mybatisflex.core.MybatisFlexBootstrap; import com.mybatisflex.core.mybatis.FlexConfiguration; import com.mybatisflex.core.mybatis.FlexSqlSessionFactoryBuilder; import com.mybatisflex.core.row.RowMapperInvoker; -import com.mybatisflex.solon.transaction.MybatisMapperInterceptor; import com.mybatisflex.solon.transaction.SolonManagedTransactionFactory; +import com.mybatisflex.solon.transaction.MybatisSessionTemplate; import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.executor.ErrorContext; import org.apache.ibatis.io.Resources; @@ -171,13 +171,10 @@ public class MybatisFlexAutoConfiguration { public void mapperPublish(FlexConfiguration flexConfiguration, FlexGlobalConfig globalConfig, SqlSessionFactory sqlSessionFactory) { - for (Class mapperClz : flexConfiguration.getMapperRegistry().getMappers()) { - MybatisMapperInterceptor handler = new MybatisMapperInterceptor(sqlSessionFactory, mapperClz); + MybatisSessionTemplate sqlSessionTemplate = new MybatisSessionTemplate(sqlSessionFactory); - Object mapperProxy = Proxy.newProxyInstance( - mapperClz.getClassLoader(), - new Class[]{mapperClz}, - handler); + for (Class mapperClz : flexConfiguration.getMapperRegistry().getMappers()) { + Object mapperProxy = sqlSessionTemplate.getMapper(mapperClz); //推入容器,之后可以被注入 appContext.wrapAndPut(mapperClz, mapperProxy); diff --git a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/transaction/MybatisMapperInterceptor.java b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/transaction/MybatisMapperInterceptor.java deleted file mode 100644 index c2eaf868..00000000 --- a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/transaction/MybatisMapperInterceptor.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.mybatisflex.solon.transaction; - -import org.apache.ibatis.session.SqlSession; -import org.apache.ibatis.session.SqlSessionFactory; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; - -/** - * Mybatis Mapper Interceptor - * - * @author noear - * @since 1.6 - */ -public class MybatisMapperInterceptor implements InvocationHandler { - private SqlSessionFactory factory; - private Class mapperClz; - - public MybatisMapperInterceptor(SqlSessionFactory factory, Class mapperClz) { - this.factory = factory; - this.mapperClz = mapperClz; - } - - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - try (SqlSession session = factory.openSession(true)) { - Object mapper = session.getMapper(mapperClz); - return method.invoke(mapper, args); - } - } -} diff --git a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/transaction/MybatisSessionTemplate.java b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/transaction/MybatisSessionTemplate.java new file mode 100644 index 00000000..add106f9 --- /dev/null +++ b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/transaction/MybatisSessionTemplate.java @@ -0,0 +1,174 @@ +package com.mybatisflex.solon.transaction; + +import org.apache.ibatis.cursor.Cursor; +import org.apache.ibatis.executor.BatchResult; +import org.apache.ibatis.session.*; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.sql.Connection; +import java.util.List; +import java.util.Map; + +/** + * @author noear 2024/12/30 created + * @since 3.0 + */ +public class MybatisSessionTemplate implements SqlSession { + private final SqlSessionFactory sqlSessionFactory; + private final ExecutorType executorType; + private final SqlSession sqlSessionProxy; + + public MybatisSessionTemplate(SqlSessionFactory sqlSessionFactory) { + this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType()); + } + + public MybatisSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) { + this.sqlSessionFactory = sqlSessionFactory; + this.executorType = executorType; + this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionInterceptor()); + } + + public SqlSessionFactory getSqlSessionFactory() { + return this.sqlSessionFactory; + } + + public ExecutorType getExecutorType() { + return this.executorType; + } + + public T selectOne(String statement) { + return (T) this.sqlSessionProxy.selectOne(statement); + } + + public T selectOne(String statement, Object parameter) { + return (T) this.sqlSessionProxy.selectOne(statement, parameter); + } + + public Map selectMap(String statement, String mapKey) { + return this.sqlSessionProxy.selectMap(statement, mapKey); + } + + public Map selectMap(String statement, Object parameter, String mapKey) { + return this.sqlSessionProxy.selectMap(statement, parameter, mapKey); + } + + public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) { + return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds); + } + + public Cursor selectCursor(String statement) { + return this.sqlSessionProxy.selectCursor(statement); + } + + public Cursor selectCursor(String statement, Object parameter) { + return this.sqlSessionProxy.selectCursor(statement, parameter); + } + + public Cursor selectCursor(String statement, Object parameter, RowBounds rowBounds) { + return this.sqlSessionProxy.selectCursor(statement, parameter, rowBounds); + } + + public List selectList(String statement) { + return this.sqlSessionProxy.selectList(statement); + } + + public List selectList(String statement, Object parameter) { + return this.sqlSessionProxy.selectList(statement, parameter); + } + + public List selectList(String statement, Object parameter, RowBounds rowBounds) { + return this.sqlSessionProxy.selectList(statement, parameter, rowBounds); + } + + public void select(String statement, ResultHandler handler) { + this.sqlSessionProxy.select(statement, handler); + } + + public void select(String statement, Object parameter, ResultHandler handler) { + this.sqlSessionProxy.select(statement, parameter, handler); + } + + public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { + this.sqlSessionProxy.select(statement, parameter, rowBounds, handler); + } + + public int insert(String statement) { + return this.sqlSessionProxy.insert(statement); + } + + public int insert(String statement, Object parameter) { + return this.sqlSessionProxy.insert(statement, parameter); + } + + public int update(String statement) { + return this.sqlSessionProxy.update(statement); + } + + public int update(String statement, Object parameter) { + return this.sqlSessionProxy.update(statement, parameter); + } + + public int delete(String statement) { + return this.sqlSessionProxy.delete(statement); + } + + public int delete(String statement, Object parameter) { + return this.sqlSessionProxy.delete(statement, parameter); + } + + public T getMapper(Class type) { + return (T) this.getConfiguration().getMapper(type, this); + } + + public void commit() { + throw new UnsupportedOperationException("Manual commit is not allowed over a Solon managed SqlSession"); + } + + public void commit(boolean force) { + throw new UnsupportedOperationException("Manual commit is not allowed over a Solon managed SqlSession"); + } + + public void rollback() { + throw new UnsupportedOperationException("Manual rollback is not allowed over a Solon managed SqlSession"); + } + + public void rollback(boolean force) { + throw new UnsupportedOperationException("Manual rollback is not allowed over a Solon managed SqlSession"); + } + + public void close() { + throw new UnsupportedOperationException("Manual close is not allowed over a Solon managed SqlSession"); + } + + public void clearCache() { + this.sqlSessionProxy.clearCache(); + } + + public Configuration getConfiguration() { + return this.sqlSessionFactory.getConfiguration(); + } + + public Connection getConnection() { + return this.sqlSessionProxy.getConnection(); + } + + public List flushStatements() { + return this.sqlSessionProxy.flushStatements(); + } + + public void destroy() throws Exception { + } + + private class SqlSessionInterceptor implements InvocationHandler { + private SqlSessionInterceptor() { + } + + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + try (SqlSession sqlSession = MybatisSessionTemplate.this.sqlSessionFactory.openSession(MybatisSessionTemplate.this.executorType)) { + return method.invoke(sqlSession, args); + } + } + } +}