mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!729 修复Convert#toMap默认转成HashMap的问题,关联issue#I5IG1F
Merge pull request !729 from 问北/v5-dev
This commit is contained in:
commit
d4d8522cd4
@ -601,7 +601,7 @@ public class Convert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为Map
|
* 转换为Map,若value原本就是Map,则转为原始类型,若不是则默认转为HashMap
|
||||||
*
|
*
|
||||||
* @param <K> 键类型
|
* @param <K> 键类型
|
||||||
* @param <V> 值类型
|
* @param <V> 值类型
|
||||||
@ -613,7 +613,27 @@ public class Convert {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> valueType, Object value) {
|
public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> valueType, Object value) {
|
||||||
return (Map<K, V>) new MapConverter(HashMap.class, keyType, valueType).convert(value, null);
|
if (value instanceof Map) {
|
||||||
|
return toMap((Class<? extends Map>) value.getClass(), keyType, valueType, value);
|
||||||
|
} else {
|
||||||
|
return toMap(HashMap.class, keyType, valueType, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换为Map
|
||||||
|
*
|
||||||
|
* @param mapType 转后的具体Map类型
|
||||||
|
* @param <K> 键类型
|
||||||
|
* @param <V> 值类型
|
||||||
|
* @param keyType 键类型
|
||||||
|
* @param valueType 值类型
|
||||||
|
* @param value 被转换的值
|
||||||
|
* @return {@link Map}
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <K, V> Map<K, V> toMap(Class<? extends Map> mapType, Class<K> keyType, Class<V> valueType, Object value) {
|
||||||
|
return (Map<K, V>) new MapConverter(mapType, keyType, valueType).convert(value, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,7 @@ package cn.hutool.core.convert;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtilTest.SubPerson;
|
import cn.hutool.core.bean.BeanUtilTest.SubPerson;
|
||||||
import cn.hutool.core.lang.TypeReference;
|
import cn.hutool.core.lang.TypeReference;
|
||||||
|
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -67,7 +68,31 @@ public class ConvertToBeanTest {
|
|||||||
Assert.assertEquals("3", map2.get("key3"));
|
Assert.assertEquals("3", map2.get("key3"));
|
||||||
Assert.assertEquals("4", map2.get("key4"));
|
Assert.assertEquals("4", map2.get("key4"));
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
public void mapToMapWithSelfTypeTest() {
|
||||||
|
CaseInsensitiveMap<String, Integer> caseInsensitiveMap = new CaseInsensitiveMap<>();
|
||||||
|
caseInsensitiveMap.put("jerry", 1);
|
||||||
|
caseInsensitiveMap.put("Jerry", 2);
|
||||||
|
caseInsensitiveMap.put("tom", 3);
|
||||||
|
|
||||||
|
Map<String, String> map = Convert.toMap(String.class, String.class, caseInsensitiveMap);
|
||||||
|
Assert.assertEquals("2", map.get("jerry"));
|
||||||
|
Assert.assertEquals("2", map.get("Jerry"));
|
||||||
|
Assert.assertEquals("3", map.get("tom"));
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void beanToSpecifyMapTest() {
|
||||||
|
SubPerson person = new SubPerson();
|
||||||
|
person.setAge(14);
|
||||||
|
person.setOpenid("11213232");
|
||||||
|
person.setName("测试A11");
|
||||||
|
person.setSubName("sub名字");
|
||||||
|
|
||||||
|
Map<String, String> map = Convert.toMap(LinkedHashMap.class, String.class, String.class, person);
|
||||||
|
Assert.assertEquals("测试A11", map.get("name"));
|
||||||
|
Assert.assertEquals("14", map.get("age"));
|
||||||
|
Assert.assertEquals("11213232", map.get("openid"));
|
||||||
|
}
|
||||||
@Test
|
@Test
|
||||||
public void mapToBeanTest() {
|
public void mapToBeanTest() {
|
||||||
HashMap<String, Object> map = new HashMap<>();
|
HashMap<String, Object> map = new HashMap<>();
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
package cn.hutool.db;
|
package cn.hutool.db;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* H2数据库单元测试
|
* H2数据库单元测试
|
||||||
@ -34,6 +37,17 @@ public class H2Test {
|
|||||||
Assert.assertEquals(4, query.size());
|
Assert.assertEquals(4, query.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void pageTest() throws SQLException {
|
||||||
|
String sql = "select * from test where a = @a and b = :b";
|
||||||
|
Map<String, Object> paramMap = MapUtil.builder(new CaseInsensitiveMap<String, Object>())
|
||||||
|
.put("A", 3)
|
||||||
|
.put("b", 31)
|
||||||
|
.build();
|
||||||
|
List<Entity> query = Db.use(DS_GROUP_NAME).page(sql, Page.of(0, 3), paramMap);
|
||||||
|
Assert.assertEquals(1, query.size());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findTest() throws SQLException {
|
public void findTest() throws SQLException {
|
||||||
List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
List<Entity> query = Db.use(DS_GROUP_NAME).find(Entity.create("test"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user