mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 09:08:24 +08:00
add Db.txWithResult method; close #I7DJBD
This commit is contained in:
parent
ee0e3f7465
commit
ddb239176a
@ -41,7 +41,8 @@ import java.util.function.Supplier;
|
|||||||
*/
|
*/
|
||||||
public class Db {
|
public class Db {
|
||||||
|
|
||||||
private Db() {}
|
private Db() {
|
||||||
|
}
|
||||||
|
|
||||||
private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>();
|
private static final Map<String, RowMapperInvoker> INVOKER_MAP = new ConcurrentHashMap<>();
|
||||||
static RowMapperInvoker defaultRowMapperInvoker;
|
static RowMapperInvoker defaultRowMapperInvoker;
|
||||||
@ -73,6 +74,7 @@ public class Db {
|
|||||||
public static int insert(String schema, String tableName, Row row) {
|
public static int insert(String schema, String tableName, Row row) {
|
||||||
return invoker().insert(schema, tableName, row);
|
return invoker().insert(schema, tableName, row);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 往 tableName 插入一条 row 数据
|
* 往 tableName 插入一条 row 数据
|
||||||
*
|
*
|
||||||
@ -131,6 +133,7 @@ public class Db {
|
|||||||
mapper.insert(schema, tableName, row);
|
mapper.insert(schema, tableName, row);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量插入数据
|
* 批量插入数据
|
||||||
*
|
*
|
||||||
@ -479,7 +482,6 @@ public class Db {
|
|||||||
/**
|
/**
|
||||||
* 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
|
* 通过 update schema.table set field = field + 1 where ... 的这种方向更新数据库某个字段内容
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @param schema 模式
|
* @param schema 模式
|
||||||
* @param tableName 表名
|
* @param tableName 表名
|
||||||
* @param fieldName 字段名
|
* @param fieldName 字段名
|
||||||
@ -587,7 +589,6 @@ public class Db {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 map 来查询 1 条数据
|
* 根据 map 来查询 1 条数据
|
||||||
*
|
*
|
||||||
@ -734,6 +735,7 @@ public class Db {
|
|||||||
public static List<Row> selectListByCondition(String tableName, QueryCondition condition) {
|
public static List<Row> selectListByCondition(String tableName, QueryCondition condition) {
|
||||||
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition));
|
return invoker().selectListByQuery(null, tableName, new QueryWrapper().where(condition));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 condition 条件来查询数据列表
|
* 根据 condition 条件来查询数据列表
|
||||||
*
|
*
|
||||||
@ -859,7 +861,6 @@ public class Db {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询某列内容,数据返回应该有 多行 1 列
|
* 查询某列内容,数据返回应该有 多行 1 列
|
||||||
*
|
*
|
||||||
@ -1111,17 +1112,31 @@ public class Db {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 进行事务操作
|
* 进行事务操作,返回 null 或者 false 或者 抛出异常,事务回滚
|
||||||
*
|
|
||||||
* @param supplier
|
|
||||||
*/
|
*/
|
||||||
public static boolean tx(Supplier<Boolean> supplier) {
|
public static boolean tx(Supplier<Boolean> supplier) {
|
||||||
return tx(supplier, Propagation.REQUIRED);
|
return tx(supplier, Propagation.REQUIRED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进行事务操作,返回 null 或者 false 或者 抛出异常, 事务回滚
|
||||||
|
*/
|
||||||
public static boolean tx(Supplier<Boolean> supplier, Propagation propagation) {
|
public static boolean tx(Supplier<Boolean> supplier, Propagation propagation) {
|
||||||
Boolean result = TransactionalManager.exec(supplier, propagation);
|
Boolean result = TransactionalManager.exec(supplier, propagation, false);
|
||||||
return result != null && result;
|
return result != null && result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚
|
||||||
|
*/
|
||||||
|
public static <T> T txWithResult(Supplier<T> supplier) {
|
||||||
|
return txWithResult(supplier, Propagation.REQUIRED);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进行事务操作,和返回结果无关,只有抛出异常时,事务回滚
|
||||||
|
*/
|
||||||
|
public static <T> T txWithResult(Supplier<T> supplier, Propagation propagation) {
|
||||||
|
return TransactionalManager.exec(supplier, propagation, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,7 +30,8 @@ import java.util.function.Supplier;
|
|||||||
*/
|
*/
|
||||||
public class TransactionalManager {
|
public class TransactionalManager {
|
||||||
|
|
||||||
private TransactionalManager() {}
|
private TransactionalManager() {
|
||||||
|
}
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(TransactionalManager.class);
|
private static final Log log = LogFactory.getLog(TransactionalManager.class);
|
||||||
|
|
||||||
@ -55,7 +56,7 @@ public class TransactionalManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Boolean exec(Supplier<Boolean> supplier, Propagation propagation) {
|
public static <T> T exec(Supplier<T> supplier, Propagation propagation, boolean withResult) {
|
||||||
//上一级事务的id,支持事务嵌套
|
//上一级事务的id,支持事务嵌套
|
||||||
String currentXID = TransactionContext.getXID();
|
String currentXID = TransactionContext.getXID();
|
||||||
try {
|
try {
|
||||||
@ -65,7 +66,7 @@ public class TransactionalManager {
|
|||||||
if (currentXID != null) {
|
if (currentXID != null) {
|
||||||
return supplier.get();
|
return supplier.get();
|
||||||
} else {
|
} else {
|
||||||
return execNewTransactional(supplier);
|
return execNewTransactional(supplier, withResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -85,7 +86,7 @@ public class TransactionalManager {
|
|||||||
|
|
||||||
//始终以新事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
//始终以新事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
||||||
case REQUIRES_NEW:
|
case REQUIRES_NEW:
|
||||||
return execNewTransactional(supplier);
|
return execNewTransactional(supplier, withResult);
|
||||||
|
|
||||||
|
|
||||||
//以非事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
//以非事务的方式运行,若存在当前事务,则暂停(挂起)当前事务。
|
||||||
@ -118,24 +119,32 @@ public class TransactionalManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Boolean execNewTransactional(Supplier<Boolean> supplier) {
|
private static <T> T execNewTransactional(Supplier<T> supplier, boolean withResult) {
|
||||||
String xid = startTransactional();
|
String xid = startTransactional();
|
||||||
Boolean success = false;
|
T result = null;
|
||||||
boolean rollbacked = false;
|
boolean isRollback = false;
|
||||||
try {
|
try {
|
||||||
success = supplier.get();
|
result = supplier.get();
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
rollbacked = true;
|
isRollback = true;
|
||||||
rollback(xid);
|
rollback(xid);
|
||||||
throw new TransactionException(e.getMessage(), e);
|
throw new TransactionException(e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
if (success != null && success) {
|
if (!isRollback) {
|
||||||
|
if (!withResult) {
|
||||||
|
if (result instanceof Boolean && (Boolean) result) {
|
||||||
commit(xid);
|
commit(xid);
|
||||||
} else if (!rollbacked) {
|
}
|
||||||
|
//null or false
|
||||||
|
else {
|
||||||
rollback(xid);
|
rollback(xid);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
commit(xid);
|
||||||
}
|
}
|
||||||
return success;
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 王帅
|
* @author 王帅
|
||||||
@ -32,9 +33,12 @@ class ReflectTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test() {
|
void test() {
|
||||||
Field field = ClassUtil.getAllFields(Account.class, f -> f.getName().equals("list")).get(0);
|
List<Field> allFields = ClassUtil.getAllFields(Account.class);
|
||||||
|
for (Field field : allFields) {
|
||||||
Type type = TypeParameterResolver.resolveFieldType(field, Account.class);
|
Type type = TypeParameterResolver.resolveFieldType(field, Account.class);
|
||||||
System.out.println(type);
|
System.out.println("field: " + field+"----->Type:" + type);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user