mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
style: format
This commit is contained in:
parent
035c28a50c
commit
b6d306ab0d
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,6 +20,10 @@ import org.apache.ibatis.cursor.Cursor;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author michael
|
||||
* 事务管理器上下文
|
||||
*/
|
||||
public class TransactionContext {
|
||||
|
||||
private TransactionContext() {
|
||||
@ -38,17 +42,15 @@ public class TransactionContext {
|
||||
}
|
||||
|
||||
private static void closeCursor() {
|
||||
try {
|
||||
Cursor<?> cursor = CURSOR_HOLDER.get();
|
||||
if (cursor != null) {
|
||||
try {
|
||||
cursor.close();
|
||||
} catch (IOException e) {
|
||||
//ignore
|
||||
}
|
||||
Cursor<?> cursor = CURSOR_HOLDER.get();
|
||||
if (cursor != null) {
|
||||
try {
|
||||
cursor.close();
|
||||
} catch (IOException e) {
|
||||
//ignore
|
||||
} finally {
|
||||
CURSOR_HOLDER.remove();
|
||||
}
|
||||
} finally {
|
||||
CURSOR_HOLDER.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -70,7 +70,7 @@ public class TransactionalManager {
|
||||
}
|
||||
|
||||
|
||||
//若存在当前事务,则加入当前事务,若不存在当前事务,则已非事务的方式运行
|
||||
//若存在当前事务,则加入当前事务,若不存在当前事务,则已非事务的方式运行
|
||||
case SUPPORTS:
|
||||
return supplier.get();
|
||||
|
||||
@ -84,7 +84,7 @@ public class TransactionalManager {
|
||||
}
|
||||
|
||||
|
||||
//始终以新事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
||||
//始终以新事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
||||
case REQUIRES_NEW:
|
||||
return execNewTransactional(supplier, withResult);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user