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

com.envision.sql.SqlHandler Maven / Gradle / Ivy

The newest version!
package com.envision.sql;

import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * @author qiweishi
 * @date 2018/11/10
 */
public abstract class SqlHandler {

    public final TypeToken typeToken = new TypeToken(getClass()) {
    };

    public final Type type = typeToken.getRawType();

    @Autowired
    private MarsMapper marsMapper;

    public static String tmpSql;

    @SuppressWarnings("unchecked")
    public List execute(String sql, Object... params) throws IllegalAccessException, InstantiationException {
        tmpSql = String.format(sql, params);
        List> maps = marsMapper.virtualSql();
        return maps2Objects(maps);
    }

    @SuppressWarnings("unchecked")
    public List maps2Objects(List> maps) throws InstantiationException, IllegalAccessException {
        List list = Lists.newArrayList();
        if (maps != null && maps.size() > 0) {
            Map map = null;
            T bean = null;
            for (Map map1 : maps) {
                map = map1;
                bean = ((Class) type).newInstance();
                map2Bean(map, bean);
                list.add(bean);
            }
        }
        return list;
    }

    public static  T map2Bean(Map map, T bean) {
        BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean);
        wrapper.setAutoGrowNestedPaths(true);
        wrapper.setPropertyValues(map);
        return bean;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy