mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add ValueListHandler
This commit is contained in:
parent
df96ef1a29
commit
1ed2ec9dc4
@ -8,6 +8,8 @@
|
||||
### 新特性
|
||||
* 【core 】 NumberUtil.decimalFormat增加Object对象参数支持
|
||||
* 【core 】 增加ReflectUtil.getFieldValue支持Alias注解
|
||||
* 【core 】 Bean字段支持Alias注解(包括转map,转bean等)
|
||||
* 【core 】 增加ValueListHandler,优化结果集获取方式
|
||||
|
||||
### Bug修复
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.annotation.Alias;
|
||||
import cn.hutool.core.bean.copier.CopyOptions;
|
||||
import cn.hutool.core.bean.copier.ValueProvider;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
@ -143,6 +144,18 @@ public class BeanUtilTest {
|
||||
Assert.assertEquals("sub名字", map.get("sub_name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanToMapWithAliasTest() {
|
||||
SubPersonWithAlias person = new SubPersonWithAlias();
|
||||
person.setAge(14);
|
||||
person.setOpenid("11213232");
|
||||
person.setName("测试A11");
|
||||
person.setSubName("sub名字");
|
||||
|
||||
Map<String, Object> map = BeanUtil.beanToMap(person);
|
||||
Assert.assertEquals("sub名字", map.get("aliasSubName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanToMapWithLocalDateTimeTest() {
|
||||
final LocalDateTime now = LocalDateTime.now();
|
||||
@ -270,6 +283,15 @@ public class BeanUtilTest {
|
||||
private Boolean slow;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class SubPersonWithAlias extends Person {
|
||||
@Alias("aliasSubName")
|
||||
private String subName;
|
||||
// boolean参数值非isXXX形式
|
||||
private Boolean slow;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class Person {
|
||||
|
@ -152,6 +152,7 @@ public final class DbUtil {
|
||||
*
|
||||
* @param objsToClose 需要关闭的对象
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public static void close(Object... objsToClose) {
|
||||
for (Object obj : objsToClose) {
|
||||
if (obj instanceof AutoCloseable) {
|
||||
|
@ -141,7 +141,7 @@ public class SqlConnRunner {
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public int[] insert(Connection conn, Collection<Entity> records) throws SQLException {
|
||||
return insert(conn, records.toArray(new Entity[records.size()]));
|
||||
return insert(conn, records.toArray(new Entity[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -505,7 +505,7 @@ public class SqlConnRunner {
|
||||
checkConn(conn);
|
||||
|
||||
final int count = count(conn, where);
|
||||
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<Entity>(page, numPerPage, count));
|
||||
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<>(page, numPerPage, count));
|
||||
return this.page(conn, fields, where, page, numPerPage, pageResultHandler);
|
||||
}
|
||||
|
||||
@ -532,7 +532,7 @@ public class SqlConnRunner {
|
||||
}
|
||||
|
||||
final int count = count(conn, where);
|
||||
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<Entity>(page.getPageNumber(), page.getPageSize(), count));
|
||||
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<>(page.getPageNumber(), page.getPageSize(), count));
|
||||
return this.page(conn, fields, where, page, pageResultHandler);
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,9 @@ import cn.hutool.db.Entity;
|
||||
public class EntityHandler implements RsHandler<Entity>{
|
||||
private static final long serialVersionUID = -8742432871908355992L;
|
||||
|
||||
/** 是否大小写不敏感 */
|
||||
private boolean caseInsensitive;
|
||||
|
||||
/**
|
||||
* 创建一个 EntityHandler对象
|
||||
* @return EntityHandler对象
|
||||
@ -23,11 +26,27 @@ public class EntityHandler implements RsHandler<Entity>{
|
||||
return new EntityHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public EntityHandler() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param caseInsensitive 是否大小写不敏感
|
||||
*/
|
||||
public EntityHandler(boolean caseInsensitive) {
|
||||
this.caseInsensitive = caseInsensitive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entity handle(ResultSet rs) throws SQLException {
|
||||
final ResultSetMetaData meta = rs.getMetaData();
|
||||
final int columnCount = meta.getColumnCount();
|
||||
|
||||
return rs.next() ? HandleHelper.handleRow(columnCount, meta, rs) : null;
|
||||
return rs.next() ? HandleHelper.handleRow(columnCount, meta, rs, this.caseInsensitive) : null;
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,9 @@ import cn.hutool.db.Entity;
|
||||
public class EntitySetHandler implements RsHandler<LinkedHashSet<Entity>>{
|
||||
private static final long serialVersionUID = 8191723216703506736L;
|
||||
|
||||
/** 是否大小写不敏感 */
|
||||
private boolean caseInsensitive;
|
||||
|
||||
/**
|
||||
* 创建一个 EntityHandler对象
|
||||
* @return EntityHandler对象
|
||||
@ -22,8 +25,24 @@ public class EntitySetHandler implements RsHandler<LinkedHashSet<Entity>>{
|
||||
return new EntitySetHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
public EntitySetHandler() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param caseInsensitive 是否大小写不敏感
|
||||
*/
|
||||
public EntitySetHandler(boolean caseInsensitive) {
|
||||
this.caseInsensitive = caseInsensitive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Entity> handle(ResultSet rs) throws SQLException {
|
||||
return HandleHelper.handleRs(rs, new LinkedHashSet<Entity>());
|
||||
return HandleHelper.handleRs(rs, new LinkedHashSet<>(), this.caseInsensitive);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,9 @@ import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import cn.hutool.core.bean.BeanDesc.PropDesc;
|
||||
@ -97,8 +99,8 @@ public class HandleHelper {
|
||||
}
|
||||
setter = (null == pd) ? null : pd.getSetter();
|
||||
if(null != setter) {
|
||||
value = getColumnValue(rs, columnLabel, meta.getColumnType(i), TypeUtil.getFirstParamType(setter));
|
||||
ReflectUtil.invokeWithCheck(bean, setter, new Object[] {value});
|
||||
value = getColumnValue(rs, i, meta.getColumnType(i), TypeUtil.getFirstParamType(setter));
|
||||
ReflectUtil.invokeWithCheck(bean, setter, value);
|
||||
}
|
||||
}
|
||||
return bean;
|
||||
@ -149,9 +151,8 @@ public class HandleHelper {
|
||||
String columnLabel;
|
||||
int type;
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
columnLabel = meta.getColumnLabel(i);
|
||||
type = meta.getColumnType(i);
|
||||
row.put(columnLabel, getColumnValue(rs, columnLabel, type, null));
|
||||
row.put(meta.getColumnLabel(i), getColumnValue(rs, i, type, null));
|
||||
}
|
||||
if (withMetaInfo) {
|
||||
row.setTableName(meta.getTableName(1));
|
||||
@ -162,7 +163,7 @@ public class HandleHelper {
|
||||
|
||||
/**
|
||||
* 处理单条数据
|
||||
*
|
||||
*
|
||||
* @param rs 数据集
|
||||
* @return 每一行的Entity
|
||||
* @throws SQLException SQL执行异常
|
||||
@ -173,6 +174,25 @@ public class HandleHelper {
|
||||
return handleRow(columnCount, meta, rs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单行数据
|
||||
*
|
||||
* @param rs 数据集(行)
|
||||
* @return 每一行的List
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.1.6
|
||||
*/
|
||||
public static List<Object> handleRowToList(ResultSet rs) throws SQLException {
|
||||
final ResultSetMetaData meta = rs.getMetaData();
|
||||
final int columnCount = meta.getColumnCount();
|
||||
final List<Object> row = new ArrayList<>(columnCount);
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
row.add(getColumnValue(rs, i, meta.getColumnType(i), null));
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理多条数据
|
||||
*
|
||||
@ -232,39 +252,6 @@ public class HandleHelper {
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* 获取字段值<br>
|
||||
* 针对日期时间等做单独处理判断
|
||||
*
|
||||
* @param <T> 返回类型
|
||||
* @param rs {@link ResultSet}
|
||||
* @param label 字段标签或者字段名
|
||||
* @param type 字段类型,默认Object
|
||||
* @param targetColumnType 结果要求的类型,需进行二次转换(null或者Object不转换)
|
||||
* @return 字段值
|
||||
* @throws SQLException SQL异常
|
||||
*/
|
||||
private static <T> Object getColumnValue(ResultSet rs, String label, int type, Type targetColumnType) throws SQLException {
|
||||
Object rawValue;
|
||||
switch (type) {
|
||||
case Types.TIMESTAMP:
|
||||
rawValue = rs.getTimestamp(label);
|
||||
break;
|
||||
case Types.TIME:
|
||||
rawValue = rs.getTime(label);
|
||||
break;
|
||||
default:
|
||||
rawValue = rs.getObject(label);
|
||||
}
|
||||
if (null == targetColumnType || Object.class == targetColumnType) {
|
||||
// 无需转换
|
||||
return rawValue;
|
||||
} else {
|
||||
// 按照返回值要求转换
|
||||
return Convert.convert(targetColumnType, rawValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字段值<br>
|
||||
* 针对日期时间等做单独处理判断
|
||||
|
@ -0,0 +1,32 @@
|
||||
package cn.hutool.db.handler;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 结果集处理类 ,处理出的结果为List列表
|
||||
* @author loolly
|
||||
*
|
||||
*/
|
||||
public class ValueListHandler implements RsHandler<List<List<Object>>>{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建一个 EntityListHandler对象
|
||||
* @return EntityListHandler对象
|
||||
*/
|
||||
public static ValueListHandler create() {
|
||||
return new ValueListHandler();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<List<Object>> handle(ResultSet rs) throws SQLException {
|
||||
final ArrayList<List<Object>> result = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
result.add(HandleHelper.handleRowToList(rs));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -81,6 +81,7 @@ public class CRUDTest {
|
||||
@Test
|
||||
public void findInTest() throws SQLException {
|
||||
List<Entity> results = db.findAll(Entity.create("user").set("id", "in 1,2,3"));
|
||||
Console.log(results);
|
||||
Assert.assertEquals(2, results.size());
|
||||
}
|
||||
|
||||
@ -123,13 +124,13 @@ public class CRUDTest {
|
||||
Long id = db.insertForGeneratedKey(Entity.create("user").set("name", "unitTestUser").set("age", 66));
|
||||
Assert.assertTrue(id > 0);
|
||||
Entity result = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(66, (int) result.getInt("age"));
|
||||
Assert.assertSame(66, result.getInt("age"));
|
||||
|
||||
// 改
|
||||
int update = db.update(Entity.create().set("age", 88), Entity.create("user").set("name", "unitTestUser"));
|
||||
Assert.assertTrue(update > 0);
|
||||
Entity result2 = db.get("user", "name", "unitTestUser");
|
||||
Assert.assertSame(88, (int) result2.getInt("age"));
|
||||
Assert.assertSame(88, result2.getInt("age"));
|
||||
|
||||
// 删
|
||||
int del = db.del("user", "name", "unitTestUser");
|
||||
|
Loading…
Reference in New Issue
Block a user