From cbebecda5805db6eff2ce04bb92ebd97324073bf Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Tue, 15 Mar 2022 22:46:35 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=A2=9E=E5=BC=BAActiveEntity=EF=BC=8C?= =?UTF-8?q?=E4=BD=BF=E5=85=B6=E5=8F=AF=E4=BB=A5=E4=BD=BF=E7=94=A8getter?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E7=9A=84lambda=E5=8F=96=E5=80=BC=E6=88=96?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/db/LambdaEntity.java | 35 +++++++++++++++++++ .../src/test/java/cn/hutool/db/CRUDTest.java | 8 +++++ 2 files changed, 43 insertions(+) create mode 100644 hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java diff --git a/hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java b/hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java new file mode 100644 index 000000000..2f0622ae6 --- /dev/null +++ b/hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java @@ -0,0 +1,35 @@ +package cn.hutool.db; + +import cn.hutool.core.lang.func.Func1; +import cn.hutool.core.lang.func.LambdaUtil; + +/** + * 支持lambda的Entity + * + * @author VampireAchao + */ +public class LambdaEntity extends ActiveEntity { + + public LambdaEntity(T entity) { + super(parse(entity)); + } + + @SuppressWarnings("unchecked") + public R get(Func1 field) { + return (R) super.get(LambdaUtil.getFieldName(field)); + } + + @SuppressWarnings("unchecked") + public LambdaEntity set(Func1 field, Object value) { + return (LambdaEntity) super.set(LambdaUtil.getFieldName(field), value); + } + + @SuppressWarnings("unchecked") + public LambdaEntity setIgnoreNull(Func1 field, Object value) { + if (null != field && null != value) { + return (LambdaEntity) set(LambdaUtil.getFieldName(field), value); + } + return this; + } + +} diff --git a/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java b/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java index d81502707..c08c2df2d 100644 --- a/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java +++ b/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java @@ -120,6 +120,14 @@ public class CRUDTest { Assert.assertFalse(entity.isEmpty()); } + @Test + public void lambdaSetTest() { + LambdaEntity entity = new LambdaEntity<>(new User()); + entity.set(User::getAge, 66).load(); + Assert.assertEquals(new Integer(66), entity.get(User::getAge)); + Assert.assertFalse(entity.isEmpty()); + } + /** * 对增删改查做单元测试 * From f34a3e9c8852628ec252c4d9fb06087b9a496463 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 16 Mar 2022 09:16:54 +0800 Subject: [PATCH 2/7] =?UTF-8?q?Revert=20"=E5=A2=9E=E5=BC=BAActiveEntity?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E5=85=B6=E5=8F=AF=E4=BB=A5=E4=BD=BF=E7=94=A8?= =?UTF-8?q?getter=E5=AF=B9=E5=BA=94=E7=9A=84lambda=E5=8F=96=E5=80=BC?= =?UTF-8?q?=E6=88=96=E8=AE=BE=E7=BD=AE=E6=9D=A1=E4=BB=B6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit cbebecda5805db6eff2ce04bb92ebd97324073bf. --- .../main/java/cn/hutool/db/LambdaEntity.java | 35 ------------------- .../src/test/java/cn/hutool/db/CRUDTest.java | 8 ----- 2 files changed, 43 deletions(-) delete mode 100644 hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java diff --git a/hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java b/hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java deleted file mode 100644 index 2f0622ae6..000000000 --- a/hutool-db/src/main/java/cn/hutool/db/LambdaEntity.java +++ /dev/null @@ -1,35 +0,0 @@ -package cn.hutool.db; - -import cn.hutool.core.lang.func.Func1; -import cn.hutool.core.lang.func.LambdaUtil; - -/** - * 支持lambda的Entity - * - * @author VampireAchao - */ -public class LambdaEntity extends ActiveEntity { - - public LambdaEntity(T entity) { - super(parse(entity)); - } - - @SuppressWarnings("unchecked") - public R get(Func1 field) { - return (R) super.get(LambdaUtil.getFieldName(field)); - } - - @SuppressWarnings("unchecked") - public LambdaEntity set(Func1 field, Object value) { - return (LambdaEntity) super.set(LambdaUtil.getFieldName(field), value); - } - - @SuppressWarnings("unchecked") - public LambdaEntity setIgnoreNull(Func1 field, Object value) { - if (null != field && null != value) { - return (LambdaEntity) set(LambdaUtil.getFieldName(field), value); - } - return this; - } - -} diff --git a/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java b/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java index c08c2df2d..d81502707 100644 --- a/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java +++ b/hutool-db/src/test/java/cn/hutool/db/CRUDTest.java @@ -120,14 +120,6 @@ public class CRUDTest { Assert.assertFalse(entity.isEmpty()); } - @Test - public void lambdaSetTest() { - LambdaEntity entity = new LambdaEntity<>(new User()); - entity.set(User::getAge, 66).load(); - Assert.assertEquals(new Integer(66), entity.get(User::getAge)); - Assert.assertFalse(entity.isEmpty()); - } - /** * 对增删改查做单元测试 * From e933fb8c2bbdba47f73bdb3e2bf40ba548d11b6c Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 16 Mar 2022 13:19:53 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=8F=90=E4=BE=9BDict=E4=B8=ADsetFields?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=AE=9E=E7=8E=B0=EF=BC=8C=E4=BC=A0=E5=85=A5?= =?UTF-8?q?lambda=E8=AE=BE=E7=BD=AE=E9=83=A8=E5=88=86=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/lang/Dict.java | 13 +++++ .../cn/hutool/core/lang/func/LambdaUtil.java | 19 ++++++ .../java/cn/hutool/core/lang/DictTest.java | 12 +++- .../main/java/cn/hutool/db/ActiveEntity.java | 58 +++++++++++-------- .../src/main/java/cn/hutool/db/Entity.java | 11 ++++ 5 files changed, 89 insertions(+), 24 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java b/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java index 168dfba4a..9a5310e29 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java @@ -10,12 +10,15 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Time; import java.sql.Timestamp; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; +import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.LambdaUtil; /** * 字典对象,扩充了HashMap中的方法 @@ -596,4 +599,14 @@ public class Dict extends LinkedHashMap implements BasicTypeGett } return key; } + + /** + * 通过lambda批量设置值 + * @param fields lambda,不能为空 + * @return this + */ + public Dict setFields(Func0... fields) { + Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.callWithRuntimeException())); + return this; + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java index f604ebb00..c204775f2 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java @@ -29,6 +29,10 @@ public class LambdaUtil { return _resolve(func); } + public static SerializedLambda resolve(Func0 func) { + return _resolve(func); + } + /** * 获取lambda表达式函数(方法)名称 * @@ -40,6 +44,10 @@ public class LambdaUtil { return resolve(func).getImplMethodName(); } + public static String getMethodName(Func0 func) { + return resolve(func).getImplMethodName(); + } + /** * 获取lambda表达式Getter或Setter函数(方法)对应的字段名称,规则如下: *
    @@ -66,6 +74,17 @@ public class LambdaUtil { } } + public static String getFieldName(Func0 func) throws IllegalArgumentException { + final String methodName = getMethodName(func); + if (methodName.startsWith("get") || methodName.startsWith("set")) { + return StrUtil.removePreAndLowerFirst(methodName, 3); + } else if (methodName.startsWith("is")) { + return StrUtil.removePreAndLowerFirst(methodName, 2); + } else { + throw new IllegalArgumentException("Invalid Getter or Setter name: " + methodName); + } + } + /** * 解析lambda表达式,加了缓存。 * 该缓存可能会在任意不定的时间被清除 diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java index 81762662d..665944c9c 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java @@ -3,6 +3,7 @@ package cn.hutool.core.lang; import cn.hutool.core.date.DateTime; import org.junit.Assert; import org.junit.Test; +import static cn.hutool.core.lang.OptTest.User; import java.util.HashMap; import java.util.Map; @@ -14,7 +15,7 @@ public class DictTest { .set("key1", 1)//int .set("key2", 1000L)//long .set("key3", DateTime.now());//Date - + Long v2 = dict.getLong("key2"); Assert.assertEquals(Long.valueOf(1000L), v2); } @@ -58,4 +59,13 @@ public class DictTest { Assert.assertTrue(dict.isEmpty()); } + + @Test + public void setFieldsTest() { + User user = User.builder().username("hutool").nickname("Hutool").build(); + Dict dict = Dict.create(); + dict.setFields(user::getNickname, user::getUsername); + Assert.assertEquals("Hutool", dict.get("nickname")); + Assert.assertEquals("hutool", dict.get("username")); + } } diff --git a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java index c0d179270..c5b3a60ab 100644 --- a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java +++ b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java @@ -3,12 +3,13 @@ package cn.hutool.db; import java.sql.SQLException; import java.util.Collection; +import cn.hutool.core.lang.func.Func0; import cn.hutool.core.map.MapUtil; /** * 动态实体类
    * 提供了针对自身实体的增删改方法 - * + * * @author Looly * */ @@ -20,7 +21,7 @@ public class ActiveEntity extends Entity { // --------------------------------------------------------------- Static method start /** * 创建ActiveEntity - * + * * @return ActiveEntity */ public static ActiveEntity create() { @@ -29,7 +30,7 @@ public class ActiveEntity extends Entity { /** * 创建ActiveEntity - * + * * @param tableName 表名 * @return ActiveEntity */ @@ -39,7 +40,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为Entity - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -50,7 +51,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity - * + * * @param Bean对象类型 * @param bean Bean对象 * @param isToUnderlineCase 是否转换为下划线模式 @@ -63,7 +64,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity,并采用下划线法转换字段 - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -83,7 +84,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param tableName 表名 */ public ActiveEntity(String tableName) { @@ -92,7 +93,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param entity 非动态实体 */ public ActiveEntity(Entity entity) { @@ -101,7 +102,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param tableName 表名 */ @@ -112,7 +113,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param entity 非动态实体 */ @@ -122,47 +123,58 @@ public class ActiveEntity extends Entity { this.db = db; } // -------------------------------------------------------------------------- Constructor end - + @Override public ActiveEntity setTableName(String tableName) { return (ActiveEntity) super.setTableName(tableName); } - + @Override public ActiveEntity setFieldNames(Collection fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + @Override public ActiveEntity setFieldNames(String... fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + + /** + * 通过lambda批量设置值 + * + * @param fields lambda,不能为空 + * @return this + */ + @Override + public ActiveEntity setFields(Func0... fields) { + return (ActiveEntity) super.setFields(fields); + } + @Override public ActiveEntity addFieldNames(String... fieldNames) { return (ActiveEntity) super.addFieldNames(fieldNames); } - + @Override public ActiveEntity parseBean(T bean) { return (ActiveEntity) super.parseBean(bean); } - + @Override public ActiveEntity parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue) { return (ActiveEntity) super.parseBean(bean, isToUnderlineCase, ignoreNullValue); } - + @Override public ActiveEntity set(String field, Object value) { return (ActiveEntity) super.set(field, value); } - + @Override public ActiveEntity setIgnoreNull(String field, Object value) { return (ActiveEntity) super.setIgnoreNull(field, value); } - + @Override public ActiveEntity clone() { return (ActiveEntity) super.clone(); @@ -171,7 +183,7 @@ public class ActiveEntity extends Entity { // -------------------------------------------------------------------------- CRUD start /** * 根据Entity中现有字段条件从数据库中增加一条数据 - * + * * @return this */ public ActiveEntity add() { @@ -185,7 +197,7 @@ public class ActiveEntity extends Entity { /** * 根据Entity中现有字段条件从数据库中加载一个Entity对象 - * + * * @return this */ public ActiveEntity load() { @@ -202,7 +214,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @return this */ public ActiveEntity del() { @@ -216,7 +228,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @param primaryKey 主键名 * @return this */ diff --git a/hutool-db/src/main/java/cn/hutool/db/Entity.java b/hutool-db/src/main/java/cn/hutool/db/Entity.java index c3dbeb533..5afd8ca10 100644 --- a/hutool-db/src/main/java/cn/hutool/db/Entity.java +++ b/hutool-db/src/main/java/cn/hutool/db/Entity.java @@ -2,6 +2,7 @@ package cn.hutool.db; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.lang.Dict; +import cn.hutool.core.lang.func.Func0; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ReflectUtil; @@ -172,6 +173,16 @@ public class Entity extends Dict { return this; } + /** + * 通过lambda批量设置值 + * @param fields lambda,不能为空 + * @return this + */ + @Override + public Entity setFields(Func0... fields) { + return (Entity) super.setFields(fields); + } + /** * 添加字段列表 * From 15b5d74180f985017fab6135025c552760b8c709 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 16 Mar 2022 13:25:04 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=EF=BC=8C=E6=8F=90=E4=BE=9B=E4=B8=BAnull?= =?UTF-8?q?=E5=80=BC=E6=97=B6=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java index 665944c9c..ec9bd481d 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java @@ -62,10 +62,10 @@ public class DictTest { @Test public void setFieldsTest() { - User user = User.builder().username("hutool").nickname("Hutool").build(); + User user = User.builder().username("hutool").nickname(null).build(); Dict dict = Dict.create(); dict.setFields(user::getNickname, user::getUsername); - Assert.assertEquals("Hutool", dict.get("nickname")); Assert.assertEquals("hutool", dict.get("username")); + Assert.assertNull(dict.get("nickname")); } } From b7e3db2ec44521793b91ec1116ea8ac309737b55 Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 16 Mar 2022 13:33:06 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E6=8F=90=E4=BE=9BDict=E4=B8=ADsetFields?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=8F=8A=E5=85=B6=E5=AD=90=E7=B1=BB=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=EF=BC=8C=E4=BC=A0=E5=85=A5lambda=E8=83=BD=E5=A4=9F?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=83=A8=E5=88=86=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/core/lang/Dict.java | 13 +++++ .../cn/hutool/core/lang/func/LambdaUtil.java | 19 +++++++ .../java/cn/hutool/core/lang/DictTest.java | 10 ++++ .../main/java/cn/hutool/db/ActiveEntity.java | 57 +++++++++++-------- .../src/main/java/cn/hutool/db/Entity.java | 11 ++++ 5 files changed, 87 insertions(+), 23 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java b/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java index 168dfba4a..9a5310e29 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Dict.java @@ -10,12 +10,15 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Time; import java.sql.Timestamp; +import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; +import cn.hutool.core.lang.func.Func0; +import cn.hutool.core.lang.func.LambdaUtil; /** * 字典对象,扩充了HashMap中的方法 @@ -596,4 +599,14 @@ public class Dict extends LinkedHashMap implements BasicTypeGett } return key; } + + /** + * 通过lambda批量设置值 + * @param fields lambda,不能为空 + * @return this + */ + public Dict setFields(Func0... fields) { + Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.callWithRuntimeException())); + return this; + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java b/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java index f604ebb00..c204775f2 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/func/LambdaUtil.java @@ -29,6 +29,10 @@ public class LambdaUtil { return _resolve(func); } + public static SerializedLambda resolve(Func0 func) { + return _resolve(func); + } + /** * 获取lambda表达式函数(方法)名称 * @@ -40,6 +44,10 @@ public class LambdaUtil { return resolve(func).getImplMethodName(); } + public static String getMethodName(Func0 func) { + return resolve(func).getImplMethodName(); + } + /** * 获取lambda表达式Getter或Setter函数(方法)对应的字段名称,规则如下: *
      @@ -66,6 +74,17 @@ public class LambdaUtil { } } + public static String getFieldName(Func0 func) throws IllegalArgumentException { + final String methodName = getMethodName(func); + if (methodName.startsWith("get") || methodName.startsWith("set")) { + return StrUtil.removePreAndLowerFirst(methodName, 3); + } else if (methodName.startsWith("is")) { + return StrUtil.removePreAndLowerFirst(methodName, 2); + } else { + throw new IllegalArgumentException("Invalid Getter or Setter name: " + methodName); + } + } + /** * 解析lambda表达式,加了缓存。 * 该缓存可能会在任意不定的时间被清除 diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java index 81762662d..3527fc5c2 100644 --- a/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/DictTest.java @@ -3,6 +3,7 @@ package cn.hutool.core.lang; import cn.hutool.core.date.DateTime; import org.junit.Assert; import org.junit.Test; +import static cn.hutool.core.lang.OptTest.User; import java.util.HashMap; import java.util.Map; @@ -58,4 +59,13 @@ public class DictTest { Assert.assertTrue(dict.isEmpty()); } + + @Test + public void setFieldsTest() { + User user = User.builder().username("hutool").nickname(null).build(); + Dict dict = Dict.create(); + dict.setFields(user::getNickname, user::getUsername); + Assert.assertEquals("hutool", dict.get("username")); + Assert.assertNull(dict.get("nickname")); + } } diff --git a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java index c0d179270..29d558654 100644 --- a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java +++ b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java @@ -3,12 +3,13 @@ package cn.hutool.db; import java.sql.SQLException; import java.util.Collection; +import cn.hutool.core.lang.func.Func0; import cn.hutool.core.map.MapUtil; /** * 动态实体类
      * 提供了针对自身实体的增删改方法 - * + * * @author Looly * */ @@ -20,7 +21,7 @@ public class ActiveEntity extends Entity { // --------------------------------------------------------------- Static method start /** * 创建ActiveEntity - * + * * @return ActiveEntity */ public static ActiveEntity create() { @@ -29,7 +30,7 @@ public class ActiveEntity extends Entity { /** * 创建ActiveEntity - * + * * @param tableName 表名 * @return ActiveEntity */ @@ -39,7 +40,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为Entity - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -50,7 +51,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity - * + * * @param Bean对象类型 * @param bean Bean对象 * @param isToUnderlineCase 是否转换为下划线模式 @@ -63,7 +64,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity,并采用下划线法转换字段 - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -83,7 +84,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param tableName 表名 */ public ActiveEntity(String tableName) { @@ -92,7 +93,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param entity 非动态实体 */ public ActiveEntity(Entity entity) { @@ -101,7 +102,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param tableName 表名 */ @@ -112,7 +113,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param entity 非动态实体 */ @@ -122,47 +123,57 @@ public class ActiveEntity extends Entity { this.db = db; } // -------------------------------------------------------------------------- Constructor end - + @Override public ActiveEntity setTableName(String tableName) { return (ActiveEntity) super.setTableName(tableName); } - + @Override public ActiveEntity setFieldNames(Collection fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + @Override public ActiveEntity setFieldNames(String... fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + @Override public ActiveEntity addFieldNames(String... fieldNames) { return (ActiveEntity) super.addFieldNames(fieldNames); } - + + /** + * 通过lambda批量设置值 + * @param fields lambda,不能为空 + * @return this + */ + @Override + public ActiveEntity setFields(Func0... fields) { + return (ActiveEntity) super.setFields(fields); + } + @Override public ActiveEntity parseBean(T bean) { return (ActiveEntity) super.parseBean(bean); } - + @Override public ActiveEntity parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue) { return (ActiveEntity) super.parseBean(bean, isToUnderlineCase, ignoreNullValue); } - + @Override public ActiveEntity set(String field, Object value) { return (ActiveEntity) super.set(field, value); } - + @Override public ActiveEntity setIgnoreNull(String field, Object value) { return (ActiveEntity) super.setIgnoreNull(field, value); } - + @Override public ActiveEntity clone() { return (ActiveEntity) super.clone(); @@ -171,7 +182,7 @@ public class ActiveEntity extends Entity { // -------------------------------------------------------------------------- CRUD start /** * 根据Entity中现有字段条件从数据库中增加一条数据 - * + * * @return this */ public ActiveEntity add() { @@ -185,7 +196,7 @@ public class ActiveEntity extends Entity { /** * 根据Entity中现有字段条件从数据库中加载一个Entity对象 - * + * * @return this */ public ActiveEntity load() { @@ -202,7 +213,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @return this */ public ActiveEntity del() { @@ -216,7 +227,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @param primaryKey 主键名 * @return this */ diff --git a/hutool-db/src/main/java/cn/hutool/db/Entity.java b/hutool-db/src/main/java/cn/hutool/db/Entity.java index c3dbeb533..5afd8ca10 100644 --- a/hutool-db/src/main/java/cn/hutool/db/Entity.java +++ b/hutool-db/src/main/java/cn/hutool/db/Entity.java @@ -2,6 +2,7 @@ package cn.hutool.db; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.lang.Dict; +import cn.hutool.core.lang.func.Func0; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ReflectUtil; @@ -172,6 +173,16 @@ public class Entity extends Dict { return this; } + /** + * 通过lambda批量设置值 + * @param fields lambda,不能为空 + * @return this + */ + @Override + public Entity setFields(Func0... fields) { + return (Entity) super.setFields(fields); + } + /** * 添加字段列表 * From f40b5a04b8afc7cfdafbc07828bc77d4ced8965a Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 16 Mar 2022 13:55:00 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/db/ActiveEntity.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java index d21655eb1..f620705d8 100644 --- a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java +++ b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java @@ -9,7 +9,7 @@ import cn.hutool.core.map.MapUtil; /** * 动态实体类
      * 提供了针对自身实体的增删改方法 - * + * * @author Looly * */ @@ -21,7 +21,7 @@ public class ActiveEntity extends Entity { // --------------------------------------------------------------- Static method start /** * 创建ActiveEntity - * + * * @return ActiveEntity */ public static ActiveEntity create() { @@ -30,7 +30,7 @@ public class ActiveEntity extends Entity { /** * 创建ActiveEntity - * + * * @param tableName 表名 * @return ActiveEntity */ @@ -40,7 +40,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为Entity - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -51,7 +51,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity - * + * * @param Bean对象类型 * @param bean Bean对象 * @param isToUnderlineCase 是否转换为下划线模式 @@ -64,7 +64,7 @@ public class ActiveEntity extends Entity { /** * 将PO对象转为ActiveEntity,并采用下划线法转换字段 - * + * * @param Bean对象类型 * @param bean Bean对象 * @return ActiveEntity @@ -84,7 +84,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param tableName 表名 */ public ActiveEntity(String tableName) { @@ -93,7 +93,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param entity 非动态实体 */ public ActiveEntity(Entity entity) { @@ -102,7 +102,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param tableName 表名 */ @@ -113,7 +113,7 @@ public class ActiveEntity extends Entity { /** * 构造 - * + * * @param db {@link Db} * @param entity 非动态实体 */ @@ -123,17 +123,17 @@ public class ActiveEntity extends Entity { this.db = db; } // -------------------------------------------------------------------------- Constructor end - + @Override public ActiveEntity setTableName(String tableName) { return (ActiveEntity) super.setTableName(tableName); } - + @Override public ActiveEntity setFieldNames(Collection fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + @Override public ActiveEntity setFieldNames(String... fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); @@ -153,27 +153,27 @@ public class ActiveEntity extends Entity { public ActiveEntity addFieldNames(String... fieldNames) { return (ActiveEntity) super.addFieldNames(fieldNames); } - + @Override public ActiveEntity parseBean(T bean) { return (ActiveEntity) super.parseBean(bean); } - + @Override public ActiveEntity parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue) { return (ActiveEntity) super.parseBean(bean, isToUnderlineCase, ignoreNullValue); } - + @Override public ActiveEntity set(String field, Object value) { return (ActiveEntity) super.set(field, value); } - + @Override public ActiveEntity setIgnoreNull(String field, Object value) { return (ActiveEntity) super.setIgnoreNull(field, value); } - + @Override public ActiveEntity clone() { return (ActiveEntity) super.clone(); @@ -182,7 +182,7 @@ public class ActiveEntity extends Entity { // -------------------------------------------------------------------------- CRUD start /** * 根据Entity中现有字段条件从数据库中增加一条数据 - * + * * @return this */ public ActiveEntity add() { @@ -196,7 +196,7 @@ public class ActiveEntity extends Entity { /** * 根据Entity中现有字段条件从数据库中加载一个Entity对象 - * + * * @return this */ public ActiveEntity load() { @@ -213,7 +213,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @return this */ public ActiveEntity del() { @@ -227,7 +227,7 @@ public class ActiveEntity extends Entity { /** * 根据现有Entity中的条件删除与之匹配的数据库记录 - * + * * @param primaryKey 主键名 * @return this */ From e55be878731e79b03a57485fe5ea8af2d2148ffa Mon Sep 17 00:00:00 2001 From: VampireAchao Date: Wed, 16 Mar 2022 13:58:32 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E7=BC=A9=E8=BF=9B=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/cn/hutool/db/ActiveEntity.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java index f620705d8..735174958 100644 --- a/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java +++ b/hutool-db/src/main/java/cn/hutool/db/ActiveEntity.java @@ -123,17 +123,17 @@ public class ActiveEntity extends Entity { this.db = db; } // -------------------------------------------------------------------------- Constructor end - + @Override public ActiveEntity setTableName(String tableName) { return (ActiveEntity) super.setTableName(tableName); } - + @Override public ActiveEntity setFieldNames(Collection fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); } - + @Override public ActiveEntity setFieldNames(String... fieldNames) { return (ActiveEntity) super.setFieldNames(fieldNames); @@ -153,27 +153,27 @@ public class ActiveEntity extends Entity { public ActiveEntity addFieldNames(String... fieldNames) { return (ActiveEntity) super.addFieldNames(fieldNames); } - + @Override public ActiveEntity parseBean(T bean) { return (ActiveEntity) super.parseBean(bean); } - + @Override public ActiveEntity parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue) { return (ActiveEntity) super.parseBean(bean, isToUnderlineCase, ignoreNullValue); } - + @Override public ActiveEntity set(String field, Object value) { return (ActiveEntity) super.set(field, value); } - + @Override public ActiveEntity setIgnoreNull(String field, Object value) { return (ActiveEntity) super.setIgnoreNull(field, value); } - + @Override public ActiveEntity clone() { return (ActiveEntity) super.clone();