From d88aeb7cc5e5fc455a83ba9aa4cdb66bf5d752d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 12 Jul 2023 09:59:42 +0800 Subject: [PATCH 1/2] feat: add UpdateWrapper.of() method --- .../core/update/UpdateWrapper.java | 10 ++++++++ .../com/mybatisflex/test/AccountTester.java | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java index fd59ac52..1c12a3fc 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateWrapper.java @@ -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); + } + } + } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java index 7637924f..48c592c4 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/AccountTester.java @@ -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 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); + + } + + } From 767eaf293dd342529d16f8eb7d6a85e2a25ced97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Wed, 12 Jul 2023 10:14:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20Relation=20=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=94=AF=E6=8C=81=E9=80=92=E5=BD=92=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotation/RelationManyToMany.java | 1 - .../annotation/RelationManyToOne.java | 1 - .../core/relation/RelationManager.java | 146 +++++++++--------- 3 files changed, 77 insertions(+), 71 deletions(-) diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java index 2bccd0f9..22a44d32 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToMany.java @@ -68,7 +68,6 @@ public @interface RelationManyToMany { */ String orderBy() default ""; - /** * 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。 */ diff --git a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java index 584bdcd4..bdfa1e41 100644 --- a/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java +++ b/mybatis-flex-annotation/src/main/java/com/mybatisflex/annotation/RelationManyToOne.java @@ -32,7 +32,6 @@ public @interface RelationManyToOne { */ String selfField(); - /** * 目标对象的关联属性 * diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java index 8986609f..82f178be 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java @@ -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; /** @@ -38,83 +36,93 @@ import java.util.concurrent.ConcurrentHashMap; */ public class RelationManager { - private static Map, List> classRelations = new ConcurrentHashMap<>(); + private static Map, List> classRelations = new ConcurrentHashMap<>(); - private static List getRelations(Class clazz) { - return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations); - } + private static List getRelations(Class clazz) { + return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations); + } - private static List doGetRelations(Class entityClass) { - List allFields = ClassUtil.getAllFields(entityClass); - List relations = new ArrayList<>(); - for (Field field : allFields) { - RelationManyToMany manyToManyAnnotation = field.getAnnotation(RelationManyToMany.class); - if (manyToManyAnnotation != null) { - relations.add(new ManyToMany<>(manyToManyAnnotation, entityClass, field)); - } + private static List doGetRelations(Class entityClass) { + List allFields = ClassUtil.getAllFields(entityClass); + List relations = new ArrayList<>(); + 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 void queryRelations(BaseMapper mapper, List entities) { - if (CollectionUtil.isEmpty(entities)) { - return; - } + public static void queryRelations(BaseMapper mapper, List entities) { + doQueryRelations(mapper, entities, new HashSet<>()); + } - Class objectClass = (Class) entities.get(0).getClass(); - List relations = getRelations(objectClass); - if (relations.isEmpty()) { - return; - } - String currentDsKey = DataSourceKey.get(); + @SuppressWarnings({"rawtypes", "unchecked"}) + private static void doQueryRelations(BaseMapper mapper, List entities, Set> queriedClass) { + if (CollectionUtil.isEmpty(entities)) { + return; + } + Class objectClass = (Class) entities.get(0).getClass(); + if (queriedClass.contains(objectClass)) { + return; + } else { + queriedClass.add(objectClass); + } + List 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); + } + } + } }