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;
}
String className = ClassUtil.getUsefulClass(entity.getClass()).getName();
fieldQueryMap.forEach((key, fieldQuery) -> {
// 不是当前类的内容
if (!key.startsWith(entity.getClass().getName() + "#")) {
if (!key.startsWith(className + "#")) {
return;
}

View File

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

View File

@ -29,6 +29,10 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
/**
* @author michael
* 用于增强对 Cursor 查询处理
*/
public class FlexResultSetHandler extends DefaultResultSetHandler {
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.TableInfoFactory;
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.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author michael
*/
public class MapperInvocationHandler implements InvocationHandler {
private final Object mapper;
private final FlexDataSource dataSource;
public MapperInvocationHandler(Object mapper, Configuration configuration) {
public MapperInvocationHandler(Object mapper, DataSource dataSource) {
this.mapper = mapper;
this.dataSource = (FlexDataSource) configuration.getEnvironment().getDataSource();
this.dataSource = (FlexDataSource) dataSource;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
boolean clearDsKey = false;
boolean clearDbType = false;
boolean needClearDsKey = false;
boolean needClearDbType = false;
try {
//获取用户动态指定由用户指定数据源则应该有用户清除
String dataSourceKey = DataSourceKey.get();
if (StringUtil.isBlank(dataSourceKey)) {
//通过 @UseDataSource 或者 @Table(dataSource) 去获取
String configDataSourceKey = getConfigDataSourceKey(method, proxy);
if (StringUtil.isNotBlank(configDataSourceKey)) {
dataSourceKey = configDataSourceKey;
DataSourceKey.use(dataSourceKey);
clearDsKey = true;
needClearDsKey = true;
}
}
@ -70,16 +72,16 @@ public class MapperInvocationHandler implements InvocationHandler {
dbType = FlexGlobalConfig.getDefaultConfig().getDbType();
}
DialectFactory.setHintDbType(dbType);
clearDbType = true;
needClearDbType = true;
}
return method.invoke(mapper, args);
} catch (InvocationTargetException e1) {
throw e1.getCause();
} catch (InvocationTargetException e) {
throw e.getCause();
} finally {
if (clearDbType) {
if (needClearDbType) {
DialectFactory.clearHintDbType();
}
if (clearDsKey) {
if (needClearDsKey) {
DataSourceKey.clear();
}
}

View File

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

View File

@ -35,7 +35,7 @@ public class TransactionalManager {
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
= ThreadLocal.withInitial(ConcurrentHashMap::new);
@ -198,11 +198,12 @@ public class TransactionalManager {
}
} finally {
holdMap.remove(xid);
if (holdMap.isEmpty()) {
CONNECTION_HOLDER.remove();
}
if (exception != null) {
log.error("TransactionalManager.release() is error. cause: " + exception.getMessage(), exception);
log.error("TransactionalManager.release() is error. Cause: " + exception.getMessage(), exception);
}
}
}