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

cn.net.vidyo.yd.common.data.dao.JPARowMapper Maven / Gradle / Ivy

package cn.net.vidyo.yd.common.data.dao;

import org.springframework.jdbc.core.RowMapper;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class JPARowMapper implements RowMapper {

    private Class targetClazz;
    public JPARowMapper(Class targetClazz) {
        this.targetClazz = targetClazz;
    }


    @Override
    public T mapRow(ResultSet rs, int arg1) throws SQLException {
        T obj = null;
        try {
            obj = (T) targetClazz.newInstance();
            Field[] fields = targetClazz.getDeclaredFields();
            for (Field field : fields) {
                String columnName = field.getName().toUpperCase();
                if (isNULL(rs, columnName) || isCustomType(field)) {
                    boolean isAccessible = field.isAccessible();
                    field.setAccessible(true);
                    if (isBasicType(field)) {
                        field.set(obj, rs.getObject(columnName));
                    } else if (isCustomType(field)) {
                        @SuppressWarnings("unchecked")
                        T obj1=(T) field.getType().newInstance();
                        Field[] fields2 = obj1.getClass().getDeclaredFields();
                        for (Field field2 : fields2) {
                            String columnName1 = field2.getName().toUpperCase();
                            if (isNULL(rs, columnName1)) {


                                boolean isAccessible1 = field2.isAccessible();
                                field2.setAccessible(true);
                                if (isBasicType(field2)) {
                                    field2.set(obj1, rs.getObject(columnName1));
                                }
                                field2.setAccessible(isAccessible1);
                            }
                        }
                        field.set(obj, obj1);
                    }


                    field.setAccessible(isAccessible);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    /**
     * 此方法用于自定类型,类对象映射,


     * 被映射的子类对象不能有自定义类型(如有自定类型 会映射不上)
     * @param field 反射的单个属性
     * @return lll
     */
    protected  boolean isCustomType (Field field) {
        return false;
    }

    private boolean isBasicType(Field field){
        if (field.getType() == int.class ||field.getType() ==Integer.class) { // int
            return true;
        } else if (field.getType() == boolean.class || field.getType() == Boolean.class) { // boolean


            return true;
        } else if (field.getType() == String.class) { // string
            return true;
        } else if (field.getType() == float.class) { // float
            return true;
        } else if (field.getType() ==double.class || field.getType() == Double.class) { // double
            return true;
        } else if (field.getType() == BigDecimal.class) { // bigdecimal
            return true;
        } else if (field.getType() == short.class || field.getType() == Short.class) { // short


            return true;
        } else if (field.getType() == Date.class ||java.util.Date.class.isAssignableFrom(field.getType())) { // date
            return true;
        } else if (field.getType() == Timestamp.class) { // timestamp
            return true;
        } else if (field.getType() == Long.class || field.getType() == long.class) { // long
            return true;
        }
        return false;
    }


    private  boolean isNULL(ResultSet rst, String columnName){
        try {
            if(rst.findColumn(columnName)>0){
                return  true;
            }
        } catch (Exception e) {
            return  false;
        }
        return false;
    }




}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy