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

com.zys.mybatis.crud.Add Maven / Gradle / Ivy

There is a newer version: 0.2.7
Show newest version
package com.zys.mybatis.crud;

import com.zys.mybatis.annotation.Default;
import com.zys.mybatis.annotation.FillEnum;
import com.zys.mybatis.exception.MybatisZysException;
import com.zys.mybatis.utils.FieldUtils;
import com.zys.mybatis.utils.QueryUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.lang.reflect.Field;
import java.util.*;

/**
 * @author zys
 * @version 1.0
 * @date 2021/2/25 10:00
 */
public class Add extends AbstractQuery> {

    /**
     * 主键
     */
    private Object id;
    /**
     * add 字段
     */
    private List fields;

    public List getFields(){
        return fields;
    }
    /**
     * 添加值
     */
    private List> addValues;


    public Add(T entity){
        this.table = QueryUtils.getTableName(entity.getClass());
        init(entity);
    }

    public Add(List entity){
        init(entity);
    }

    private void init(Object entity){
        if (Collection.class.isAssignableFrom(entity.getClass())) {
            //是集合,说明是批量添加
            List entityList = (List) entity;
            this.add(entityList);
            return;
        }
        this.add((T)entity);
    }

    public void add(T entity) {
        addValues = new ArrayList<>(1);
        Map, Set> mapColumnField = FieldUtils.getMapColumnField(entity.getClass(), true, 0);
        setFields(mapColumnField);
        putAddValues(mapColumnField,entity);
    }

    public void add(Collection entity) {
        addValues = new ArrayList<>(entity.size());
        Map, Set> mapColumnField = null;
        for (T o : entity) {
            if (fields == null) {
                this.table = QueryUtils.getTableName(o.getClass());
                mapColumnField = FieldUtils.getMapColumnField(o.getClass(), true, 0);
                setFields(mapColumnField);
            }
            putAddValues(mapColumnField,o);
        }
    }

    private void setFields(Map, Set> mapColumnField){
        mapColumnField.values().forEach(s -> {
            if (fields == null) {
                fields = new ArrayList<>(s);
            }else {
                fields.addAll(s);
            }
        });
    }

    private void putAddValues(Map, Set> mapColumnField,T entity){
        Map map = new LinkedHashMap<>(fields.size());
        MetaObject metaObject = SystemMetaObject.forObject(entity);
        mapColumnField.forEach((aClass,fields) -> {
            for (String fieldName : fields) {
                try {
                    Field field = aClass.getDeclaredField(FieldUtils.toHump(fieldName).replaceAll("`",""));
                    Default aDefault = field.getDeclaredAnnotation(Default.class);
                    Object value = metaObject.getValue(field.getName());
                    map.put(fieldName, aDefault != null ? (
                            value == null ? ( ( FillEnum.INSERT.equals(aDefault.fill()) || FillEnum.INSERT_UPDATE.equals(aDefault.fill()) ) ? QueryUtils.setDefault(metaObject.getGetterType(field.getName()), aDefault.value(),aDefault.defaultValue()) : null) : value
                    ) : value);
                } catch (NoSuchFieldException e) {
                    throw new MybatisZysException("未找到字段",e);
                }
            }
        });
        addValues.add(map);
    }


    public List> getAddValues(){
        return addValues;
    }

    public Object getId(){
        return id;
    }

    public void setId(Object id){
        this.id = id;
    }



}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy