All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.smallbun.scaffold.framework.mybatis.utils.MappingHelp Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2018-2019.‭‭‭‭‭‭‭‭‭‭‭‭[zuoqinggang] www.pingfangushi.com
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */


package cn.smallbun.scaffold.framework.mybatis.utils;

import cn.smallbun.scaffold.framework.mybatis.page.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;

/**
 * 结果映射工具类
 * @author SanLi
 * Created by [email protected] / [email protected] on 2019/6/20 21:17
 */
public class MappingHelp implements Serializable {
	/**
	 * 单个值转换
	 * @param source source
	 * @param destination destination
	 * @param  TSource
	 * @param  TDestination
	 * @return TDestination
	 */
	public static  TDestination mapping(TSource source, TDestination destination) {
		if (!Objects.isNull(source)) {
			Method[] srcMethods = source.getClass().getMethods();
			Method[] destMethods = destination.getClass().getMethods();
			for (Method m : srcMethods) {
				String srcMethodName = m.getName();
				//调用get方法
				if (srcMethodName.startsWith("get")) {
					try {
						//获取当前方法返回值(获取当前属性值)
						Object getValue = m.invoke(source);
						for (Method dm : destMethods) {
							//目标方法名称
							String destMethodName = dm.getName();
							if (destMethodName.startsWith("set") && destMethodName.substring(3)
									.equals(srcMethodName.substring(3))) {
								dm.invoke(destination, getValue);
							}
						}
					} catch (Exception ignored) {
					}
				}
			}
			return destination;
		}
		return null;
	}

	/**
	 * list 转换
	 * @param src src
	 * @param targetClass targetClass
	 * @param  S
	 * @param  T
	 * @return List
	 */
	public static  List listMapping(List src, Class targetClass) {
		List target = Lists.newArrayList();
		if (!CollectionUtils.isEmpty(src)) {
			for (S s : src) {
				try {
					T object = targetClass.newInstance();
					target.add(object);
					mapping(s, object);
				} catch (Exception ignored) {
				}
			}
			return target;
		}
		return null;
	}

	/**
	 * page 转换
	 * @param src 来源
	 * @param targetClass 目标类
	 * @param  src
	 * @param  targetClass
	 * @return PaginationResult {@link Page}
	 */
	public static  Page pageMapping(IPage src, Class targetClass) {
		//数据集合
		Page result = new Page<>();
		result.setList(listMapping(src.getRecords(), targetClass));
		//页面数据
		Page.Pagination pagination = new Page.Pagination();
		pagination.setCurrent(src.getCurrent());
		pagination.setPageSize(src.getSize());
		pagination.setTotal(src.getTotal());
		result.setPagination(pagination);
		return result;
	}
}