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

top.doudou.common.tool.datebase.jpa.CustomTransformer Maven / Gradle / Ivy

There is a newer version: 1.3.2
Show newest version
package top.doudou.common.tool.datebase.jpa;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.transform.AliasToBeanResultTransformer;
import top.doudou.base.util.StrUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author 傻男人<[email protected]>
 * @create 2018-11-08 19:37
 * jpa结果集转换工具
 */
@Slf4j
public class CustomTransformer extends AliasToBeanResultTransformer {
    private Class resultClass;

    public CustomTransformer(Class resultClass) {
        super(resultClass);
        this.resultClass = resultClass;
    }

    private static final long serialVersionUID = 1L;

    @Override
    public Object transformTuple(Object[] tuple, String[] aliases) {
        Object obj = null;
        try {
            obj = resultClass.newInstance();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }
        // 返回这个类里面方法的集合
        Method[] methods = resultClass.getMethods();
        for (int k = 0; k < aliases.length; k++) {
            String aliase = removeUnderLine(aliases[k]);
            char[] ch = aliase.toCharArray();
            ch[0] = Character.toUpperCase(ch[0]);
            String s = new String(ch);
            String[] names = new String[]{("set" + s).intern(),
                    ("get" + s).intern(), ("is" + s).intern(),
                    ("read" + s).intern()};
            Method setter = null;
            Method getter = null;
            int length = methods.length;
            for (int i = 0; i < length; ++i) {
                Method method = methods[i];
                /**
                 * 检查该方法是否为公共方法,如果非公共方法就继续
                 */
                if (!Modifier.isPublic(method.getModifiers()))
                    continue;
                String methodName = method.getName();

                for (String name : names) {
                    if (name.equals(methodName)) {
                        if (name.startsWith("set") || name.startsWith("read"))
                            setter = method;
                        else if (name.startsWith("get") || name.startsWith("is"))
                            getter = method;

                    }
                }
            }
            if (getter != null) {
                Object param = tuple[k];
                if (param instanceof BigInteger) {
                    param = ((BigInteger) param).longValue();
                }
                if (param instanceof Timestamp) {
                    param = new Date(((Timestamp) param).getTime());
                }
                if (param instanceof Boolean) {
                    param = ((Boolean) param) ? (byte) 1 : (byte) 0;
                }
                try {
                    setter.invoke(obj, param);
                } catch (Exception e) {
                    log.info("出错时执行方法:"+setter.getName()+"  ,参数为:"+param);
                    e.printStackTrace();
                }
            }
        }
        return obj;
    }

    private String removeUnderLine(String attrName) {
        //去掉数据库字段的下划线
        if (attrName.contains("_")) {
            String[] names = attrName.split("_");
            String firstPart = names[0];
            String otherPart = "";
            for (int i = 1; i < names.length; i++) {
                String word = StrUtils.firstToUpperCase(names[i].toLowerCase());
                otherPart += word;
            }
            attrName = firstPart.toLowerCase() + otherPart;
        }
        return attrName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy