Commit 56acdc3e by hufengqin

数据库代码自动生成

parent 53489b54
...@@ -12,6 +12,9 @@ ...@@ -12,6 +12,9 @@
<modules> <modules>
<module>zgqc-platform</module> <module>zgqc-platform</module>
<module>zgqc-app</module> <module>zgqc-app</module>
<module>zgqc-bigdata</module>
<module>zgqc-market</module>
<module>zgqc-common</module>
</modules> </modules>
</project> </project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.panda.zgqc</groupId>
<artifactId>zgqc</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>zgqc-common</artifactId>
<description>每一个微服务的公共依赖</description>
<dependencies>
<!-- mybaits plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
<!-- <dependency>-->
<!-- <groupId>com.baomidou</groupId>-->
<!-- <artifactId>mybatis-plus-generator</artifactId>-->
<!-- <version>3.4.1</version>-->
<!-- </dependency>-->
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.0.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>xin.altitude.cms</groupId>
<artifactId>ucode-cms-common</artifactId>
<version>1.6.2.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
/*
*
* Copyright (c) 2020-2022, Java知识图谱 (http://www.altitude.xin).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.panda.zgqc.common.entity;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.springframework.http.HttpStatus;
import com.panda.zgqc.common.util.FieldFilterUtils;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
/**
* 操作消息提醒
*
* @author <a href="http://www.altitude.xin" target="_blank">Java知识图谱</a>
* @author <a href="https://gitee.com/decsa/ucode-cms-vue" target="_blank">UCode CMS</a>
* @author <a href="https://space.bilibili.com/1936685014" target="_blank">B站视频</a>
*/
public class AjaxResult extends LinkedHashMap<String, Object> {
/**
* 状态码
*/
public static final String CODE_TAG = "code";
/**
* 返回内容
*/
public static final String MSG_TAG = "msg";
/**
* 数据对象
*/
public static final String DATA_TAG = "data";
/**
* 操作成功信息
*/
protected static final String SUCCESS_MSG = "操作成功";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult() {
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(int code, String msg) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public AjaxResult(int code, String msg, Object data) {
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
Optional.ofNullable(data).ifPresent(e -> super.put(DATA_TAG, e));
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success() {
return new AjaxResult(HttpStatus.OK.value(), SUCCESS_MSG);
}
/**
* 返回成功数据
*
* @param data 响应数据
* @return 成功消息
*/
public static AjaxResult success(Object data) {
return new AjaxResult(HttpStatus.OK.value(), SUCCESS_MSG, data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data) {
return new AjaxResult(HttpStatus.OK.value(), msg, data);
}
/**
* 完成对象实体类的属性过滤
*
* @param data 原始对象实例
* @param action 方法引用选中需要过滤排除的列
* @param <T> 原始数据类型
* @return AjaxResult
* @since 1.5.7
*/
@SafeVarargs
public static <T> AjaxResult success(T data, final SFunction<T, ? extends Serializable>... action) {
return success(data, false, action);
}
/**
* 完成对象实体类的属性过滤
*
* @param data 原始对象实例
* @param isInclude 如果是true代表保留字段、false代表排除字段
* @param action 方法引用选中需要过滤排除的列
* @param <T> 原始数据类型
* @return AjaxResult
* @since 1.5.7.2
*/
@SafeVarargs
public static <T> AjaxResult success(T data, boolean isInclude, final SFunction<T, ? extends Serializable>... action) {
return AjaxResult.success(SUCCESS_MSG, FieldFilterUtils.filterFields(data, isInclude, action));
}
/**
* 完成列表对象实体类的属性过滤
*
* @param data 原始列表对象实例
* @param action 方法引用选中需要过滤排除的列
* @param <T> 原始数据类型
* @return AjaxResult
* @since 1.5.7
*/
@SafeVarargs
public static <T> AjaxResult success(List<T> data, final SFunction<T, ? extends Serializable>... action) {
return success(data, false, action);
}
/**
* 完成列表对象实体类的属性过滤
*
* @param data 原始列表对象实例
* @param isInclude 如果是true代表保留字段、false代表排除字段
* @param action 方法引用选中需要过滤排除的列
* @param <T> 原始数据类型
* @return AjaxResult
* @since 1.5.7.2
*/
@SafeVarargs
public static <T> AjaxResult success(List<T> data, boolean isInclude, final SFunction<T, ? extends Serializable>... action) {
return AjaxResult.success(SUCCESS_MSG, FieldFilterUtils.filterFields(data, isInclude, action));
}
/**
* 完成分页对象实体类的属性过滤
*
* @param data 原始分页对象实例
* @param action 方法引用选中需要过滤排除的列
* @param <T> 原始数据类型
* @return AjaxResult
* @since 1.5.7
*/
@SafeVarargs
public static <T> AjaxResult success(IPage<T> data, final SFunction<T, ? extends Serializable>... action) {
return success(data, false, action);
}
/**
* 完成分页对象实体类的属性过滤
*
* @param data 原始分页对象实例
* @param isInclude 如果是true代表保留字段、false代表排除字段
* @param action 方法引用选中需要过滤排除的列
* @param <T> 原始数据类型
* @return AjaxResult
* @since 1.5.7.2
*/
@SafeVarargs
public static <T> AjaxResult success(IPage<T> data, boolean isInclude, final SFunction<T, ? extends Serializable>... action) {
return AjaxResult.success(SUCCESS_MSG, FieldFilterUtils.filterFields(data, isInclude, action));
}
/**
* 返回错误消息
*
* @return 警告消息
*/
public static AjaxResult error() {
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(String msg) {
return new AjaxResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data) {
return new AjaxResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(int code, String msg) {
return new AjaxResult(code, msg);
}
}
/*
*
* Copyright (c) 2020-2022, Java知识图谱 (http://www.altitude.xin).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.panda.zgqc.common.entity;
import java.util.Map;
/**
* <p>{@link Map.Entry}的默认实现类 用于快速创建{@link Map}实例</p>
*
* @author <a href="http://www.altitude.xin" target="_blank">Java知识图谱</a>
* @author <a href="https://gitee.com/decsa/ucode-cms-vue" target="_blank">UCode CMS</a>
* @author <a href="https://space.bilibili.com/1936685014" target="_blank">B站视频</a>
**/
public class DefaultEntry<K, V> implements Map.Entry<K, V> {
private K key;
private V value;
public DefaultEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
this.value = value;
return this.value;
}
}
/*
*
* Copyright (c) 2020-2022, Java知识图谱 (http://www.altitude.xin).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.panda.zgqc.common.entity;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
* 简易分页实体
*
* @author <a href="http://www.altitude.xin" target="_blank">Java知识图谱</a>
* @author <a href="https://gitee.com/decsa/ucode-cms-vue" target="_blank">UCode CMS</a>
* @author <a href="https://space.bilibili.com/1936685014" target="_blank">B站视频</a>
*/
public class PageEntity {
private long current = 1;
private long size = 10;
public PageEntity() {
}
public PageEntity(long current, long size) {
this.current = current;
this.size = size;
}
public long getCurrent() {
return current;
}
public void setCurrent(long current) {
this.current = current;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
/**
* 转换为MybatisPlus分页对象
*
* @return 分页实体
*/
public <T> Page<T> toPage() {
return new Page<>(current, size);
}
@Override
public String toString() {
return "PageEntity{" +
"current=" + current +
", size=" + size +
'}';
}
}
/*
*
* Copyright (c) 2020-2022, Java知识图谱 (http://www.altitude.xin).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.panda.zgqc.common.model;
/**
* 用户存储K、V结构的Model实体类
*
* @author 赛先生和泰先生
* @author 笔者专题技术博客 —— http://www.altitude.xin
* @author B站视频 —— https://space.bilibili.com/1936685014
**/
public class KVModel<K, V> {
private K key;
private V value;
public KVModel() {
}
public KVModel(K key, V value) {
this.key = key;
this.value = value;
}
public KVModel(KVModel<K, V> kvModel) {
this.key = kvModel.key;
this.value = kvModel.value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
@Override
public String toString() {
return "KVModel{" +
"key=" + key +
", value=" + value +
'}';
}
}
/*
*
* Copyright (c) 2020-2022, Java知识图谱 (http://www.altitude.xin).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.panda.zgqc.common.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.ToIntFunction;
/**
* 集合工具类
*
* @author <a href="http://www.altitude.xin" target="_blank">Java知识图谱</a>
* @author <a href="https://gitee.com/decsa/ucode-cms-vue" target="_blank">UCode CMS</a>
* @author <a href="https://space.bilibili.com/1936685014" target="_blank">B站视频</a>
* @since 2020/06/03 23:17
**/
public class ColUtils {
private ColUtils() {
}
public static <E> boolean isEmpty(Collection<E> data) {
return data == null || (data.size() == 0);
}
public static <E> boolean isEmpty(E[] data) {
return data == null || (data.length == 0);
}
public static <E> boolean isNotEmpty(Collection<E> data) {
return !isEmpty(data);
}
/**
* <p>如果集合实例不为空 则执行{@link Consumer}函数式接口回调方法 无返回结果</p>
*
* @param coll 集合实例
* @param consumer 有输入无输出函数式接口
* @param <E> 集合实例类型
*/
public static <E> void ifNotEmpty(Collection<E> coll, Consumer<Collection<E>> consumer) {
Objects.requireNonNull(consumer);
if (isNotEmpty(coll)) {
consumer.accept(coll);
}
}
/**
* <p>如果集合实例不为空 则执行{@link Function}函数式接口回调方法 有返回结果</p>
* <p>将以{@code E}类型为元素的集合,转换成以{@code R}类型为元素的集合</p>
*
* @param coll 集合实例
* @param action 转换规则
* @param <E> 集合实例类型
* @param <R> 返回值集合实例类型
* @return 以{@code R}类型为元素的集合实例
*/
public static <E, R> List<R> ifNotEmpty(Collection<E> coll, Function<E, R> action) {
Objects.requireNonNull(action);
if (isNotEmpty(coll)) {
List<R> rs = new ArrayList<>(coll.size());
coll.forEach(e -> rs.add(action.apply(e)));
return rs;
}
return Collections.emptyList();
}
public static <E> boolean isNotEmpty(E[] data) {
return !isEmpty(data);
}
public static <E> void ifNotEmpty(E[] data, Consumer<E[]> consumer) {
Objects.requireNonNull(consumer);
if (isNotEmpty(data)) {
consumer.accept(data);
}
}
public static <E, R> List<R> ifNotEmpty(E[] data, Function<E, R> action) {
Objects.requireNonNull(action);
if (isNotEmpty(data)) {
List<R> rs = new ArrayList<>(data.length);
Arrays.stream(data).forEach(e -> rs.add(action.apply(e)));
return rs;
}
return Collections.emptyList();
}
/**
* 将单个对象转化为集合
*
* @param obj 对象实例
* @param <E> 对象类型
* @return 包含对象的集合实例
*/
public static <E> List<E> toCol(E obj) {
if (obj == null) {
return Collections.emptyList();
}
return Collections.singletonList(obj);
}
/**
* 取出集合中第一个元素
*
* @param coll 集合实例
* @param <E> 集合中元素类型
* @return 泛型类型
*/
public static <E> E toObj(Collection<E> coll) {
if (isNotEmpty(coll)) {
return coll.iterator().next();
}
return null;
}
/**
* 以指定列为排序规则 获取排序列最大值所对应的对象
*
* @param data 集合实例
* @param column 比较排序列
* @param <E> 集合中元素泛型
* @return 最大的元素对象
* @since 1.6.0
*/
public static <E> E max(Collection<E> data, ToIntFunction<? super E> column) {
Objects.requireNonNull(column);
if (isNotEmpty(data)) {
return data.stream().max(Comparator.comparingInt(column)).orElse(null);
}
return null;
}
/**
* @param data 集合实例
* @param column 比较排序列
* @param <E> 集合中元素泛型
* @param <U> 排序列对应的对象泛型 如果是非基础数据类型 需要实现{@code Comparable}
* @return 最大的元素对象
* @since 1.6.0
*/
public static <E, U extends Comparable<? super U>> E max(Collection<E> data, Function<? super E, ? extends U> column) {
Objects.requireNonNull(column);
if (isNotEmpty(data)) {
return data.stream().max(Comparator.comparing(column)).orElse(null);
}
return null;
}
/**
* @param data 集合实例
* @param comparator 比较排序列
* @param <E> 集合中元素泛型
* @return 最小的元素对象
* @since 1.6.0
*/
public static <E> E max(Collection<E> data, Comparator<? super E> comparator) {
Objects.requireNonNull(comparator);
if (isNotEmpty(data)) {
return data.stream().max(comparator).orElse(null);
}
return null;
}
/**
* 以指定列为排序规则 获取最小元素的对象
*
* @param data 集合实例
* @param column 比较排序列
* @param <E> 集合元素泛型
* @return 最小的元素对象
* @since 1.6.0
*/
public static <E> E min(Collection<E> data, ToIntFunction<? super E> column) {
Objects.requireNonNull(column);
if (isNotEmpty(data)) {
return data.stream().min(Comparator.comparingInt(column)).orElse(null);
}
return null;
}
/**
* @param data 集合实例
* @param column 比较排序列
* @param <E> 集合中元素泛型
* @param <U> 排序列对应的对象泛型 如果是非基础数据类型 需要实现{@code Comparable}
* @return 最小的元素对象
* @since 1.6.0
*/
public static <E, U extends Comparable<? super U>> E min(Collection<E> data, Function<? super E, ? extends U> column) {
Objects.requireNonNull(column);
if (isNotEmpty(data)) {
return data.stream().min(Comparator.comparing(column)).orElse(null);
}
return null;
}
/**
* @param data 集合实例
* @param comparator 比较排序列
* @param <E> 集合中元素泛型
* @return 最小的元素对象
* @since 1.6.0
*/
public static <E> E min(Collection<E> data, Comparator<? super E> comparator) {
Objects.requireNonNull(comparator);
if (isNotEmpty(data)) {
return data.stream().min(comparator).orElse(null);
}
return null;
}
}
/*
*
* Copyright (c) 2020-2022, Java知识图谱 (http://www.altitude.xin).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.panda.zgqc.common.util;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.panda.zgqc.common.support.ArrayCollector;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* <p>超级强大的<i>POJO</i>数据实体类转换工具</p>
* <p>{@link EntityUtils}工具类用于基于Lambda表达式实现类型转换,具有如下优点:</p>
* <p>1. 实现对象转对象;集合转集合;分页对象转分页对象</p>
* <p>2. 实体类转Vo、实体类转DTO等都能应用此工具类</p>
* <p>3. 转换参数均为不可变类型,业务更加安全</p>
*
* @author <a href="http://www.altitude.xin" target="_blank">Java知识图谱</a>
* @author <a href="https://gitee.com/decsa/ucode-cms-vue" target="_blank">UCode CMS</a>
* @author <a href="https://space.bilibili.com/1936685014" target="_blank">B站视频</a>
* @since 2019/06/19 17:23
**/
public class EntityUtils {
private EntityUtils() {
}
/**
* 将对象集合按照一定规则映射后收集为另一种形式的集合
*
* @param <R> 最终结果的泛型
* @param <S> 原始集合元素的类泛型
* @param <T> 转换后元素的中间状态泛型
* @param <A> 最终结果收集器泛型
* @param source 最原始的集合实例
* @param action 转换规则
* @param collector 收集器的类型
* @return 变换后存储新元素的集合实例
*/
public static <R, S, T, A> R collectCommon(final Collection<S> source, Function<? super S, ? extends T> action, Collector<? super T, A, R> collector) {
Objects.requireNonNull(source);
Objects.requireNonNull(collector);
return source.stream().map(action).collect(collector);
}
/**
* 将实体类对象从{@code T}类型转化为{@code R}类型
* <pre>
* public class Subject{
* private Integer subId;
* private String subName;
*
* public Subject(Subject subject) {
* if (Objects.nonNull(subject)) {
* this.subId=subject.subId;
* this.subName=subject.subName;
* }
* }
* }
* public class SubjectBo extends Subject {
* private Integer score;
*
* public SubjectBo(Subject subject) {
* super(subject);
* }
* }
* </pre>
* 使用示例实现实体类Subject转换为SubjectBo
* <pre>
* SubjectBo subjectBo = EntityUtils.toObj(subject, SubjectBo::new);
* </pre>
*
* @param <T> 源数据类型
* @param <R> 变换后数据类型
* @param obj 源对象实例
* @param action 映射Lambda表达式 参数不能为<code>null</code> 否则抛出异常
* @return 变换后的类型,如果source为null,则返回null
*/
public static <T, R> R toObj(final T obj, final Function<? super T, ? extends R> action) {
Objects.requireNonNull(action);
return Optional.ofNullable(obj).map(action).orElse(null);
}
/**
* <p>将{@code List}集合换成另一种类型</p>
* <pre>
* public class User {
* private Long userId;
* private String userName;
* private String sex;
* }
* </pre>
* <p>通过方法引用获得任意列组成的新{@code List}集合</p>
* <pre>
* List&lt;Long&gt; userIds = EntityUtils.toList(list,User::getUserId)
* </pre>
* <p>在{@code User}类中添加有如下构造器</p>
* <pre>
* public User(User user) {
* if(user != null) {
* this.userId = user.userId;
* this.userName = user.userName;
* this.sex = user.sex;
* }
* }
* </pre>
* <pre>
* public class UserVo extends User {
* private String deptName;
*
* public UserVo (User user) {
* super(user);
* }
* }
* </pre>
* 通过如下代码可实现DO 转 VO
* <pre>
* List&lt;Long&gt; userVos = EntityUtils.toList(list,UserVo::new)
* </pre>
*
* @param <T> 源数据类型
* @param <R> 变换后数据类型
* @param list 源List集合
* @param action 映射Lambda表达式
* @return 变换后的类型集合,如果source为null,则返回空集合
*/
public static <T, R> List<R> toList(final Collection<T> list, final Function<? super T, ? extends R> action) {
Objects.requireNonNull(action);
if (Objects.nonNull(list)) {
return list.stream().map(action).collect(Collectors.toList());
}
return Collections.emptyList();
}
/**
* <p>将数组转化为集合</p>
* 示例:不转换实体类类型
*
* @param arrays 数组实例
* @param <T> 原始实体类类型
* @return 以<code>R</code>为元素的集合实例
*/
public static <T> List<T> toList(final T[] arrays) {
return new ArrayList<>(Arrays.asList(arrays));
}
/**
* <p>将数组转化为集合</p>
* 示例一:不转换实体类类型
* 示例二:转换实体类类型
*
* @param arrays 数组实例
* @param action 值为{@code Function.identity()}不转变实体类类型
* @param <T> 原始实体类类型
* @param <R> 目标实体类类型
* @return 以<code>R</code>为元素的集合实例
*/
public static <T, R> List<R> toList(final T[] arrays, final Function<? super T, ? extends R> action) {
return toList(Arrays.asList(arrays), action);
}
/**
* <p>将以{@code T}类型为元素的集合转化成以以{@code R}类型为元素的集合 并允许过滤数据</p>
*
* @param <T> 源数据类型
* @param <R> 变换后数据类型
* @param list 源List集合
* @param action 映射Lambda表达式
* @return 变换后的类型集合,如果source为null,则返回空集合
*/
public static <T, R> List<R> toList(final Collection<T> list, final Function<? super T, ? extends R> action, Predicate<R> pred) {
Objects.requireNonNull(action);
Objects.requireNonNull(pred);
if (Objects.nonNull(list)) {
return list.stream().map(action).filter(pred).collect(Collectors.toList());
}
return Collections.emptyList();
}
/**
* <p>使用反射将元素{@code T}类型的集合转化为元素{@code VO}类型的集合</p>
* <p>使用本方法的限制条件是泛型{@code VO}类包含以泛型{@code T}类为参数的构造器</p>
*
* @param list 元素{@code T}类型的集合实例
* @param voClazz 泛型{@code VO}实例类Class对象
* @param <T> DO为{@code T}实体类泛型
* @param <VO> VO为{@code VO}实体类泛型
* @return 以元素{@code VO}为类型的集合实例 如果获取不到指定类型的构造器或者发生异常 则返回空集合
*/
public static <T, VO> List<VO> toList(final Collection<T> list, Class<VO> voClazz) {
if (ColUtils.isNotEmpty(list)) {
T obj = ColUtils.toObj(list);
if (obj != null) {
Constructor<VO> constructor = RefUtils.getConstructor(voClazz, obj.getClass());
return toList(list, e -> RefUtils.newInstance(constructor, e));
}
}
return Collections.emptyList();
}
/**
* 将集合{@code List<T>}实例按照参数<code>action</code>映射关系转换后 生成{@code G[]}数组
*
* <pre>
* User[] userNames = EntityUtils.toArray(userList)
* </pre>
*
* @param <T> 集合元素的类型
* @param list 集合实例 不允许为<code>null</code>
* @return {@code G[]}数组实例
* @since 1.6.1
*/
public static <T> T[] toArray(final List<T> list) {
if (ColUtils.isNotEmpty(list)) {
Class<?> tClazz = Objects.requireNonNull(ColUtils.toObj(list)).getClass();
ArrayCollector<T> collector = new ArrayCollector<>(RefUtils.getClass(tClazz));
return list.stream().collect(collector);
}
return null;
}
/**
* 将集合{@code List<T>}实例按照参数<code>action</code>映射关系转换后 生成{@code G[]}数组
*
* <pre>
* String[] userNames = EntityUtils.toArray(userList, User::getUserName)
* </pre>
*
* @param list 数组实例 不允许为<code>null</code>
* @param action 映射关系 不允许为<code>null</code>
* @param <T> 集合元素的类型
* @param <R> 转换后数组的类型
* @return {@code R[]}数组实例
*/
public static <T, R> R[] toArray(final T[] list, final Function<? super T, ? extends R> action) {
Objects.requireNonNull(list);
return toArray(Arrays.asList(list), action);
}
/**
* 将集合{@code List<T>}实例按照参数<code>action</code>映射关系转换后 生成{@code G[]}数组
*
* <pre>
* String[] userNames = EntityUtils.toArray(userList, User::getUserName)
* </pre>
*
* @param list 集合实例 不允许为<code>null</code>
* @param action 映射关系 不允许为<code>null</code>
* @param <T> 集合元素的类型
* @param <R> 转换后数组的类型
* @return {@code G[]}数组实例
*/
public static <T, R> R[] toArray(final Collection<T> list, final Function<? super T, ? extends R> action) {
Objects.requireNonNull(action);
if (ColUtils.isNotEmpty(list)) {
Class<?> rClazz = toObj(ColUtils.toObj(list), action).getClass();
ArrayCollector<R> collector = new ArrayCollector<>(RefUtils.getClass(rClazz));
return list.stream().map(action).collect(collector);
}
return null;
}
/**
* <p>将{@code T}元素类型的集合转换成另{@code G}元素类型的数组</p>
* <p>更优雅的实现参考{@link EntityUtils#toArray(Collection, Function)}</p>
*
* @param <T> 原始的数据类型(泛型)
* @param <R> 目标类型(泛型)
* @param list 以{@code T}元素为类型的集合实例
* @param action 转换规则(方法引用表示)
* @param generator 以{@code G}元素为类型数组{@code Class}对象
* @return 以{@code G}元素为类型数组实例
* @author 明日之春 赛泰
* @see #toArray(Collection, Function, Class)
* @since 1.6.0
*/
public static <T, R> R[] toArray(final Collection<T> list, final Function<? super T, ? extends R> action, IntFunction<R[]> generator) {
Objects.requireNonNull(action);
if (ColUtils.isNotEmpty(list)) {
return list.stream().map(action).toArray(generator);
}
return null;
}
/**
* <p>将{@code T}元素类型的集合转换成另{@code G}元素类型的数组</p>
* <p>更优雅的实现参考{@link EntityUtils#toArray(Collection, Function)}</p>
* <pre>
* EntityUtils.toArray(userList, User::getUserName, String[].class)
* </pre>
*
* @param <T> 原始的数据类型(泛型)
* @param <R> 目标类型(泛型)
* @param list 以{@code T}元素为类型的集合实例
* @param action 转换规则(方法引用表示)
* @param clazz 以{@code G}元素为类型数组{@code Class}对象
* @return 以{@code G}元素为类型数组实例
* @author 明日之春 赛泰
* @since 1.6.0
*/
@SuppressWarnings("unchecked")
public static <T, R> R[] toArray(final Collection<T> list, final Function<? super T, ? extends R> action, final Class<R[]> clazz) {
Objects.requireNonNull(action);
Objects.requireNonNull(clazz);
Class<?> itemClazz = clazz.isArray() ? clazz.getComponentType() : clazz;
IntFunction<R[]> generator = e -> (R[]) Array.newInstance(itemClazz, e);
return toArray(list, action, generator);
}
/**
* 将IPaged对象以一种类型转换成另一种类型
*
* @param page 源Page
* @param action 转换规则
* @param <E> 源Page类型泛型
* @param <T> 源实体类
* @param <R> 目标Page类型泛型
* @return 变换后的分页类型
*/
public static <E extends IPage<T>, T, R> IPage<R> toPage(E page, final Function<? super T, ? extends R> action) {
Objects.requireNonNull(page);
Objects.requireNonNull(action);
return page.convert(action);
}
/**
* <p>将{@code T}类型实体类对象转化为{@code Map}实例</p>
*
* @param obj POJO对象实例
* @param <T> 对象类型
* @return 以字段名称为Key 以字段值为Value的{@code Map}实例
*/
public static <T> Map<String, Object> toMap(final T obj) {
Objects.requireNonNull(obj);
List<Field> fieldList = ReflectionKit.getFieldList(obj.getClass());
Map<String, Object> map = new HashMap<>();
fieldList.forEach(e -> map.put(e.getName(), RefUtils.getFieldValue(obj, e)));
return map;
}
/**
* <p>将{@code T}类型实体类对象转化为{@code Map}实例</p>
*
* @param pred 断言器 方便用户完成更高级的需求 如无需要 可忽略
* @param obj POJO对象实例
* @param <T> 对象类型
* @return 以字段名称为Key 以字段值为Value的{@code Map}实例
*/
public static <T> Map<String, Object> toMap(final T obj, final Predicate<Field> pred) {
Objects.requireNonNull(obj);
Objects.requireNonNull(pred);
List<Field> fieldList = ReflectionKit.getFieldList(obj.getClass()).stream().filter(pred).collect(Collectors.toList());
Map<String, Object> map = new HashMap<>();
fieldList.forEach(e -> map.put(e.getName(), RefUtils.getFieldValue(obj, e)));
return map;
}
/**
* 将集合转化成Map
*
* @param list 集合实例
* @param keyAction key转换规则
* @param <T> 集合实体类泛型
* @param <K> Key实体类型泛型
* @return Map实例
* @since 1.6.0
*/
public static <T, K> Map<K, T> toMap(final Collection<T> list, Function<? super T, ? extends K> keyAction) {
Objects.requireNonNull(keyAction);
return toMap(list, keyAction, Function.identity());
}
/**
* 将集合转化成Map
*
* @param list 集合实例
* @param keyAction key转换规则
* @param valueAction value转换规则
* @param <T> 集合实体类泛型
* @param <K> Key实体类型泛型
* @param <V> Value实体类型泛型
* @return Map实例
*/
public static <T, K, V> Map<K, V> toMap(final Collection<T> list, Function<? super T, ? extends K> keyAction, Function<? super T, ? extends V> valueAction) {
Objects.requireNonNull(keyAction);
Objects.requireNonNull(valueAction);
if (ColUtils.isNotEmpty(list)) {
return list.stream().collect(Collectors.toMap(keyAction, valueAction));
}
return Collections.emptyMap();
}
/**
* 将List集合以一种类型转换成Set集合
*
* @param <T> 源数据类型
* @param list 源List集合
* @return 变换后的类型集合,如果source为null,则返回空集合
*/
public static <T> Set<T> toSet(final Collection<T> list) {
if (ColUtils.isNotEmpty(list)) {
// 这里不使用流 使用构造器性能更好
return new HashSet<>(list);
}
return Collections.emptySet();
}
/**
* 将List集合以一种类型转换成Set集合
*
* @param <T> 源数据类型
* @param <R> 变换后数据类型
* @param data 源List集合
* @param action 映射Lambda表达式
* @return 变换后的类型集合,如果source为null,则返回空集合
*/
public static <T, R> Set<R> toSet(final Collection<T> data, final Function<? super T, ? extends R> action) {
Objects.requireNonNull(action);
if (Objects.nonNull(data)) {
return data.stream().map(action).collect(Collectors.toSet());
}
return Collections.emptySet();
}
/**
* <p>对集合中元素按照指定列进行分组</p>
* <p>返回值是{@code Map},其中Key为分组列,Value为当前元素</p>
*
* @param list 集合实例
* @param gColumn 分组列(方法引用表示)
* @param <E> 集合元素泛型
* @param <G> 分组列数据类型泛型
* @return {@code Map}实例
* @since 1.6.0
*/
public static <E, G> Map<G, List<E>> groupBy(final Collection<E> list, final Function<E, G> gColumn) {
Objects.requireNonNull(gColumn);
if (Objects.nonNull(list)) {
return list.stream().collect(Collectors.groupingBy(gColumn));
}
return Collections.emptyMap();
}
/**
* <p>对集合中元素按照指定列进行分组</p>
* <p>返回值是{@code Map},其中Key为分组列,Value为当前元素</p>
* <p>通过比较器对集合中元素排序</p>
*
* @param list 集合实例
* @param gColumn 分组列(方法引用表示)
* @param <E> 集合元素泛型
* @param <G> 分组列数据类型泛型
* @return {@code Map}实例
* @since 1.6.1
*/
public static <E, G> Map<G, List<E>> groupBy(final Collection<E> list, final Function<E, G> gColumn, Comparator<E> comparator) {
Objects.requireNonNull(comparator);
Map<G, List<E>> map = groupBy(list, gColumn);
map.forEach((k, v) -> v.sort(comparator));
return map;
}
/**
* <p>对集合中元素按照指定列进行分组</p>
* <p>返回值是{@code Map},其中Key为分组列</p>
*
* @param list 集合实例
* @param gColumn 分组列(方法引用表示)
* @param action 转换行为
* @param <U> Value集合元素类型泛型
* @param <E> 集合元素泛型
* @param <G> 分组列数据类型泛型
* @return {@code Map}实例
* @since 1.6.0
*/
public static <E, G, U> Map<G, List<U>> groupBy(final Collection<E> list, final Function<E, G> gColumn, final Function<E, U> action) {
Objects.requireNonNull(gColumn);
if (Objects.nonNull(list)) {
return list.stream().collect(Collectors.groupingBy(gColumn, Collectors.mapping(action, Collectors.toList())));
}
return Collections.emptyMap();
}
}
/*
*
* Copyright (c) 2020-2022, Java知识图谱 (http://www.altitude.xin).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.panda.zgqc.common.util;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.panda.zgqc.common.entity.DefaultEntry;
import com.panda.zgqc.common.util.EntityUtils;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
/**
* <p>{ FieldCleanUtils}和{ FieldFilterUtils}的主要区别是
* 前者将字段置空,后者将字段移除;前者适用于数据模型各个阶段,后者通常在控制器返回前调用</p>
*
* @author <a href="http://www.altitude.xin" target="_blank">Java知识图谱</a>
* @author <a href="https://gitee.com/decsa/ucode-cms-vue" target="_blank">UCode CMS</a>
* @author <a href="https://space.bilibili.com/1936685014" target="_blank">B站视频</a>
**/
public class FieldFilterUtils {
private FieldFilterUtils() {
}
/**
* 过滤属性 操作单个对象 默认情况为排除属性(字段)
*
* @param <T> 目标对象泛型
* @param obj 泛型对象实例
* @param columns 待处理的列字段(用方法引用表示)
* @return Map实例
*/
@SafeVarargs
public static <T> Map<?, ?> filterFields(T obj, final SFunction<T, ? extends Serializable>... columns) {
return filterFields(obj, false, columns);
}
/**
* 过滤属性 操作单个对象
*
* @param <T> 目标对象泛型
* @param obj 泛型对象实例
* @param isInclude 如果是true代表保留字段、false代表排除字段
* @param columns 待处理的列字段(用方法引用表示)
* @return Map实例
*/
@SafeVarargs
public static <T> Map<?, ?> filterFields(T obj, boolean isInclude, final SFunction<T, ? extends Serializable>... columns) {
return Optional.of(obj).map(e -> {
Set<String> fieldNames = new HashSet<>(RefUtils.getFiledNames(columns));
Map<String, Object> map = EntityUtils.toMap(obj);
return doFilter(map, fieldNames, isInclude);
}).orElse(null);
}
/**
* 过滤属性 操作列表对象 默认情况为排除属性(字段)
*
* @param <T> 目标对象泛型
* @param list 泛型列表集合实例
* @param action 待处理的列字段(用方法引用表示)
* @return Map集合实例
*/
@SafeVarargs
public static <T> List<? extends Map<?, ?>> filterFields(List<T> list, final SFunction<T, ? extends Serializable>... action) {
return filterFields(list, false, action);
}
/**
* 过滤属性 操作列表对象
*
* @param <T> 目标对象泛型
* @param list 泛型列表集合实例
* @param isInclude 如果是true代表保留字段、false代表排除字段
* @param action 待处理的列字段(用方法引用表示)
* @return Map集合实例
*/
@SafeVarargs
public static <T> List<? extends Map<?, ?>> filterFields(List<T> list, boolean isInclude, final SFunction<T, ? extends Serializable>... action) {
return Optional.of(list).map(f -> EntityUtils.toList(f, e -> filterFields(e, isInclude, action))).orElse(null);
}
/**
* 过滤属性 操作分页对象
*
* @param <T> 目标对象泛型
* @param page 泛型分页实例
* @param columns 待处理的列字段(用方法引用表示)
* @return Map分页实例
*/
@SafeVarargs
public static <T> IPage<? extends Map<?, ?>> filterFields(IPage<T> page, final SFunction<T, ? extends Serializable>... columns) {
return filterFields(page, false, columns);
}
/**
* 过滤属性 操作分页对象
*
* @param <T> 目标对象泛型
* @param page 泛型分页实例
* @param isInclude 如果是true代表保留字段、false代表排除字段
* @param columns 待处理的列字段(用方法引用表示)
* @return Map分页实例
*/
@SafeVarargs
public static <T> IPage<? extends Map<?, ?>> filterFields(IPage<T> page, boolean isInclude, final SFunction<T, ? extends Serializable>... columns) {
return Optional.of(page).map(f -> EntityUtils.toPage(f, e -> filterFields(e, isInclude, columns))).orElse(null);
}
/**
* 具体执行过滤
*/
private static Map<String, Object> doFilter(Map<String, Object> map, Set<String> fieldNames, boolean contains) {
if (contains) {
List<DefaultEntry<String, Object>> list = EntityUtils.toList(fieldNames, e -> new DefaultEntry<>(e, map.get(e)));
return MapUtils.transMap(list);
} else {
return MapUtils.transMap(map.entrySet(), (Predicate<Map.Entry<String, Object>>) e -> !fieldNames.contains(e.getKey()));
}
}
}
package com.panda.zgqc.common.util;
import com.panda.zgqc.common.model.KVModel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* MapUtils工具类
*
* @author <a href="http://www.altitude.xin" target="_blank">Java知识图谱</a>
* @author <a href="https://gitee.com/decsa/ucode-cms-vue" target="_blank">UCode CMS</a>
* @author <a href="https://space.bilibili.com/1936685014" target="_blank">B站视频</a>
* @since 2019/06/03 22:49
**/
public class MapUtils {
private MapUtils() {
}
public static <K, V> boolean isEmpty(Map<K, V> map) {
if (map == null) {
return true;
} else {
return map.isEmpty();
}
}
public static <K, V> boolean isNotEmpty(Map<K, V> map) {
return !isEmpty(map);
}
public static <K, V> void ifNotEmpty(Map<K, V> map, Consumer<Map<K, V>> consumer) {
Objects.requireNonNull(consumer);
if (isNotEmpty(map)) {
consumer.accept(map);
}
}
/**
* 批量取出Map中的值
*
* @param map map实例
* @param keys 键的集合
* @param <K> key的泛型
* @param <V> value的泛型
* @return value的泛型的集合
*/
@SafeVarargs
public static <K, V> List<V> getCollection(Map<K, V> map, K... keys) {
Objects.requireNonNull(keys);
return getCollection(map, Arrays.asList(keys));
}
/**
* 批量取出Map中的值
*
* @param map map实例
* @param keys 键的集合
* @param <K> key的泛型
* @param <V> value的泛型
* @return value的泛型的集合
*/
public static <K, V> List<V> getCollection(Map<K, V> map, Collection<K> keys) {
Objects.requireNonNull(keys);
List<V> result = new ArrayList<>();
ifNotEmpty(map, e -> keys.forEach(key -> Optional.ofNullable(e.get(key)).ifPresent(result::add)));
return result;
}
/**
* 批量取出Map中的值
*
* @param source map实例
* @param keys 键key集合
* @param comparator 排序器
* @param <K> key的泛型
* @param <V> value的泛型
* @return value的泛型的集合
*/
public static <K, V> List<V> getCollection(Map<K, V> source, Collection<K> keys, Comparator<V> comparator) {
List<V> result = getCollection(source, keys);
Optional.ofNullable(comparator).ifPresent(result::sort);
return result;
}
/**
* 将Map转化成List
*
* @param source 原始Map实例
* @param <K> Key类型
* @param <V> Value类型
* @return 返回KVModel类型集合
*/
public static <K, V> List<KVModel<K, V>> mapToList(Map<K, V> source) {
Objects.requireNonNull(source);
List<KVModel<K, V>> result = source.entrySet().stream()
.map(e -> new KVModel<>(e.getKey(), e.getValue()))
.collect(Collectors.toList());
return result;
}
/**
* 讲Map中 value进行转换
*
* @param map 原始Map实例
* @param valueAction value转换的行为
* @param <K> Key的类型
* @param <V> 原始value的类型
* @param <R> 目标value类型
* @return 转换后的Map
*/
public static <K, V, R> Map<K, R> transMap(Map<K, V> map, Function<? super V, ? extends R> valueAction) {
Objects.requireNonNull(valueAction);
Map<K, R> hashMap = new HashMap<>(16);
ifNotEmpty(map, e -> e.forEach((key, value) -> hashMap.put(key, EntityUtils.toObj(value, valueAction))));
return hashMap;
}
public static <K, V, NK, NV> Map<NK, NV> transMap(Map<K, V> map, Function<? super K, ? extends NK> keyAction, Function<? super V, ? extends NV> valueAction) {
Objects.requireNonNull(valueAction);
Map<NK, NV> hashMap = new HashMap<>(16);
ifNotEmpty(map, e -> e.forEach((key, value) -> hashMap.put(EntityUtils.toObj(key, keyAction), EntityUtils.toObj(value, valueAction))));
return hashMap;
}
/**
* 将{@link Map.Entry}集合实例转化成{@link Map}实例
*
* @param list {@code Map.Entry}集合实例
* @param <K> {@code Map}的Key类型
* @param <V> {@code Map}的Value类型
* @return {@code Map}实例 如果输入集合为null或者空集合 则返回空{@code Map}实例
*/
public static <E extends Map.Entry<K, V>, K, V> Map<K, V> transMap(Collection<E> list) {
return transMap(list, (Predicate<E>) e -> true);
}
/**
* 将{@link Map.Entry}集合实例转化成{@link Map}实例
*
* @param list {@code Map.Entry}集合实例
* @param pred 断言器 用以辅助过滤数据
* @param <K> {@code Map}的Key类型
* @param <V> {@code Map}的Value类型
* @return {@code Map}实例 如果输入集合为null或者空集合 则返回空{@code Map}实例
*/
public static <E extends Map.Entry<K, V>, K, V> Map<K, V> transMap(Collection<E> list, Predicate<E> pred) {
Objects.requireNonNull(pred);
if (ColUtils.isNotEmpty(list)) {
Map<K, V> map = new HashMap<>(16);
list.stream().filter(pred).forEach(e -> map.put(e.getKey(), e.getValue()));
return map;
}
return Collections.emptyMap();
}
/**
* <p>将普通的集合 按照一定的映射关系转换后 生成新{@code Map}实例</p>
*
* @param list {@code E}集合实例
* @param acton 转换规则
* @param <E> {@code E}集合类型
* @param <K> {@code Map}的Key类型
* @param <V> {@code Map}的Value类型
* @return {@code Map}实例 如果输入集合为null或者空集合 则返回空{@code Map}实例
*/
public static <E, K, V> Map<K, V> transMap(Collection<E> list, Function<E, ? extends Map.Entry<K, V>> acton) {
return transMap(EntityUtils.toList(list, acton));
}
/**
* <p>从{@code Map}实例中取值 防止因{@code Map}实例为<code>null</code>而发生运行时空指针异常</p>
* <p>如果{@code Map}实例为<code>null</code>,则返回<code>null</code></p>
*
* @param map {@code Map}实例 允许为<code>null</code>
* @param key Key的值 允许为<code>null</code>
* @param <K> Key的类型
* @param <V> Value的类型
* @return 从{@code Map}实例通过Key取出的Value值
*/
public static <K, V> V getObj(Map<K, V> map, K key) {
return Optional.ofNullable(map).map(e -> e.get(key)).orElse(null);
}
/**
* <p>从{@code Map}实例中取值</p>
* <p>防止因{@code Map}实例为<code>null</code>而发生运行时空指针异常</p>
*
* @param map {@code Map}实例 允许为<code>null</code>
* @param key Key的值 允许为<code>null</code>
* @param defaultValue 默认值 允许为<code>null</code>
* @param <K> Key的类型
* @param <V> Value的类型
* @return 从{@code Map}实例通过Key取出的Value值
*/
public static <K, V> V getObj(Map<K, V> map, K key, V defaultValue) {
return Optional.ofNullable(map).map(e -> e.get(key)).orElse(defaultValue);
}
/**
* <p>从{@code Map}实例中取值 并将Value从{@code V}类型转化为{@code R}类型实例</p>
* <p>防止因{@code Map}实例为<code>null</code>而发生运行时空指针异常</p>
*
* @param map {@code Map}实例 允许为<code>null</code>
* @param key Key的值 允许为<code>null</code>
* @param action Value转换规则
* @param <K> Key的类型
* @param <V> Value的类型
* @return 从{@code Map}实例通过Key取出的Value值
*/
public static <K, V, R> R getObj(Map<K, V> map, K key, Function<V, R> action) {
Objects.requireNonNull(action);
return Optional.ofNullable(map).map(e -> e.get(key)).map(action).orElse(null);
}
}
package com.panda.zgqc.common.util;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.ibatis.reflection.property.PropertyNamer;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author 赛先生和泰先生
* @author 笔者专题技术博客 —— http://www.altitude.xin
* @author B站视频 —— https://space.bilibili.com/1936685014
**/
public class RefUtils {
private RefUtils() {
}
@SuppressWarnings("unchecked")
public static <T, R> R getFiledValue(T t, SFunction<T, R> action) {
String fieldName = Optional.ofNullable(getFiledName(action)).orElse("");
try {
Field field = t.getClass().getField(fieldName);
return (R) field.get(t);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* 通过反射 给属性赋值
*
* @param t
* @param action
* @param value
* @param <S>
* @param <RR>
*/
public static <T, S, RR> void setFiledValue(T t, SFunction<S, RR> action, Object value) {
String fieldName = Optional.ofNullable(getFiledName(action)).orElse("");
try {
Field field = t.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(t, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static <T> void setFiledValue(T t, String fieldName, Object value) {
try {
Field field = t.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(t, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static <T> void setFiledValue(T t, Field field, Object value) {
try {
field.setAccessible(true);
field.set(t, value);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* 通过方法引用获取指定实体类的字段名(属性名)
*
* @param action
* @param <T>
* @param <R>
* @return
*/
public static <T, R> String getFiledName(SFunction<T, R> action) {
return Optional.ofNullable(action).map(LambdaUtils::extract)
.map(LambdaMeta::getImplMethodName)
.map(PropertyNamer::methodToProperty).orElse(null);
}
// /**
// * 通过方法引用获取指定实体类的字段名(属性名)
// *
// * @param <T>
// * @param <R>
// * @param action
// * @return
// */
// @SafeVarargs
// public static <T, R> List<String> getFiledNames(SFunction<T, R>... action) {
// return Arrays.stream(action).map(LambdaUtils::extract)
// .map(LambdaMeta::getImplMethodName)
// .map(PropertyNamer::methodToProperty)
// .collect(Collectors.toList());
// }
@SafeVarargs
public static <T> List<String> getFiledNames(SFunction<T, ? extends Serializable>... action) {
return Arrays.stream(action).map(LambdaUtils::extract)
.map(LambdaMeta::getImplMethodName)
.map(PropertyNamer::methodToProperty)
.collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(Object entity, String fieldName) {
return (T) ReflectionKit.getFieldValue(entity, fieldName);
}
public static Object getFieldValue(Object entity, Field field) {
field.setAccessible(true);
try {
return field.get(entity);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* 通过Clazz对象创建实例
*
* @param clazz CLass对象
* @param <T> 泛型
* @return 泛型实例
*/
public static <T> T newInstance(Class<T> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* 获得参数为{@code doClazz}的构造器
*
* @param doClazz DO实体类的CLass对象实例
* @param voClazz VO实体类的CLass对象实例
* @param <VO> VO实体类泛型
* @return VO实体类的构造器
*/
// @SafeVarargs
public static <VO> Constructor<VO> getConstructor(Class<VO> voClazz, Class<?>... doClazz) {
Objects.requireNonNull(doClazz);
try {
return voClazz.getConstructor(doClazz);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
/**
* 通过构造器创建对象
*
* @param constructor 以泛型{@code VO}为类型的构造器实例
* @param initargs 以泛型{@code DO}为类型的参数实例
* @param <DO> {@code DO}泛型
* @param <VO> {@code VO}泛型
* @return 以泛型{@code VO}为类型的对象实例
*/
@SafeVarargs
public static <DO, VO extends DO> VO newInstance(Constructor<VO> constructor, DO... initargs) {
try {
return constructor.newInstance(initargs);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
/**
* <p>显示化获得{@code Class<T>}对象的类型</p>
* <p>本方法的作用时避免在显示强转时出现<i>未检查警告</i></p>
* <p>注意{@code Class<\?>}与{@code Class<T>}是同一个类型才能强转</p>
*
* @param clazz Class对象实例
* @param <T> 元素类型
* @return 如果参数<code>clazz</code>不为<code>null</code>,则返回强转后的对象,否则返回<code>null</code>
*/
@SuppressWarnings("unchecked")
public static <T> Class<T> getClass(Class<?> clazz) {
return (Class<T>) clazz;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment