提供Dict中setFields方法实现,传入lambda设置部分值

This commit is contained in:
VampireAchao 2022-03-16 13:19:53 +08:00
parent f34a3e9c88
commit e933fb8c2b
5 changed files with 89 additions and 24 deletions

View File

@ -10,12 +10,15 @@ import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.lang.func.LambdaUtil;
/** /**
* 字典对象扩充了HashMap中的方法 * 字典对象扩充了HashMap中的方法
@ -596,4 +599,14 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
} }
return key; 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;
}
} }

View File

@ -29,6 +29,10 @@ public class LambdaUtil {
return _resolve(func); return _resolve(func);
} }
public static <R> SerializedLambda resolve(Func0<R> func) {
return _resolve(func);
}
/** /**
* 获取lambda表达式函数方法名称 * 获取lambda表达式函数方法名称
* *
@ -40,6 +44,10 @@ public class LambdaUtil {
return resolve(func).getImplMethodName(); return resolve(func).getImplMethodName();
} }
public static <R> String getMethodName(Func0<R> func) {
return resolve(func).getImplMethodName();
}
/** /**
* 获取lambda表达式Getter或Setter函数方法对应的字段名称规则如下 * 获取lambda表达式Getter或Setter函数方法对应的字段名称规则如下
* <ul> * <ul>
@ -66,6 +74,17 @@ public class LambdaUtil {
} }
} }
public static <R> String getFieldName(Func0<R> 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表达式,加了缓存 * 解析lambda表达式,加了缓存
* 该缓存可能会在任意不定的时间被清除 * 该缓存可能会在任意不定的时间被清除

View File

@ -3,6 +3,7 @@ package cn.hutool.core.lang;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static cn.hutool.core.lang.OptTest.User;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -14,7 +15,7 @@ public class DictTest {
.set("key1", 1)//int .set("key1", 1)//int
.set("key2", 1000L)//long .set("key2", 1000L)//long
.set("key3", DateTime.now());//Date .set("key3", DateTime.now());//Date
Long v2 = dict.getLong("key2"); Long v2 = dict.getLong("key2");
Assert.assertEquals(Long.valueOf(1000L), v2); Assert.assertEquals(Long.valueOf(1000L), v2);
} }
@ -58,4 +59,13 @@ public class DictTest {
Assert.assertTrue(dict.isEmpty()); 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"));
}
} }

View File

@ -3,12 +3,13 @@ package cn.hutool.db;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collection; import java.util.Collection;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
/** /**
* 动态实体类<br> * 动态实体类<br>
* 提供了针对自身实体的增删改方法 * 提供了针对自身实体的增删改方法
* *
* @author Looly * @author Looly
* *
*/ */
@ -20,7 +21,7 @@ public class ActiveEntity extends Entity {
// --------------------------------------------------------------- Static method start // --------------------------------------------------------------- Static method start
/** /**
* 创建ActiveEntity * 创建ActiveEntity
* *
* @return ActiveEntity * @return ActiveEntity
*/ */
public static ActiveEntity create() { public static ActiveEntity create() {
@ -29,7 +30,7 @@ public class ActiveEntity extends Entity {
/** /**
* 创建ActiveEntity * 创建ActiveEntity
* *
* @param tableName 表名 * @param tableName 表名
* @return ActiveEntity * @return ActiveEntity
*/ */
@ -39,7 +40,7 @@ public class ActiveEntity extends Entity {
/** /**
* 将PO对象转为Entity * 将PO对象转为Entity
* *
* @param <T> Bean对象类型 * @param <T> Bean对象类型
* @param bean Bean对象 * @param bean Bean对象
* @return ActiveEntity * @return ActiveEntity
@ -50,7 +51,7 @@ public class ActiveEntity extends Entity {
/** /**
* 将PO对象转为ActiveEntity * 将PO对象转为ActiveEntity
* *
* @param <T> Bean对象类型 * @param <T> Bean对象类型
* @param bean Bean对象 * @param bean Bean对象
* @param isToUnderlineCase 是否转换为下划线模式 * @param isToUnderlineCase 是否转换为下划线模式
@ -63,7 +64,7 @@ public class ActiveEntity extends Entity {
/** /**
* 将PO对象转为ActiveEntity,并采用下划线法转换字段 * 将PO对象转为ActiveEntity,并采用下划线法转换字段
* *
* @param <T> Bean对象类型 * @param <T> Bean对象类型
* @param bean Bean对象 * @param bean Bean对象
* @return ActiveEntity * @return ActiveEntity
@ -83,7 +84,7 @@ public class ActiveEntity extends Entity {
/** /**
* 构造 * 构造
* *
* @param tableName 表名 * @param tableName 表名
*/ */
public ActiveEntity(String tableName) { public ActiveEntity(String tableName) {
@ -92,7 +93,7 @@ public class ActiveEntity extends Entity {
/** /**
* 构造 * 构造
* *
* @param entity 非动态实体 * @param entity 非动态实体
*/ */
public ActiveEntity(Entity entity) { public ActiveEntity(Entity entity) {
@ -101,7 +102,7 @@ public class ActiveEntity extends Entity {
/** /**
* 构造 * 构造
* *
* @param db {@link Db} * @param db {@link Db}
* @param tableName 表名 * @param tableName 表名
*/ */
@ -112,7 +113,7 @@ public class ActiveEntity extends Entity {
/** /**
* 构造 * 构造
* *
* @param db {@link Db} * @param db {@link Db}
* @param entity 非动态实体 * @param entity 非动态实体
*/ */
@ -122,47 +123,58 @@ public class ActiveEntity extends Entity {
this.db = db; this.db = db;
} }
// -------------------------------------------------------------------------- Constructor end // -------------------------------------------------------------------------- Constructor end
@Override @Override
public ActiveEntity setTableName(String tableName) { public ActiveEntity setTableName(String tableName) {
return (ActiveEntity) super.setTableName(tableName); return (ActiveEntity) super.setTableName(tableName);
} }
@Override @Override
public ActiveEntity setFieldNames(Collection<String> fieldNames) { public ActiveEntity setFieldNames(Collection<String> fieldNames) {
return (ActiveEntity) super.setFieldNames(fieldNames); return (ActiveEntity) super.setFieldNames(fieldNames);
} }
@Override @Override
public ActiveEntity setFieldNames(String... fieldNames) { public ActiveEntity setFieldNames(String... fieldNames) {
return (ActiveEntity) super.setFieldNames(fieldNames); return (ActiveEntity) super.setFieldNames(fieldNames);
} }
/**
* 通过lambda批量设置值
*
* @param fields lambda,不能为空
* @return this
*/
@Override
public ActiveEntity setFields(Func0<?>... fields) {
return (ActiveEntity) super.setFields(fields);
}
@Override @Override
public ActiveEntity addFieldNames(String... fieldNames) { public ActiveEntity addFieldNames(String... fieldNames) {
return (ActiveEntity) super.addFieldNames(fieldNames); return (ActiveEntity) super.addFieldNames(fieldNames);
} }
@Override @Override
public <T> ActiveEntity parseBean(T bean) { public <T> ActiveEntity parseBean(T bean) {
return (ActiveEntity) super.parseBean(bean); return (ActiveEntity) super.parseBean(bean);
} }
@Override @Override
public <T> ActiveEntity parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue) { public <T> ActiveEntity parseBean(T bean, boolean isToUnderlineCase, boolean ignoreNullValue) {
return (ActiveEntity) super.parseBean(bean, isToUnderlineCase, ignoreNullValue); return (ActiveEntity) super.parseBean(bean, isToUnderlineCase, ignoreNullValue);
} }
@Override @Override
public ActiveEntity set(String field, Object value) { public ActiveEntity set(String field, Object value) {
return (ActiveEntity) super.set(field, value); return (ActiveEntity) super.set(field, value);
} }
@Override @Override
public ActiveEntity setIgnoreNull(String field, Object value) { public ActiveEntity setIgnoreNull(String field, Object value) {
return (ActiveEntity) super.setIgnoreNull(field, value); return (ActiveEntity) super.setIgnoreNull(field, value);
} }
@Override @Override
public ActiveEntity clone() { public ActiveEntity clone() {
return (ActiveEntity) super.clone(); return (ActiveEntity) super.clone();
@ -171,7 +183,7 @@ public class ActiveEntity extends Entity {
// -------------------------------------------------------------------------- CRUD start // -------------------------------------------------------------------------- CRUD start
/** /**
* 根据Entity中现有字段条件从数据库中增加一条数据 * 根据Entity中现有字段条件从数据库中增加一条数据
* *
* @return this * @return this
*/ */
public ActiveEntity add() { public ActiveEntity add() {
@ -185,7 +197,7 @@ public class ActiveEntity extends Entity {
/** /**
* 根据Entity中现有字段条件从数据库中加载一个Entity对象 * 根据Entity中现有字段条件从数据库中加载一个Entity对象
* *
* @return this * @return this
*/ */
public ActiveEntity load() { public ActiveEntity load() {
@ -202,7 +214,7 @@ public class ActiveEntity extends Entity {
/** /**
* 根据现有Entity中的条件删除与之匹配的数据库记录 * 根据现有Entity中的条件删除与之匹配的数据库记录
* *
* @return this * @return this
*/ */
public ActiveEntity del() { public ActiveEntity del() {
@ -216,7 +228,7 @@ public class ActiveEntity extends Entity {
/** /**
* 根据现有Entity中的条件删除与之匹配的数据库记录 * 根据现有Entity中的条件删除与之匹配的数据库记录
* *
* @param primaryKey 主键名 * @param primaryKey 主键名
* @return this * @return this
*/ */

View File

@ -2,6 +2,7 @@ package cn.hutool.db;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Dict; import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
@ -172,6 +173,16 @@ public class Entity extends Dict {
return this; return this;
} }
/**
* 通过lambda批量设置值
* @param fields lambda,不能为空
* @return this
*/
@Override
public Entity setFields(Func0<?>... fields) {
return (Entity) super.setFields(fields);
}
/** /**
* 添加字段列表 * 添加字段列表
* *