add setCaseInsensitive

This commit is contained in:
Looly 2020-03-16 23:19:04 +08:00
parent 3a579171c1
commit 87167a07d0
27 changed files with 296 additions and 217 deletions

View File

@ -11,6 +11,7 @@
* 【core 】 添加获取class当前文件夹名称方法pr#106@Gitee * 【core 】 添加获取class当前文件夹名称方法pr#106@Gitee
* 【core 】 BooleanUtil中重载歧义修正修改了包装参数的方法名issue#I1BSK8@Gitee * 【core 】 BooleanUtil中重载歧义修正修改了包装参数的方法名issue#I1BSK8@Gitee
* 【core 】 XmlUtil增加xmlToBean和beanToXml方法 * 【core 】 XmlUtil增加xmlToBean和beanToXml方法
* 【db 】 设置全局忽略大小写DbUtil.setCaseInsensitiveGlobal(true)issue#784@Github
### Bug修复 ### Bug修复
* 【core 】 修复TypeUtil无法获取泛型接口的泛型参数问题issue#I1BRFI@Gitee * 【core 】 修复TypeUtil无法获取泛型接口的泛型参数问题issue#I1BRFI@Gitee

View File

@ -1,13 +1,12 @@
package cn.hutool.core.convert.impl; package cn.hutool.core.convert.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.util.TypeUtil;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Collection; import java.util.Collection;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Converter;
import cn.hutool.core.util.TypeUtil;
/** /**
* 各种集合类转换器 * 各种集合类转换器
* *
@ -77,7 +76,7 @@ public class CollectionConverter implements Converter<Collection<?>> {
* @return 转换后的集合对象 * @return 转换后的集合对象
*/ */
protected Collection<?> convertInternal(Object value) { protected Collection<?> convertInternal(Object value) {
final Collection<Object> collection = CollectionUtil.create(TypeUtil.getClass(this.collectionType)); final Collection<Object> collection = CollUtil.create(TypeUtil.getClass(this.collectionType));
return CollUtil.addAll(collection, value, this.elementType); return CollUtil.addAll(collection, value, this.elementType);
} }
} }

View File

@ -1,5 +1,11 @@
package cn.hutool.core.exceptions; package cn.hutool.core.exceptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException; import java.lang.reflect.UndeclaredThrowableException;
@ -8,12 +14,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
/** /**
* 异常工具类 * 异常工具类
* *
@ -208,7 +208,7 @@ public class ExceptionUtil {
length = limit; length = limit;
} }
if (CollectionUtil.isNotEmpty(replaceCharToStrMap)) { if (CollUtil.isNotEmpty(replaceCharToStrMap)) {
final StringBuilder sb = StrUtil.builder(); final StringBuilder sb = StrUtil.builder();
char c; char c;
String value; String value;

View File

@ -1,5 +1,14 @@
package cn.hutool.core.io.resource; package cn.hutool.core.io.resource;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.EnumerationIter;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -8,15 +17,6 @@ import java.nio.charset.Charset;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.EnumerationIter;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
/** /**
* ClassPath资源工具类 * ClassPath资源工具类
* *
@ -134,7 +134,7 @@ public class ResourceUtil {
} catch (IOException e) { } catch (IOException e) {
throw new IORuntimeException(e); throw new IORuntimeException(e);
} }
return CollectionUtil.newArrayList(resources); return CollUtil.newArrayList(resources);
} }
/** /**

View File

@ -1,14 +1,14 @@
package cn.hutool.core.io.watch.watchers; package cn.hutool.core.io.watch.watchers;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.lang.Chain;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.WatchEvent; import java.nio.file.WatchEvent;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.lang.Chain;
/** /**
* 观察者链<br> * 观察者链<br>
* 用于加入多个观察者 * 用于加入多个观察者
@ -35,7 +35,7 @@ public class WatcherChain implements Watcher, Chain<Watcher, WatcherChain>{
* @param watchers 观察者列表 * @param watchers 观察者列表
*/ */
public WatcherChain(Watcher... watchers) { public WatcherChain(Watcher... watchers) {
chain = CollectionUtil.newArrayList(watchers); chain = CollUtil.newArrayList(watchers);
} }
@Override @Override
@ -66,6 +66,7 @@ public class WatcherChain implements Watcher, Chain<Watcher, WatcherChain>{
} }
} }
@SuppressWarnings("NullableProblems")
@Override @Override
public Iterator<Watcher> iterator() { public Iterator<Watcher> iterator() {
return this.chain.iterator(); return this.chain.iterator();

View File

@ -3,7 +3,7 @@ package cn.hutool.core.lang;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
@ -353,7 +353,7 @@ public class Assert {
* @throws IllegalArgumentException if the collection is {@code null} or has no elements * @throws IllegalArgumentException if the collection is {@code null} or has no elements
*/ */
public static <T> Collection<T> notEmpty(Collection<T> collection, String errorMsgTemplate, Object... params) throws IllegalArgumentException { public static <T> Collection<T> notEmpty(Collection<T> collection, String errorMsgTemplate, Object... params) throws IllegalArgumentException {
if (CollectionUtil.isEmpty(collection)) { if (CollUtil.isEmpty(collection)) {
throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params));
} }
return collection; return collection;
@ -392,7 +392,7 @@ public class Assert {
* @throws IllegalArgumentException if the map is {@code null} or has no entries * @throws IllegalArgumentException if the map is {@code null} or has no entries
*/ */
public static <K, V> Map<K, V> notEmpty(Map<K, V> map, String errorMsgTemplate, Object... params) throws IllegalArgumentException { public static <K, V> Map<K, V> notEmpty(Map<K, V> map, String errorMsgTemplate, Object... params) throws IllegalArgumentException {
if (CollectionUtil.isEmpty(map)) { if (CollUtil.isEmpty(map)) {
throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params)); throw new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params));
} }
return map; return map;

View File

@ -1,5 +1,10 @@
package cn.hutool.core.lang; package cn.hutool.core.lang;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.getter.BasicTypeGetter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.sql.Time; import java.sql.Time;
@ -10,11 +15,6 @@ import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.getter.BasicTypeGetter;
/** /**
* 字典对象扩充了HashMap中的方法 * 字典对象扩充了HashMap中的方法
* *
@ -231,7 +231,7 @@ public class Dict extends LinkedHashMap<String, Object> implements BasicTypeGett
* @param withoutNames 不需要去除的字段名 * @param withoutNames 不需要去除的字段名
*/ */
public <T extends Dict> void removeEqual(T dict, String... withoutNames) { public <T extends Dict> void removeEqual(T dict, String... withoutNames) {
HashSet<String> withoutSet = CollectionUtil.newHashSet(withoutNames); HashSet<String> withoutSet = CollUtil.newHashSet(withoutNames);
for (Map.Entry<String, Object> entry : dict.entrySet()) { for (Map.Entry<String, Object> entry : dict.entrySet()) {
if (withoutSet.contains(entry.getKey())) { if (withoutSet.contains(entry.getKey())) {
continue; continue;

View File

@ -1,10 +1,6 @@
package cn.hutool.core.map; package cn.hutool.core.map;
import java.util.*; import cn.hutool.core.collection.CollUtil;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Editor; import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Filter; import cn.hutool.core.lang.Filter;
@ -14,6 +10,22 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* Map相关工具类 * Map相关工具类
* *
@ -357,7 +369,7 @@ public class MapUtil {
*/ */
public static <K, V> Map<K, List<V>> toListMap(Iterable<? extends Map<K, V>> mapList) { public static <K, V> Map<K, List<V>> toListMap(Iterable<? extends Map<K, V>> mapList) {
final HashMap<K, List<V>> resultMap = new HashMap<>(); final HashMap<K, List<V>> resultMap = new HashMap<>();
if (CollectionUtil.isEmpty(mapList)) { if (CollUtil.isEmpty(mapList)) {
return resultMap; return resultMap;
} }
@ -370,7 +382,7 @@ public class MapUtil {
key = entry.getKey(); key = entry.getKey();
valueList = resultMap.get(key); valueList = resultMap.get(key);
if (null == valueList) { if (null == valueList) {
valueList = CollectionUtil.newArrayList(entry.getValue()); valueList = CollUtil.newArrayList(entry.getValue());
resultMap.put(key, valueList); resultMap.put(key, valueList);
} else { } else {
valueList.add(entry.getValue()); valueList.add(entry.getValue());
@ -425,7 +437,7 @@ public class MapUtil {
List<V> vList; List<V> vList;
int vListSize; int vListSize;
for (Entry<K, ? extends Iterable<V>> entry : listMap.entrySet()) { for (Entry<K, ? extends Iterable<V>> entry : listMap.entrySet()) {
vList = CollectionUtil.newArrayList(entry.getValue()); vList = CollUtil.newArrayList(entry.getValue());
vListSize = vList.size(); vListSize = vList.size();
if (index < vListSize) { if (index < vListSize) {
map.put(entry.getKey(), vList.get(index)); map.put(entry.getKey(), vList.get(index));

View File

@ -1,7 +1,6 @@
package cn.hutool.core.net; package cn.hutool.core.net;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
@ -350,7 +349,7 @@ public class NetUtil {
return null; return null;
} }
return CollectionUtil.addAll(new ArrayList<>(), networkInterfaces); return CollUtil.addAll(new ArrayList<>(), networkInterfaces);
} }
/** /**

View File

@ -1,5 +1,6 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.IterUtil; import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
@ -8,7 +9,14 @@ import cn.hutool.core.lang.Filter;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
/** /**
* 数组工具类 * 数组工具类
@ -931,7 +939,7 @@ public class ArrayUtil {
} }
final int size = Math.min(keys.length, values.length); final int size = Math.min(keys.length, values.length);
final Map<K, V> map = CollectionUtil.newHashMap(size, isOrder); final Map<K, V> map = CollUtil.newHashMap(size, isOrder);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
map.put(keys[i], values[i]); map.put(keys[i], values[i]);
} }
@ -2674,7 +2682,7 @@ public class ArrayUtil {
* @since 3.0.9 * @since 3.0.9
*/ */
public static <T> T[] toArray(Iterator<T> iterator, Class<T> componentType) { public static <T> T[] toArray(Iterator<T> iterator, Class<T> componentType) {
return toArray(CollectionUtil.newArrayList(iterator), componentType); return toArray(CollUtil.newArrayList(iterator), componentType);
} }
/** /**

View File

@ -1,7 +1,7 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.annotation.Alias; import cn.hutool.core.annotation.Alias;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
@ -391,7 +391,7 @@ public class ReflectUtil {
} }
} }
} else { } else {
methodList = CollectionUtil.newArrayList(methods); methodList = CollUtil.newArrayList(methods);
} }
return methodList; return methodList;
} }
@ -404,7 +404,7 @@ public class ReflectUtil {
* @return 过滤后的方法列表 * @return 过滤后的方法列表
*/ */
public static List<Method> getPublicMethods(Class<?> clazz, Method... excludeMethods) { public static List<Method> getPublicMethods(Class<?> clazz, Method... excludeMethods) {
final HashSet<Method> excludeMethodSet = CollectionUtil.newHashSet(excludeMethods); final HashSet<Method> excludeMethodSet = CollUtil.newHashSet(excludeMethods);
return getPublicMethods(clazz, method -> false == excludeMethodSet.contains(method)); return getPublicMethods(clazz, method -> false == excludeMethodSet.contains(method));
} }
@ -416,7 +416,7 @@ public class ReflectUtil {
* @return 过滤后的方法列表 * @return 过滤后的方法列表
*/ */
public static List<Method> getPublicMethods(Class<?> clazz, String... excludeMethodNames) { public static List<Method> getPublicMethods(Class<?> clazz, String... excludeMethodNames) {
final HashSet<String> excludeMethodNameSet = CollectionUtil.newHashSet(excludeMethodNames); final HashSet<String> excludeMethodNameSet = CollUtil.newHashSet(excludeMethodNames);
return getPublicMethods(clazz, method -> false == excludeMethodNameSet.contains(method.getName())); return getPublicMethods(clazz, method -> false == excludeMethodNameSet.contains(method.getName()));
} }

View File

@ -31,7 +31,7 @@ public class CollUtilTest {
@Test @Test
public void newHashSetTest() { public void newHashSetTest() {
Set<String> set = CollectionUtil.newHashSet((String[]) null); Set<String> set = CollUtil.newHashSet((String[]) null);
Assert.assertNotNull(set); Assert.assertNotNull(set);
} }
@ -41,12 +41,12 @@ public class CollUtilTest {
Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四"); Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四");
final String[] keys = v1.keySet().toArray(new String[0]); final String[] keys = v1.keySet().toArray(new String[0]);
ArrayList<Object> v1s = CollectionUtil.valuesOfKeys(v1, keys); ArrayList<Object> v1s = CollUtil.valuesOfKeys(v1, keys);
Assert.assertTrue(v1s.contains(12)); Assert.assertTrue(v1s.contains(12));
Assert.assertTrue(v1s.contains(23)); Assert.assertTrue(v1s.contains(23));
Assert.assertTrue(v1s.contains("张三")); Assert.assertTrue(v1s.contains("张三"));
ArrayList<Object> v2s = CollectionUtil.valuesOfKeys(v2, keys); ArrayList<Object> v2s = CollUtil.valuesOfKeys(v2, keys);
Assert.assertTrue(v2s.contains(15)); Assert.assertTrue(v2s.contains(15));
Assert.assertTrue(v2s.contains(13)); Assert.assertTrue(v2s.contains(13));
Assert.assertTrue(v2s.contains("李四")); Assert.assertTrue(v2s.contains("李四"));
@ -54,34 +54,34 @@ public class CollUtilTest {
@Test @Test
public void unionTest() { public void unionTest() {
ArrayList<String> list1 = CollectionUtil.newArrayList("a", "b", "b", "c", "d", "x"); ArrayList<String> list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x");
ArrayList<String> list2 = CollectionUtil.newArrayList("a", "b", "b", "b", "c", "d"); ArrayList<String> list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d");
Collection<String> union = CollectionUtil.union(list1, list2); Collection<String> union = CollUtil.union(list1, list2);
Assert.assertEquals(3, CollectionUtil.count(union, t -> t.equals("b"))); Assert.assertEquals(3, CollUtil.count(union, t -> t.equals("b")));
} }
@Test @Test
public void intersectionTest() { public void intersectionTest() {
ArrayList<String> list1 = CollectionUtil.newArrayList("a", "b", "b", "c", "d", "x"); ArrayList<String> list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x");
ArrayList<String> list2 = CollectionUtil.newArrayList("a", "b", "b", "b", "c", "d"); ArrayList<String> list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d");
Collection<String> union = CollectionUtil.intersection(list1, list2); Collection<String> union = CollUtil.intersection(list1, list2);
Assert.assertEquals(2, CollectionUtil.count(union, t -> t.equals("b"))); Assert.assertEquals(2, CollUtil.count(union, t -> t.equals("b")));
} }
@Test @Test
public void disjunctionTest() { public void disjunctionTest() {
ArrayList<String> list1 = CollectionUtil.newArrayList("a", "b", "b", "c", "d", "x"); ArrayList<String> list1 = CollUtil.newArrayList("a", "b", "b", "c", "d", "x");
ArrayList<String> list2 = CollectionUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); ArrayList<String> list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2");
Collection<String> disjunction = CollectionUtil.disjunction(list1, list2); Collection<String> disjunction = CollUtil.disjunction(list1, list2);
Assert.assertTrue(disjunction.contains("b")); Assert.assertTrue(disjunction.contains("b"));
Assert.assertTrue(disjunction.contains("x2")); Assert.assertTrue(disjunction.contains("x2"));
Assert.assertTrue(disjunction.contains("x")); Assert.assertTrue(disjunction.contains("x"));
Collection<String> disjunction2 = CollectionUtil.disjunction(list2, list1); Collection<String> disjunction2 = CollUtil.disjunction(list2, list1);
Assert.assertTrue(disjunction2.contains("b")); Assert.assertTrue(disjunction2.contains("b"));
Assert.assertTrue(disjunction2.contains("x2")); Assert.assertTrue(disjunction2.contains("x2"));
Assert.assertTrue(disjunction2.contains("x")); Assert.assertTrue(disjunction2.contains("x"));
@ -90,29 +90,29 @@ public class CollUtilTest {
@Test @Test
public void disjunctionTest2() { public void disjunctionTest2() {
// 任意一个集合为空差集为另一个集合 // 任意一个集合为空差集为另一个集合
ArrayList<String> list1 = CollectionUtil.newArrayList(); ArrayList<String> list1 = CollUtil.newArrayList();
ArrayList<String> list2 = CollectionUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2"); ArrayList<String> list2 = CollUtil.newArrayList("a", "b", "b", "b", "c", "d", "x2");
Collection<String> disjunction = CollectionUtil.disjunction(list1, list2); Collection<String> disjunction = CollUtil.disjunction(list1, list2);
Assert.assertEquals(list2, disjunction); Assert.assertEquals(list2, disjunction);
Collection<String> disjunction2 = CollectionUtil.disjunction(list2, list1); Collection<String> disjunction2 = CollUtil.disjunction(list2, list1);
Assert.assertEquals(list2, disjunction2); Assert.assertEquals(list2, disjunction2);
} }
@Test @Test
public void disjunctionTest3() { public void disjunctionTest3() {
// 无交集下返回共同的元素 // 无交集下返回共同的元素
ArrayList<String> list1 = CollectionUtil.newArrayList("1", "2", "3"); ArrayList<String> list1 = CollUtil.newArrayList("1", "2", "3");
ArrayList<String> list2 = CollectionUtil.newArrayList("a", "b", "c"); ArrayList<String> list2 = CollUtil.newArrayList("a", "b", "c");
Collection<String> disjunction = CollectionUtil.disjunction(list1, list2); Collection<String> disjunction = CollUtil.disjunction(list1, list2);
Assert.assertTrue(disjunction.contains("1")); Assert.assertTrue(disjunction.contains("1"));
Assert.assertTrue(disjunction.contains("2")); Assert.assertTrue(disjunction.contains("2"));
Assert.assertTrue(disjunction.contains("3")); Assert.assertTrue(disjunction.contains("3"));
Assert.assertTrue(disjunction.contains("a")); Assert.assertTrue(disjunction.contains("a"));
Assert.assertTrue(disjunction.contains("b")); Assert.assertTrue(disjunction.contains("b"));
Assert.assertTrue(disjunction.contains("c")); Assert.assertTrue(disjunction.contains("c"));
Collection<String> disjunction2 = CollectionUtil.disjunction(list2, list1); Collection<String> disjunction2 = CollUtil.disjunction(list2, list1);
Assert.assertTrue(disjunction2.contains("1")); Assert.assertTrue(disjunction2.contains("1"));
Assert.assertTrue(disjunction2.contains("2")); Assert.assertTrue(disjunction2.contains("2"));
Assert.assertTrue(disjunction2.contains("3")); Assert.assertTrue(disjunction2.contains("3"));
@ -132,13 +132,13 @@ public class CollUtilTest {
map2.put("c", "值3"); map2.put("c", "值3");
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
ArrayList<HashMap<String, String>> list = CollectionUtil.newArrayList(map1, map2); ArrayList<HashMap<String, String>> list = CollUtil.newArrayList(map1, map2);
Map<String, List<String>> map = CollectionUtil.toListMap(list); Map<String, List<String>> map = CollUtil.toListMap(list);
Assert.assertEquals("值1", map.get("a").get(0)); Assert.assertEquals("值1", map.get("a").get(0));
Assert.assertEquals("值2", map.get("a").get(1)); Assert.assertEquals("值2", map.get("a").get(1));
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
List<Map<String, String>> listMap = CollectionUtil.toMapList(map); List<Map<String, String>> listMap = CollUtil.toMapList(map);
Assert.assertEquals("值1", listMap.get(0).get("a")); Assert.assertEquals("值1", listMap.get(0).get("a"));
Assert.assertEquals("值2", listMap.get(1).get("a")); Assert.assertEquals("值2", listMap.get(1).get("a"));
} }
@ -147,17 +147,17 @@ public class CollUtilTest {
public void getFieldValuesTest() { public void getFieldValuesTest() {
Dict v1 = Dict.create().set("id", 12).set("name", "张三").set("age", 23); Dict v1 = Dict.create().set("id", 12).set("name", "张三").set("age", 23);
Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四"); Dict v2 = Dict.create().set("age", 13).set("id", 15).set("name", "李四");
ArrayList<Dict> list = CollectionUtil.newArrayList(v1, v2); ArrayList<Dict> list = CollUtil.newArrayList(v1, v2);
List<Object> fieldValues = CollectionUtil.getFieldValues(list, "name"); List<Object> fieldValues = CollUtil.getFieldValues(list, "name");
Assert.assertEquals("张三", fieldValues.get(0)); Assert.assertEquals("张三", fieldValues.get(0));
Assert.assertEquals("李四", fieldValues.get(1)); Assert.assertEquals("李四", fieldValues.get(1));
} }
@Test @Test
public void splitTest() { public void splitTest() {
final ArrayList<Integer> list = CollectionUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9); final ArrayList<Integer> list = CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<List<Integer>> split = CollectionUtil.split(list, 3); List<List<Integer>> split = CollUtil.split(list, 3);
Assert.assertEquals(3, split.size()); Assert.assertEquals(3, split.size());
Assert.assertEquals(3, split.get(0).size()); Assert.assertEquals(3, split.get(0).size());
} }
@ -170,7 +170,7 @@ public class CollUtilTest {
map.put("c", "3"); map.put("c", "3");
final String[] result = new String[1]; final String[] result = new String[1];
CollectionUtil.forEach(map, (key, value, index) -> { CollUtil.forEach(map, (key, value, index) -> {
if (key.equals("a")) { if (key.equals("a")) {
result[0] = value; result[0] = value;
} }
@ -234,10 +234,10 @@ public class CollUtilTest {
@Test @Test
public void groupTest() { public void groupTest() {
List<String> list = CollUtil.newArrayList("1", "2", "3", "4", "5", "6"); List<String> list = CollUtil.newArrayList("1", "2", "3", "4", "5", "6");
List<List<String>> group = CollectionUtil.group(list, null); List<List<String>> group = CollUtil.group(list, null);
Assert.assertTrue(group.size() > 0); Assert.assertTrue(group.size() > 0);
List<List<String>> group2 = CollectionUtil.group(list, t -> { List<List<String>> group2 = CollUtil.group(list, t -> {
// 按照奇数偶数分类 // 按照奇数偶数分类
return Integer.parseInt(t) % 2; return Integer.parseInt(t) % 2;
}); });

View File

@ -1,7 +1,10 @@
package cn.hutool.core.lang; package cn.hutool.core.lang;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.*; import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -14,7 +17,7 @@ import java.util.List;
*/ */
public class TreeTest { public class TreeTest {
// 模拟数据 // 模拟数据
static List<TreeNode<String>> nodeList = CollectionUtil.newArrayList(); static List<TreeNode<String>> nodeList = CollUtil.newArrayList();
static { static {
// 模拟数据 // 模拟数据

View File

@ -1,27 +1,26 @@
package cn.hutool.core.util; package cn.hutool.core.util;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
public class RandomUtilTest { public class RandomUtilTest {
@Test @Test
public void randomEleSetTest(){ public void randomEleSetTest(){
Set<Integer> set = RandomUtil.randomEleSet(CollectionUtil.newArrayList(1, 2, 3, 4, 5, 6), 2); Set<Integer> set = RandomUtil.randomEleSet(CollUtil.newArrayList(1, 2, 3, 4, 5, 6), 2);
Assert.assertEquals(set.size(), 2); Assert.assertEquals(set.size(), 2);
} }
@Test @Test
public void randomElesTest(){ public void randomElesTest(){
List<Integer> result = RandomUtil.randomEles(CollectionUtil.newArrayList(1, 2, 3, 4, 5, 6), 2); List<Integer> result = RandomUtil.randomEles(CollUtil.newArrayList(1, 2, 3, 4, 5, 6), 2);
Assert.assertEquals(result.size(), 2); Assert.assertEquals(result.size(), 2);
} }

View File

@ -1,14 +1,6 @@
package cn.hutool.cron; package cn.hutool.cron;
import java.io.Serializable; import cn.hutool.core.collection.CollUtil;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.thread.ExecutorBuilder; import cn.hutool.core.thread.ExecutorBuilder;
import cn.hutool.core.thread.ThreadFactoryBuilder; import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.CharUtil;
@ -23,6 +15,14 @@ import cn.hutool.cron.task.Task;
import cn.hutool.log.StaticLog; import cn.hutool.log.StaticLog;
import cn.hutool.setting.Setting; import cn.hutool.setting.Setting;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/** /**
* 任务调度器<br> * 任务调度器<br>
* *
@ -182,7 +182,7 @@ public class Scheduler implements Serializable {
* @return this * @return this
*/ */
public Scheduler schedule(Setting cronSetting) { public Scheduler schedule(Setting cronSetting) {
if (CollectionUtil.isNotEmpty(cronSetting)) { if (CollUtil.isNotEmpty(cronSetting)) {
String group; String group;
for (Entry<String, LinkedHashMap<String, String>> groupedEntry : cronSetting.getGroupedMap().entrySet()) { for (Entry<String, LinkedHashMap<String, String>> groupedEntry : cronSetting.getGroupedMap().entrySet()) {
group = groupedEntry.getKey(); group = groupedEntry.getKey();

View File

@ -1,9 +1,6 @@
package cn.hutool.cron.pattern.matcher; package cn.hutool.cron.pattern.matcher;
import java.util.ArrayList; import cn.hutool.core.collection.CollUtil;
import java.util.List;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.cron.CronException; import cn.hutool.cron.CronException;
@ -11,6 +8,9 @@ import cn.hutool.cron.pattern.parser.DayOfMonthValueParser;
import cn.hutool.cron.pattern.parser.ValueParser; import cn.hutool.cron.pattern.parser.ValueParser;
import cn.hutool.cron.pattern.parser.YearValueParser; import cn.hutool.cron.pattern.parser.YearValueParser;
import java.util.ArrayList;
import java.util.List;
/** /**
* {@link ValueMatcher} 构建器用于构建表达式中每一项的匹配器 * {@link ValueMatcher} 构建器用于构建表达式中每一项的匹配器
* @author Looly * @author Looly
@ -64,7 +64,7 @@ public class ValueMatcherBuilder {
final List<String> parts = StrUtil.split(value, StrUtil.C_COMMA); final List<String> parts = StrUtil.split(value, StrUtil.C_COMMA);
for (String part : parts) { for (String part : parts) {
CollectionUtil.addAllIfNotContains(values, parseStep(part, parser)); CollUtil.addAllIfNotContains(values, parseStep(part, parser));
} }
return values; return values;
} }
@ -170,11 +170,7 @@ public class ValueMatcherBuilder {
NumberUtil.appendRange(v1, parser.getMax(), step, results); NumberUtil.appendRange(v1, parser.getMax(), step, results);
NumberUtil.appendRange(parser.getMin(), v2, step, results); NumberUtil.appendRange(parser.getMin(), v2, step, results);
} else {// v1 == v2此时与单值模式一致 } else {// v1 == v2此时与单值模式一致
if(step > 0) {//类似 20/2的形式 NumberUtil.appendRange(v1, parser.getMax(), step, results);
NumberUtil.appendRange(v1, parser.getMax(), step, results);
}else {
results.add(v1);
}
} }
} else { } else {
throw new CronException("Invalid syntax of field: [{}]", value); throw new CronException("Invalid syntax of field: [{}]", value);

View File

@ -1,15 +1,6 @@
package cn.hutool.db; package cn.hutool.db;
import java.io.Serializable; import cn.hutool.core.collection.CollUtil;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.db.dialect.Dialect; import cn.hutool.db.dialect.Dialect;
import cn.hutool.db.handler.BeanListHandler; import cn.hutool.db.handler.BeanListHandler;
import cn.hutool.db.handler.EntityHandler; import cn.hutool.db.handler.EntityHandler;
@ -24,6 +15,14 @@ import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.db.sql.SqlUtil; import cn.hutool.db.sql.SqlUtil;
import cn.hutool.db.sql.Wrapper; import cn.hutool.db.sql.Wrapper;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/** /**
* 抽象数据库操作类<br> * 抽象数据库操作类<br>
* 通过给定的数据源执行给定SQL或者给定数据源和方言执行相应的CRUD操作<br> * 通过给定的数据源执行给定SQL或者给定数据源和方言执行相应的CRUD操作<br>
@ -39,6 +38,10 @@ public abstract class AbstractDb implements Serializable {
* 是否支持事务 * 是否支持事务
*/ */
protected Boolean isSupportTransaction = null; protected Boolean isSupportTransaction = null;
/**
* 是否大小写不敏感默认大小写不敏感
*/
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
protected SqlConnRunner runner; protected SqlConnRunner runner;
// ------------------------------------------------------- Constructor start // ------------------------------------------------------- Constructor start
@ -81,7 +84,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.1.1 * @since 3.1.1
*/ */
public List<Entity> query(String sql, Map<String, Object> params) throws SQLException { public List<Entity> query(String sql, Map<String, Object> params) throws SQLException {
return query(sql, new EntityListHandler(), params); return query(sql, new EntityListHandler(this.caseInsensitive), params);
} }
/** /**
@ -94,7 +97,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.1.1 * @since 3.1.1
*/ */
public List<Entity> query(String sql, Object... params) throws SQLException { public List<Entity> query(String sql, Object... params) throws SQLException {
return query(sql, new EntityListHandler(), params); return query(sql, new EntityListHandler(this.caseInsensitive), params);
} }
/** /**
@ -121,7 +124,7 @@ public abstract class AbstractDb implements Serializable {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public Entity queryOne(String sql, Object... params) throws SQLException { public Entity queryOne(String sql, Object... params) throws SQLException {
return query(sql, new EntityHandler(), params); return query(sql, new EntityHandler(this.caseInsensitive), params);
} }
/** /**
@ -171,9 +174,9 @@ public abstract class AbstractDb implements Serializable {
/** /**
* 支持占位符的查询例如select * from table where field1=:name1 * 支持占位符的查询例如select * from table where field1=:name1
* *
* @param <T> 结果集需要处理的对象类型 * @param <T> 结果集需要处理的对象类型
* @param sql 查询语句使用参数名占位符例如:name * @param sql 查询语句使用参数名占位符例如:name
* @param rsh 结果集处理对象 * @param rsh 结果集处理对象
* @param paramMap 参数 * @param paramMap 参数
* @return 结果对象 * @return 结果对象
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
@ -429,7 +432,7 @@ public abstract class AbstractDb implements Serializable {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public Entity get(Entity where) throws SQLException { public Entity get(Entity where) throws SQLException {
return find(where.getFieldNames(), where, new EntityHandler()); return find(where.getFieldNames(), where, new EntityHandler(this.caseInsensitive));
} }
// ------------------------------------------------------------- Get end // ------------------------------------------------------------- Get end
@ -466,7 +469,7 @@ public abstract class AbstractDb implements Serializable {
* @since 4.5.16 * @since 4.5.16
*/ */
public List<Entity> find(Collection<String> fields, Entity where) throws SQLException { public List<Entity> find(Collection<String> fields, Entity where) throws SQLException {
return find(fields, where, EntityListHandler.create()); return find(fields, where, new EntityListHandler(this.caseInsensitive));
} }
/** /**
@ -502,7 +505,7 @@ public abstract class AbstractDb implements Serializable {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public <T> T find(Entity where, RsHandler<T> rsh, String... fields) throws SQLException { public <T> T find(Entity where, RsHandler<T> rsh, String... fields) throws SQLException {
return find(CollectionUtil.newArrayList(fields), where, rsh); return find(CollUtil.newArrayList(fields), where, rsh);
} }
/** /**
@ -515,7 +518,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.2.1 * @since 3.2.1
*/ */
public List<Entity> find(Entity where) throws SQLException { public List<Entity> find(Entity where) throws SQLException {
return find(where.getFieldNames(), where, EntityListHandler.create()); return find(where.getFieldNames(), where, new EntityListHandler(this.caseInsensitive));
} }
/** /**
@ -549,8 +552,8 @@ public abstract class AbstractDb implements Serializable {
* 查询数据列表返回所有字段<br> * 查询数据列表返回所有字段<br>
* 查询条件为多个key value对表示默认key = value如果使用其它条件可以使用where.put("key", " &gt; 1")value也可以传Condition对象key被忽略 * 查询条件为多个key value对表示默认key = value如果使用其它条件可以使用where.put("key", " &gt; 1")value也可以传Condition对象key被忽略
* *
* @param <T> Bean类型 * @param <T> Bean类型
* @param where 条件实体类包含表名 * @param where 条件实体类包含表名
* @param beanClass 返回的对象类型 * @param beanClass 返回的对象类型
* @return 数据对象列表 * @return 数据对象列表
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
@ -595,7 +598,7 @@ public abstract class AbstractDb implements Serializable {
*/ */
public List<Entity> findBy(String tableName, Condition... wheres) throws SQLException { public List<Entity> findBy(String tableName, Condition... wheres) throws SQLException {
final Query query = new Query(wheres, tableName); final Query query = new Query(wheres, tableName);
return find(query, EntityListHandler.create()); return find(query, new EntityListHandler(this.caseInsensitive));
} }
/** /**
@ -695,7 +698,7 @@ public abstract class AbstractDb implements Serializable {
* @since 3.2.2 * @since 3.2.2
*/ */
public List<Entity> pageForEntityList(Entity where, Page page) throws SQLException { public List<Entity> pageForEntityList(Entity where, Page page) throws SQLException {
return page(where, page, EntityListHandler.create()); return page(where, page, new EntityListHandler(this.caseInsensitive));
} }
/** /**
@ -808,6 +811,17 @@ public abstract class AbstractDb implements Serializable {
// ---------------------------------------------------------------------------- Getters and Setters start // ---------------------------------------------------------------------------- Getters and Setters start
/**
* 设置是否在结果中忽略大小写<br>
* 如果忽略则在Entity中调用getXXX时字段值忽略大小写默认忽略
*
* @param caseInsensitive 否在结果中忽略大小写
* @since 5.2.4
*/
public void setCaseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
}
/** /**
* 获取{@link SqlConnRunner} * 获取{@link SqlConnRunner}
* *

View File

@ -1,16 +1,13 @@
package cn.hutool.db; package cn.hutool.db;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import javax.sql.DataSource;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.db.ds.DSFactory; import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.handler.EntityHandler;
import cn.hutool.db.handler.EntityListHandler; import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
/** /**
* 数据访问层模板<br> * 数据访问层模板<br>
@ -189,7 +186,7 @@ public class DaoTemplate {
} }
final Entity where = Entity.create(tableName).set(primaryKeyField, pk); final Entity where = Entity.create(tableName).set(primaryKeyField, pk);
final Entity record = (Entity) entity.clone(); final Entity record = entity.clone();
record.remove(primaryKeyField); record.remove(primaryKeyField);
return db.update(record, where); return db.update(record, where);
@ -241,7 +238,7 @@ public class DaoTemplate {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public Entity get(Entity where) throws SQLException { public Entity get(Entity where) throws SQLException {
return db.find(null, fixEntity(where), new EntityHandler()); return db.get(fixEntity(where));
} }
//------------------------------------------------------------- Get end //------------------------------------------------------------- Get end
@ -276,7 +273,7 @@ public class DaoTemplate {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public List<Entity> find(Entity where) throws SQLException { public List<Entity> find(Entity where) throws SQLException {
return db.find(null, fixEntity(where), new EntityListHandler()); return db.find(null, fixEntity(where));
} }
/** /**
@ -294,7 +291,7 @@ public class DaoTemplate {
if(false == "select".equals(selectKeyword)){ if(false == "select".equals(selectKeyword)){
sql = "SELECT * FROM " + this.tableName + " " + sql; sql = "SELECT * FROM " + this.tableName + " " + sql;
} }
return db.query(sql, new EntityListHandler(), params); return db.query(sql, params);
} }
/** /**

View File

@ -24,16 +24,20 @@ import cn.hutool.setting.Setting;
/** /**
* 数据库操作工具类 * 数据库操作工具类
* *
* @author Luxiaolei * @author Luxiaolei
*
*/ */
public final class DbUtil { public final class DbUtil {
private final static Log log = LogFactory.get(); private final static Log log = LogFactory.get();
/**
* 是否大小写不敏感默认大小写不敏感
*/
protected static boolean caseInsensitiveGlobal = true;
/** /**
* 实例化一个新的SQL运行对象 * 实例化一个新的SQL运行对象
* *
* @param dialect 数据源 * @param dialect 数据源
* @return SQL执行类 * @return SQL执行类
*/ */
@ -43,7 +47,7 @@ public final class DbUtil {
/** /**
* 实例化一个新的SQL运行对象 * 实例化一个新的SQL运行对象
* *
* @param ds 数据源 * @param ds 数据源
* @return SQL执行类 * @return SQL执行类
*/ */
@ -53,7 +57,7 @@ public final class DbUtil {
/** /**
* 实例化一个新的SQL运行对象 * 实例化一个新的SQL运行对象
* *
* @param conn 数据库连接对象 * @param conn 数据库连接对象
* @return SQL执行类 * @return SQL执行类
*/ */
@ -63,7 +67,7 @@ public final class DbUtil {
/** /**
* 实例化一个新的SQL运行对象使用默认数据源 * 实例化一个新的SQL运行对象使用默认数据源
* *
* @return SQL执行类 * @return SQL执行类
* @deprecated 请使用 {@link #use()} * @deprecated 请使用 {@link #use()}
*/ */
@ -74,7 +78,7 @@ public final class DbUtil {
/** /**
* 实例化一个新的SQL运行对象 * 实例化一个新的SQL运行对象
* *
* @param ds 数据源 * @param ds 数据源
* @return SQL执行类 * @return SQL执行类
* @deprecated 请使用 {@link #use(DataSource)} * @deprecated 请使用 {@link #use(DataSource)}
@ -86,8 +90,8 @@ public final class DbUtil {
/** /**
* 实例化一个新的SQL运行对象 * 实例化一个新的SQL运行对象
* *
* @param ds 数据源 * @param ds 数据源
* @param dialect SQL方言 * @param dialect SQL方言
* @return SQL执行类 * @return SQL执行类
* @deprecated 请使用 {@link #use(DataSource, Dialect)} * @deprecated 请使用 {@link #use(DataSource, Dialect)}
@ -99,7 +103,7 @@ public final class DbUtil {
/** /**
* 实例化一个新的Db使用默认数据源 * 实例化一个新的Db使用默认数据源
* *
* @return SQL执行类 * @return SQL执行类
*/ */
public static Db use() { public static Db use() {
@ -108,7 +112,7 @@ public final class DbUtil {
/** /**
* 实例化一个新的Db对象 * 实例化一个新的Db对象
* *
* @param ds 数据源 * @param ds 数据源
* @return SQL执行类 * @return SQL执行类
*/ */
@ -118,8 +122,8 @@ public final class DbUtil {
/** /**
* 实例化一个新的SQL运行对象 * 实例化一个新的SQL运行对象
* *
* @param ds 数据源 * @param ds 数据源
* @param dialect SQL方言 * @param dialect SQL方言
* @return SQL执行类 * @return SQL执行类
*/ */
@ -129,7 +133,7 @@ public final class DbUtil {
/** /**
* 新建数据库会话使用默认数据源 * 新建数据库会话使用默认数据源
* *
* @return 数据库会话 * @return 数据库会话
*/ */
public static Session newSession() { public static Session newSession() {
@ -138,7 +142,7 @@ public final class DbUtil {
/** /**
* 新建数据库会话 * 新建数据库会话
* *
* @param ds 数据源 * @param ds 数据源
* @return 数据库会话 * @return 数据库会话
*/ */
@ -149,7 +153,7 @@ public final class DbUtil {
/** /**
* 连续关闭一系列的SQL相关对象<br> * 连续关闭一系列的SQL相关对象<br>
* 这些对象必须按照顺序关闭否则会出错 * 这些对象必须按照顺序关闭否则会出错
* *
* @param objsToClose 需要关闭的对象 * @param objsToClose 需要关闭的对象
*/ */
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
@ -183,7 +187,7 @@ public final class DbUtil {
/** /**
* 获得默认数据源 * 获得默认数据源
* *
* @return 默认数据源 * @return 默认数据源
*/ */
public static DataSource getDs() { public static DataSource getDs() {
@ -192,7 +196,7 @@ public final class DbUtil {
/** /**
* 获取指定分组的数据源 * 获取指定分组的数据源
* *
* @param group 分组 * @param group 分组
* @return 数据源 * @return 数据源
*/ */
@ -202,7 +206,7 @@ public final class DbUtil {
/** /**
* 获得JNDI数据源 * 获得JNDI数据源
* *
* @param jndiName JNDI名称 * @param jndiName JNDI名称
* @return 数据源 * @return 数据源
*/ */
@ -217,7 +221,7 @@ public final class DbUtil {
/** /**
* 获得JNDI数据源 * 获得JNDI数据源
* *
* @param jndiName JNDI名称 * @param jndiName JNDI名称
* @return 数据源 * @return 数据源
*/ */
@ -231,6 +235,7 @@ public final class DbUtil {
/** /**
* 从配置文件中读取SQL打印选项 * 从配置文件中读取SQL打印选项
*
* @param setting 配置文件 * @param setting 配置文件
* @since 4.1.7 * @since 4.1.7
*/ */
@ -250,14 +255,25 @@ public final class DbUtil {
/** /**
* 设置全局配置是否通过debug日志显示SQL * 设置全局配置是否通过debug日志显示SQL
* *
* @param isShowSql 是否显示SQL * @param isShowSql 是否显示SQL
* @param isFormatSql 是否格式化显示的SQL * @param isFormatSql 是否格式化显示的SQL
* @param isShowParams 是否打印参数 * @param isShowParams 是否打印参数
* @param level SQL打印到的日志等级 * @param level SQL打印到的日志等级
* @since 4.1.7 * @since 4.1.7
*/ */
public static void setShowSqlGlobal(boolean isShowSql, boolean isFormatSql, boolean isShowParams, Level level) { public static void setShowSqlGlobal(boolean isShowSql, boolean isFormatSql, boolean isShowParams, Level level) {
SqlLog.INSTANCE.init(isShowSql, isFormatSql, isShowParams, level); SqlLog.INSTANCE.init(isShowSql, isFormatSql, isShowParams, level);
} }
/**
* 设置全局是否在结果中忽略大小写<br>
* 如果忽略则在Entity中调用getXXX时字段值忽略大小写默认忽略
*
* @param caseInsensitive 否在结果中忽略大小写
* @since 5.2.4
*/
public static void setCaseInsensitiveGlobal(boolean caseInsensitive) {
caseInsensitiveGlobal = caseInsensitive;
}
} }

View File

@ -1,6 +1,6 @@
package cn.hutool.db; package cn.hutool.db;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
@ -18,6 +18,7 @@ import cn.hutool.db.sql.SqlUtil;
import cn.hutool.db.sql.Wrapper; import cn.hutool.db.sql.Wrapper;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.SQLException; import java.sql.SQLException;
@ -31,8 +32,14 @@ import java.util.List;
* *
* @author Luxiaolei * @author Luxiaolei
*/ */
public class SqlConnRunner { public class SqlConnRunner implements Serializable {
private static final long serialVersionUID = 1L;
private Dialect dialect; private Dialect dialect;
/**
* 是否大小写不敏感默认大小写不敏感
*/
protected boolean caseInsensitive = DbUtil.caseInsensitiveGlobal;
/** /**
* 实例化一个新的SQL运行对象 * 实例化一个新的SQL运行对象
@ -98,7 +105,7 @@ public class SqlConnRunner {
*/ */
public int insert(Connection conn, Entity record) throws SQLException { public int insert(Connection conn, Entity record) throws SQLException {
checkConn(conn); checkConn(conn);
if (CollectionUtil.isEmpty(record)) { if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!"); throw new SQLException("Empty entity provided!");
} }
PreparedStatement ps = null; PreparedStatement ps = null;
@ -185,7 +192,7 @@ public class SqlConnRunner {
*/ */
public List<Object> insertForGeneratedKeys(Connection conn, Entity record) throws SQLException { public List<Object> insertForGeneratedKeys(Connection conn, Entity record) throws SQLException {
checkConn(conn); checkConn(conn);
if (CollectionUtil.isEmpty(record)) { if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!"); throw new SQLException("Empty entity provided!");
} }
@ -210,7 +217,7 @@ public class SqlConnRunner {
*/ */
public Long insertForGeneratedKey(Connection conn, Entity record) throws SQLException { public Long insertForGeneratedKey(Connection conn, Entity record) throws SQLException {
checkConn(conn); checkConn(conn);
if (CollectionUtil.isEmpty(record)) { if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!"); throw new SQLException("Empty entity provided!");
} }
@ -235,7 +242,7 @@ public class SqlConnRunner {
*/ */
public int del(Connection conn, Entity where) throws SQLException { public int del(Connection conn, Entity where) throws SQLException {
checkConn(conn); checkConn(conn);
if (CollectionUtil.isEmpty(where)) { if (CollUtil.isEmpty(where)) {
//不允许做全表删除 //不允许做全表删除
throw new SQLException("Empty entity provided!"); throw new SQLException("Empty entity provided!");
} }
@ -262,10 +269,10 @@ public class SqlConnRunner {
*/ */
public int update(Connection conn, Entity record, Entity where) throws SQLException { public int update(Connection conn, Entity record, Entity where) throws SQLException {
checkConn(conn); checkConn(conn);
if (CollectionUtil.isEmpty(record)) { if (CollUtil.isEmpty(record)) {
throw new SQLException("Empty entity provided!"); throw new SQLException("Empty entity provided!");
} }
if (CollectionUtil.isEmpty(where)) { if (CollUtil.isEmpty(where)) {
//不允许做全表更新 //不允许做全表更新
throw new SQLException("Empty where provided!"); throw new SQLException("Empty where provided!");
} }
@ -342,7 +349,7 @@ public class SqlConnRunner {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public <T> T find(Connection conn, Entity where, RsHandler<T> rsh, String... fields) throws SQLException { public <T> T find(Connection conn, Entity where, RsHandler<T> rsh, String... fields) throws SQLException {
return find(conn, CollectionUtil.newArrayList(fields), where, rsh); return find(conn, CollUtil.newArrayList(fields), where, rsh);
} }
/** /**
@ -355,7 +362,7 @@ public class SqlConnRunner {
* @since 3.2.1 * @since 3.2.1
*/ */
public List<Entity> find(Connection conn, Entity where) throws SQLException { public List<Entity> find(Connection conn, Entity where) throws SQLException {
return find(conn, where.getFieldNames(), where, EntityListHandler.create()); return find(conn, where.getFieldNames(), where, new EntityListHandler(this.caseInsensitive));
} }
/** /**
@ -367,7 +374,7 @@ public class SqlConnRunner {
* @throws SQLException SQL执行异常 * @throws SQLException SQL执行异常
*/ */
public List<Entity> findAll(Connection conn, Entity where) throws SQLException { public List<Entity> findAll(Connection conn, Entity where) throws SQLException {
return find(conn, where, EntityListHandler.create()); return find(conn, where, new EntityListHandler(this.caseInsensitive));
} }
/** /**
@ -505,7 +512,7 @@ public class SqlConnRunner {
checkConn(conn); checkConn(conn);
final int count = count(conn, where); final int count = count(conn, where);
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<>(page, numPerPage, count)); final PageResultHandler pageResultHandler = new PageResultHandler(new PageResult<>(page, numPerPage, count), this.caseInsensitive);
return this.page(conn, fields, where, page, numPerPage, pageResultHandler); return this.page(conn, fields, where, page, numPerPage, pageResultHandler);
} }
@ -525,14 +532,14 @@ public class SqlConnRunner {
//查询全部 //查询全部
if (null == page) { if (null == page) {
List<Entity> entityList = this.find(conn, fields, where, new EntityListHandler()); List<Entity> entityList = this.find(conn, fields, where, new EntityListHandler(DbUtil.caseInsensitiveGlobal));
final PageResult<Entity> pageResult = new PageResult<>(0, entityList.size(), entityList.size()); final PageResult<Entity> pageResult = new PageResult<>(0, entityList.size(), entityList.size());
pageResult.addAll(entityList); pageResult.addAll(entityList);
return pageResult; return pageResult;
} }
final int count = count(conn, where); final int count = count(conn, where);
PageResultHandler pageResultHandler = PageResultHandler.create(new PageResult<>(page.getPageNumber(), page.getPageSize(), count)); PageResultHandler pageResultHandler = new PageResultHandler(new PageResult<>(page.getPageNumber(), page.getPageSize(), count), this.caseInsensitive);
return this.page(conn, fields, where, page, pageResultHandler); return this.page(conn, fields, where, page, pageResultHandler);
} }
@ -553,6 +560,17 @@ public class SqlConnRunner {
//---------------------------------------------------------------------------- Getters and Setters end //---------------------------------------------------------------------------- Getters and Setters end
/**
* 设置是否在结果中忽略大小写<br>
* 如果忽略则在Entity中调用getXXX时字段值忽略大小写默认忽略
*
* @param caseInsensitive 否在结果中忽略大小写
* @since 5.2.4
*/
public void setCaseInsensitive(boolean caseInsensitive) {
this.caseInsensitive = caseInsensitive;
}
/** /**
* @return SQL方言 * @return SQL方言
*/ */

View File

@ -1,5 +1,15 @@
package cn.hutool.db.handler; package cn.hutool.db.handler;
import cn.hutool.core.bean.BeanDesc.PropDesc;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import cn.hutool.db.Entity;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -11,16 +21,6 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import cn.hutool.core.bean.BeanDesc.PropDesc;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.TypeUtil;
import cn.hutool.db.Entity;
/** /**
* 数据结果集处理辅助类 * 数据结果集处理辅助类
* *

View File

@ -8,35 +8,53 @@ import cn.hutool.db.PageResult;
/** /**
* 分页结果集处理类 处理出的结果为PageResult * 分页结果集处理类 处理出的结果为PageResult
* @author loolly
* *
* @author loolly
*/ */
public class PageResultHandler implements RsHandler<PageResult<Entity>>{ public class PageResultHandler implements RsHandler<PageResult<Entity>> {
private static final long serialVersionUID = -1474161855834070108L; private static final long serialVersionUID = -1474161855834070108L;
private PageResult<Entity> pageResult; private PageResult<Entity> pageResult;
/**
* 是否大小写不敏感
*/
private boolean caseInsensitive;
/** /**
* 创建一个 EntityHandler对象<br> * 创建一个 EntityHandler对象<br>
* 结果集根据给定的分页对象查询数据库填充结果 * 结果集根据给定的分页对象查询数据库填充结果
*
* @param pageResult 分页结果集空对象 * @param pageResult 分页结果集空对象
* @return EntityHandler对象 * @return EntityHandler对象
*/ */
public static PageResultHandler create(PageResult<Entity> pageResult) { public static PageResultHandler create(PageResult<Entity> pageResult) {
return new PageResultHandler(pageResult); return new PageResultHandler(pageResult);
} }
/** /**
* 构造<br> * 构造<br>
* 结果集根据给定的分页对象查询数据库填充结果 * 结果集根据给定的分页对象查询数据库填充结果
*
* @param pageResult 分页结果集空对象 * @param pageResult 分页结果集空对象
*/ */
public PageResultHandler(PageResult<Entity> pageResult) { public PageResultHandler(PageResult<Entity> pageResult) {
this(pageResult, false);
}
/**
* 构造<br>
* 结果集根据给定的分页对象查询数据库填充结果
*
* @param pageResult 分页结果集空对象
* @param caseInsensitive 是否大小写不敏感
*/
public PageResultHandler(PageResult<Entity> pageResult, boolean caseInsensitive) {
this.pageResult = pageResult; this.pageResult = pageResult;
this.caseInsensitive = caseInsensitive;
} }
@Override @Override
public PageResult<Entity> handle(ResultSet rs) throws SQLException { public PageResult<Entity> handle(ResultSet rs) throws SQLException {
return HandleHelper.handleRs(rs, pageResult); return HandleHelper.handleRs(rs, pageResult, this.caseInsensitive);
} }
} }

View File

@ -10,9 +10,6 @@ import org.junit.Test;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console; import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.db.Db;
import cn.hutool.db.DbRuntimeException;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.EntityListHandler; import cn.hutool.db.handler.EntityListHandler;
/** /**

View File

@ -175,7 +175,7 @@ public class ServletUtil {
* @return Bean * @return Bean
*/ */
public static <T> T toBean(ServletRequest request, Class<T> beanClass, boolean isIgnoreError) { public static <T> T toBean(ServletRequest request, Class<T> beanClass, boolean isIgnoreError) {
return fillBean(request, ReflectUtil.newInstance(beanClass), isIgnoreError); return fillBean(request, ReflectUtil.newInstanceIfPossible(beanClass), isIgnoreError);
} }
// --------------------------------------------------------- fillBean end // --------------------------------------------------------- fillBean end

View File

@ -413,9 +413,9 @@ public class HttpUtil {
key = item.getKey(); key = item.getKey();
value = item.getValue(); value = item.getValue();
if (value instanceof Iterable) { if (value instanceof Iterable) {
value = CollectionUtil.join((Iterable<?>) value, ","); value = CollUtil.join((Iterable<?>) value, ",");
} else if (value instanceof Iterator) { } else if (value instanceof Iterator) {
value = CollectionUtil.join((Iterator<?>) value, ","); value = CollUtil.join((Iterator<?>) value, ",");
} }
valueStr = Convert.toStr(value); valueStr = Convert.toStr(value);
if (StrUtil.isNotEmpty(key)) { if (StrUtil.isNotEmpty(key)) {

View File

@ -6,12 +6,12 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import cn.hutool.core.annotation.Alias; import cn.hutool.core.annotation.Alias;
import cn.hutool.core.collection.CollUtil;
import lombok.Data; import lombok.Data;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
@ -377,6 +377,7 @@ public class JSONObjectTest {
Assert.assertEquals("张三", jsonObject.getStr("name")); Assert.assertEquals("张三", jsonObject.getStr("name"));
Assert.assertEquals(new Integer(35), jsonObject.getInt("age")); Assert.assertEquals(new Integer(35), jsonObject.getInt("age"));
@SuppressWarnings("ConstantConditions")
JSONObject json = JSONUtil.createObj() JSONObject json = JSONUtil.createObj()
.put("name", "张三") .put("name", "张三")
.put("age", 35); .put("age", 35);

View File

@ -299,7 +299,7 @@ public abstract class AbsSetting implements OptNullBasicTypeFromStringGetter<Str
* @since 5.0.6 * @since 5.0.6
*/ */
public <T> T toBean(String group, Class<T> beanClass) { public <T> T toBean(String group, Class<T> beanClass) {
return toBean(group, ReflectUtil.newInstance(beanClass)); return toBean(group, ReflectUtil.newInstanceIfPossible(beanClass));
} }
/** /**