mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
Merge branch 'main' of gitee.com:mybatis-flex/mybatis-flex into main
Signed-off-by: 王帅 <1474983351@qq.com>
This commit is contained in:
commit
302e884d67
@ -28,9 +28,7 @@ import com.mybatisflex.core.util.StringUtil;
|
||||
import org.apache.ibatis.util.MapUtil;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@ -50,74 +48,84 @@ public class RelationManager {
|
||||
private static List<AbstractRelation> doGetRelations(Class<?> entityClass) {
|
||||
List<Field> allFields = ClassUtil.getAllFields(entityClass);
|
||||
List<AbstractRelation> relations = new ArrayList<>();
|
||||
for (Field field : allFields) {
|
||||
RelationManyToMany manyToManyAnnotation = field.getAnnotation(RelationManyToMany.class);
|
||||
if (manyToManyAnnotation != null) {
|
||||
relations.add(new ManyToMany<>(manyToManyAnnotation, entityClass, field));
|
||||
}
|
||||
for (Field field : allFields) {
|
||||
RelationManyToMany manyToManyAnnotation = field.getAnnotation(RelationManyToMany.class);
|
||||
if (manyToManyAnnotation != null) {
|
||||
relations.add(new ManyToMany<>(manyToManyAnnotation, entityClass, field));
|
||||
}
|
||||
|
||||
RelationManyToOne manyToOneAnnotation = field.getAnnotation(RelationManyToOne.class);
|
||||
if (manyToOneAnnotation != null) {
|
||||
relations.add(new ManyToOne<>(manyToOneAnnotation, entityClass, field));
|
||||
}
|
||||
RelationManyToOne manyToOneAnnotation = field.getAnnotation(RelationManyToOne.class);
|
||||
if (manyToOneAnnotation != null) {
|
||||
relations.add(new ManyToOne<>(manyToOneAnnotation, entityClass, field));
|
||||
}
|
||||
|
||||
RelationOneToMany oneToManyAnnotation = field.getAnnotation(RelationOneToMany.class);
|
||||
if (oneToManyAnnotation != null) {
|
||||
relations.add(new OneToMany<>(oneToManyAnnotation, entityClass, field));
|
||||
}
|
||||
RelationOneToMany oneToManyAnnotation = field.getAnnotation(RelationOneToMany.class);
|
||||
if (oneToManyAnnotation != null) {
|
||||
relations.add(new OneToMany<>(oneToManyAnnotation, entityClass, field));
|
||||
}
|
||||
|
||||
RelationOneToOne oneToOneAnnotation = field.getAnnotation(RelationOneToOne.class);
|
||||
if (oneToOneAnnotation != null) {
|
||||
relations.add(new OneToOne<>(oneToOneAnnotation, entityClass, field));
|
||||
}
|
||||
}
|
||||
return relations;
|
||||
}
|
||||
RelationOneToOne oneToOneAnnotation = field.getAnnotation(RelationOneToOne.class);
|
||||
if (oneToOneAnnotation != null) {
|
||||
relations.add(new OneToOne<>(oneToOneAnnotation, entityClass, field));
|
||||
}
|
||||
}
|
||||
return relations;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public static <Entity> void queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
|
||||
if (CollectionUtil.isEmpty(entities)) {
|
||||
return;
|
||||
}
|
||||
public static <Entity> void queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
|
||||
doQueryRelations(mapper, entities, new HashSet<>());
|
||||
}
|
||||
|
||||
Class<Entity> objectClass = (Class<Entity>) entities.get(0).getClass();
|
||||
List<AbstractRelation> relations = getRelations(objectClass);
|
||||
if (relations.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String currentDsKey = DataSourceKey.get();
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
private static <Entity> void doQueryRelations(BaseMapper<?> mapper, List<Entity> entities, Set<Class<?>> queriedClass) {
|
||||
if (CollectionUtil.isEmpty(entities)) {
|
||||
return;
|
||||
}
|
||||
Class<Entity> objectClass = (Class<Entity>) entities.get(0).getClass();
|
||||
if (queriedClass.contains(objectClass)) {
|
||||
return;
|
||||
} else {
|
||||
queriedClass.add(objectClass);
|
||||
}
|
||||
List<AbstractRelation> relations = getRelations(objectClass);
|
||||
if (relations.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
String currentDsKey = DataSourceKey.get();
|
||||
try {
|
||||
relations.forEach(relation -> {
|
||||
|
||||
try {
|
||||
relations.forEach(relation -> {
|
||||
QueryWrapper queryWrapper = relation.toQueryWrapper(entities);
|
||||
Class<?> mappingType = relation.getMappingType();
|
||||
|
||||
QueryWrapper queryWrapper = relation.toQueryWrapper(entities);
|
||||
Class<?> mappingType = relation.getMappingType();
|
||||
String dataSource = relation.getDataSource();
|
||||
if (StringUtil.isBlank(dataSource) && currentDsKey != null) {
|
||||
dataSource = currentDsKey;
|
||||
}
|
||||
|
||||
String dataSource = relation.getDataSource();
|
||||
if (StringUtil.isBlank(dataSource) && currentDsKey != null) {
|
||||
dataSource = currentDsKey;
|
||||
}
|
||||
try {
|
||||
if (StringUtil.isNotBlank(dataSource)) {
|
||||
DataSourceKey.use(dataSource);
|
||||
}
|
||||
|
||||
try {
|
||||
if (StringUtil.isNotBlank(dataSource)) {
|
||||
DataSourceKey.use(dataSource);
|
||||
}
|
||||
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, mappingType);
|
||||
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
||||
relation.join(entities, targetObjectList, mapper);
|
||||
}
|
||||
} finally {
|
||||
if (StringUtil.isNotBlank(dataSource)) {
|
||||
DataSourceKey.clear();
|
||||
}
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
if (currentDsKey != null) {
|
||||
DataSourceKey.use(currentDsKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, mappingType);
|
||||
doQueryRelations(mapper, targetObjectList, queriedClass);
|
||||
|
||||
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
||||
relation.join(entities, targetObjectList, mapper);
|
||||
}
|
||||
} finally {
|
||||
if (StringUtil.isNotBlank(dataSource)) {
|
||||
DataSourceKey.clear();
|
||||
}
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
if (currentDsKey != null) {
|
||||
DataSourceKey.use(currentDsKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,6 +20,7 @@ import com.mybatisflex.core.query.QueryCondition;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.util.LambdaGetter;
|
||||
import com.mybatisflex.core.util.LambdaUtil;
|
||||
import com.mybatisflex.core.util.UpdateEntity;
|
||||
import org.apache.ibatis.javassist.util.proxy.ProxyObject;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -79,4 +80,13 @@ public interface UpdateWrapper extends Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
static UpdateWrapper of(Object entity) {
|
||||
if (entity instanceof UpdateWrapper) {
|
||||
return (UpdateWrapper) entity;
|
||||
} else {
|
||||
return (UpdateWrapper) UpdateEntity.ofNotNull(entity);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -21,6 +21,8 @@ import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.audit.MessageCollector;
|
||||
import com.mybatisflex.core.query.If;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.update.UpdateWrapper;
|
||||
import com.mybatisflex.core.util.UpdateEntity;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
@ -85,4 +87,25 @@ public class AccountTester {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
List<Account> accounts = accountMapper.selectAll();
|
||||
System.out.println(accounts);
|
||||
|
||||
|
||||
Account account = UpdateEntity.of(Account.class,1);
|
||||
account.setUserName("lisi");
|
||||
|
||||
UpdateWrapper.of(account)
|
||||
.setRaw("age","age + 1");
|
||||
accountMapper.update(account);
|
||||
|
||||
|
||||
accounts = accountMapper.selectAll();
|
||||
System.out.println(accounts);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user