提供Dict中setFields方法及其子类实现,传入lambda能够设置部分值

This commit is contained in:
VampireAchao
2022-03-16 13:33:06 +08:00
parent 23e21f8f9f
commit b7e3db2ec4
5 changed files with 87 additions and 23 deletions

View File

@@ -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;
/**
* 动态实体类<br>
* 提供了针对自身实体的增删改方法
*
*
* @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 <T> Bean对象类型
* @param bean Bean对象
* @return ActiveEntity
@@ -50,7 +51,7 @@ public class ActiveEntity extends Entity {
/**
* 将PO对象转为ActiveEntity
*
*
* @param <T> Bean对象类型
* @param bean Bean对象
* @param isToUnderlineCase 是否转换为下划线模式
@@ -63,7 +64,7 @@ public class ActiveEntity extends Entity {
/**
* 将PO对象转为ActiveEntity,并采用下划线法转换字段
*
*
* @param <T> 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<String> 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 <T> ActiveEntity parseBean(T bean) {
return (ActiveEntity) super.parseBean(bean);
}
@Override
public <T> 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
*/

View File

@@ -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);
}
/**
* 添加字段列表
*