style: format

This commit is contained in:
开源海哥 2023-07-20 18:32:38 +08:00
parent 035c28a50c
commit b6d306ab0d
6 changed files with 41 additions and 32 deletions

View File

@ -44,9 +44,11 @@ public class FieldQueryManager {
continue; continue;
} }
String className = ClassUtil.getUsefulClass(entity.getClass()).getName();
fieldQueryMap.forEach((key, fieldQuery) -> { fieldQueryMap.forEach((key, fieldQuery) -> {
// 不是当前类的内容 // 不是当前类的内容
if (!key.startsWith(entity.getClass().getName() + "#")) { if (!key.startsWith(className + "#")) {
return; return;
} }

View File

@ -146,7 +146,6 @@ public class FlexConfiguration extends Configuration {
@Override @Override
public MappedStatement getMappedStatement(String id) { public MappedStatement getMappedStatement(String id) {
MappedStatement ms = super.getMappedStatement(id); MappedStatement ms = super.getMappedStatement(id);
//动态 resultsMap方法名称为selectListByQuery //动态 resultsMap方法名称为selectListByQuery
Class<?> asType = MappedStatementTypes.getCurrentType(); Class<?> asType = MappedStatementTypes.getCurrentType();
if (asType != null) { if (asType != null) {
@ -333,13 +332,12 @@ public class FlexConfiguration extends Configuration {
} }
@SuppressWarnings("unchecked")
@Override @Override
public <T> T getMapper(Class<T> type, SqlSession sqlSession) { public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
T mapper = super.getMapper(type, sqlSession); T mapper = super.getMapper(type, sqlSession);
return (T) Proxy.newProxyInstance(type.getClassLoader() return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}
, new Class[]{type} , new MapperInvocationHandler(mapper, environment.getDataSource()));
, new MapperInvocationHandler(mapper, this));
} }
} }

View File

@ -29,6 +29,10 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Iterator; import java.util.Iterator;
/**
* @author michael
* 用于增强对 Cursor 查询处理
*/
public class FlexResultSetHandler extends DefaultResultSetHandler { public class FlexResultSetHandler extends DefaultResultSetHandler {
public FlexResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler public FlexResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler

View File

@ -25,38 +25,40 @@ import com.mybatisflex.core.row.RowMapper;
import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.session.Configuration;
import javax.sql.DataSource;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
/**
* @author michael
*/
public class MapperInvocationHandler implements InvocationHandler { public class MapperInvocationHandler implements InvocationHandler {
private final Object mapper; private final Object mapper;
private final FlexDataSource dataSource; private final FlexDataSource dataSource;
public MapperInvocationHandler(Object mapper, Configuration configuration) { public MapperInvocationHandler(Object mapper, DataSource dataSource) {
this.mapper = mapper; this.mapper = mapper;
this.dataSource = (FlexDataSource) configuration.getEnvironment().getDataSource(); this.dataSource = (FlexDataSource) dataSource;
} }
@Override @Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
boolean clearDsKey = false; boolean needClearDsKey = false;
boolean clearDbType = false; boolean needClearDbType = false;
try { try {
//获取用户动态指定由用户指定数据源则应该有用户清除 //获取用户动态指定由用户指定数据源则应该有用户清除
String dataSourceKey = DataSourceKey.get(); String dataSourceKey = DataSourceKey.get();
if (StringUtil.isBlank(dataSourceKey)) { if (StringUtil.isBlank(dataSourceKey)) {
//通过 @UseDataSource 或者 @Table(dataSource) 去获取 //通过 @UseDataSource 或者 @Table(dataSource) 去获取
String configDataSourceKey = getConfigDataSourceKey(method, proxy); String configDataSourceKey = getConfigDataSourceKey(method, proxy);
if (StringUtil.isNotBlank(configDataSourceKey)) { if (StringUtil.isNotBlank(configDataSourceKey)) {
dataSourceKey = configDataSourceKey; dataSourceKey = configDataSourceKey;
DataSourceKey.use(dataSourceKey); DataSourceKey.use(dataSourceKey);
clearDsKey = true; needClearDsKey = true;
} }
} }
@ -70,16 +72,16 @@ public class MapperInvocationHandler implements InvocationHandler {
dbType = FlexGlobalConfig.getDefaultConfig().getDbType(); dbType = FlexGlobalConfig.getDefaultConfig().getDbType();
} }
DialectFactory.setHintDbType(dbType); DialectFactory.setHintDbType(dbType);
clearDbType = true; needClearDbType = true;
} }
return method.invoke(mapper, args); return method.invoke(mapper, args);
} catch (InvocationTargetException e1) { } catch (InvocationTargetException e) {
throw e1.getCause(); throw e.getCause();
} finally { } finally {
if (clearDbType) { if (needClearDbType) {
DialectFactory.clearHintDbType(); DialectFactory.clearHintDbType();
} }
if (clearDsKey) { if (needClearDsKey) {
DataSourceKey.clear(); DataSourceKey.clear();
} }
} }

View File

@ -20,6 +20,10 @@ import org.apache.ibatis.cursor.Cursor;
import java.io.IOException; import java.io.IOException;
/**
* @author michael
* 事务管理器上下文
*/
public class TransactionContext { public class TransactionContext {
private TransactionContext() { private TransactionContext() {
@ -38,17 +42,15 @@ public class TransactionContext {
} }
private static void closeCursor() { private static void closeCursor() {
try { Cursor<?> cursor = CURSOR_HOLDER.get();
Cursor<?> cursor = CURSOR_HOLDER.get(); if (cursor != null) {
if (cursor != null) { try {
try { cursor.close();
cursor.close(); } catch (IOException e) {
} catch (IOException e) { //ignore
//ignore } finally {
} CURSOR_HOLDER.remove();
} }
} finally {
CURSOR_HOLDER.remove();
} }
} }

View File

@ -35,7 +35,7 @@ public class TransactionalManager {
private static final Log log = LogFactory.getLog(TransactionalManager.class); private static final Log log = LogFactory.getLog(TransactionalManager.class);
//<xid : <datasource : connection>> //<xid : <dataSourceKey : connection>>
private static final ThreadLocal<Map<String, Map<String, Connection>>> CONNECTION_HOLDER private static final ThreadLocal<Map<String, Map<String, Connection>>> CONNECTION_HOLDER
= ThreadLocal.withInitial(ConcurrentHashMap::new); = ThreadLocal.withInitial(ConcurrentHashMap::new);
@ -70,7 +70,7 @@ public class TransactionalManager {
} }
//若存在当前事务则加入当前事务若不存在当前事务则已非事务的方式运行 //若存在当前事务则加入当前事务若不存在当前事务则已非事务的方式运行
case SUPPORTS: case SUPPORTS:
return supplier.get(); return supplier.get();
@ -84,7 +84,7 @@ public class TransactionalManager {
} }
//始终以新事务的方式运行若存在当前事务则暂停挂起当前事务 //始终以新事务的方式运行若存在当前事务则暂停挂起当前事务
case REQUIRES_NEW: case REQUIRES_NEW:
return execNewTransactional(supplier, withResult); return execNewTransactional(supplier, withResult);
@ -198,11 +198,12 @@ public class TransactionalManager {
} }
} finally { } finally {
holdMap.remove(xid); holdMap.remove(xid);
if (holdMap.isEmpty()) { if (holdMap.isEmpty()) {
CONNECTION_HOLDER.remove(); CONNECTION_HOLDER.remove();
} }
if (exception != null) { if (exception != null) {
log.error("TransactionalManager.release() is error. cause: " + exception.getMessage(), exception); log.error("TransactionalManager.release() is error. Cause: " + exception.getMessage(), exception);
} }
} }
} }