Commit 56acdc3e by hufengqin

数据库代码自动生成

parent 53489b54
......@@ -12,6 +12,9 @@
<modules>
<module>zgqc-platform</module>
<module>zgqc-app</module>
<module>zgqc-bigdata</module>
<module>zgqc-market</module>
<module>zgqc-common</module>
</modules>
</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.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