mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
mybatis-flex-solon: MybatisMapperInterceptor 方案换成 MybatisSessionTemplate 方案
This commit is contained in:
parent
7716d9d26e
commit
aacdbfaa08
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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> T selectOne(String statement) {
|
||||
return (T) this.sqlSessionProxy.selectOne(statement);
|
||||
}
|
||||
|
||||
public <T> T selectOne(String statement, Object parameter) {
|
||||
return (T) this.sqlSessionProxy.selectOne(statement, parameter);
|
||||
}
|
||||
|
||||
public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
|
||||
return this.sqlSessionProxy.selectMap(statement, mapKey);
|
||||
}
|
||||
|
||||
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
|
||||
return this.sqlSessionProxy.selectMap(statement, parameter, mapKey);
|
||||
}
|
||||
|
||||
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
|
||||
return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds);
|
||||
}
|
||||
|
||||
public <T> Cursor<T> selectCursor(String statement) {
|
||||
return this.sqlSessionProxy.selectCursor(statement);
|
||||
}
|
||||
|
||||
public <T> Cursor<T> selectCursor(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.selectCursor(statement, parameter);
|
||||
}
|
||||
|
||||
public <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds) {
|
||||
return this.sqlSessionProxy.selectCursor(statement, parameter, rowBounds);
|
||||
}
|
||||
|
||||
public <E> List<E> selectList(String statement) {
|
||||
return this.sqlSessionProxy.selectList(statement);
|
||||
}
|
||||
|
||||
public <E> List<E> selectList(String statement, Object parameter) {
|
||||
return this.sqlSessionProxy.selectList(statement, parameter);
|
||||
}
|
||||
|
||||
public <E> List<E> 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> T getMapper(Class<T> 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<BatchResult> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user