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

link.jfire.sql.function.impl.MapBeanImpl Maven / Gradle / Ivy

package link.jfire.sql.function.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import link.jfire.baseutil.collection.set.LightSet;
import link.jfire.baseutil.reflect.ReflectUtil;
import link.jfire.sql.annotation.SqlIgnore;
import link.jfire.sql.field.MapField;
import link.jfire.sql.function.MapBean;
import link.jfire.sql.util.DaoFactory;

public class MapBeanImpl implements MapBean
{
    private MapField[]                mapFields;
    private HashMap fieldMap = new HashMap<>();
    private Class                  entityClass;
    
    public MapBeanImpl(Class entityClass)
    {
        this.entityClass = entityClass;
        LightSet set = new LightSet<>();
        for (Field each : ReflectUtil.getAllFields(entityClass))
        {
            if (each.isAnnotationPresent(SqlIgnore.class) || Map.class.isAssignableFrom(each.getType()) || List.class.isAssignableFrom(each.getType()) || each.getType().isInterface() || each.getType().isArray() || Modifier.isStatic(each.getModifiers()))
            {
                continue;
            }
            set.add(DaoFactory.buildMapField(each));
        }
        mapFields = set.toArray(MapField.class);
        for (MapField each : mapFields)
        {
            fieldMap.put(each.getColName(), each);
        }
    }
    
    @Override
    public List toBean(ResultSet resultSet) throws SQLException, InstantiationException, IllegalAccessException
    {
        List list = new ArrayList<>();
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int colNum = resultSetMetaData.getColumnCount();
        MapField[] mapFields = new MapField[colNum];
        int index = 0;
        MapField mapField = null;
        for (int i = 0; i < colNum; i++)
        {
            mapField = fieldMap.get(resultSetMetaData.getColumnLabel(i + 1));
            if (mapField != null)
            {
                mapFields[index++] = mapField;
            }
        }
        while (resultSet.next())
        {
            T entity = entityClass.newInstance();
            for (int i = 0; i < index; i++)
            {
                mapFields[i].setEntityValue(entity, resultSet);
            }
            list.add(entity);
        }
        return list;
    }
    
    @Override
    public T singleResultToBean(ResultSet resultSet) throws SQLException, InstantiationException, IllegalAccessException
    {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        int colNum = resultSetMetaData.getColumnCount();
        int num = 0;
        T entity = entityClass.newInstance();
        while (resultSet.next())
        {
            for (int i = 0; i < colNum; i++)
            {
                MapField mapField = fieldMap.get(resultSetMetaData.getColumnName(i + 1));
                if (mapField != null)
                {
                    mapField.setEntityValue(entity, resultSet);
                }
            }
            num++;
            if (num > 1)
            {
                throw new RuntimeException("查询结果不是唯一的,请检查");
            }
        }
        if (num == 0)
        {
            return null;
        }
        else
        {
            return entity;
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy