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

io.github.portaldalaran.talons.meta.AssociationTableInfo Maven / Gradle / Ivy

Go to download

Expand the toolkit for query, save, and delete Mybatis plus entity associations 对于Mybatis plu的扩展工具,包括关联表查询,以及级联保存、更新、删除

The newest version!
package io.github.portaldalaran.talons.meta;


import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.google.common.collect.Lists;
import io.github.portaldalaran.talons.annotation.*;
import io.github.portaldalaran.talons.exception.TalonsException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangxiaoli
 * @version 0.1
 * @date 2021/5/30 21:03
 * @email [email protected]
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AssociationTableInfo {
    private String name;
    private Class tableClass;
    private TableInfo tableInfo;
    private List oneToManys;
    private List manyToOnes;
    private List manyToManys;

    /**
     * 根据实体对象,从注解里初始化表关系
     *
     * @param modelClass 实体对象Class
     * @param         实体对象类型
     * @return
     * @throws TalonsException
     * @return: associationTableInfo 表关系对象
     */
    public static  AssociationTableInfo instance(Class modelClass) throws TalonsException {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(modelClass);
        AssociationTableInfo associationTableInfo = new AssociationTableInfo<>();
        associationTableInfo.setTableInfo(tableInfo);
        associationTableInfo.setTableClass(modelClass);
        associationTableInfo.setName(modelClass.getSimpleName());

        //取mybatis中注册的mapper
        List> mappers = new ArrayList<>(SqlHelper.sqlSession(modelClass).getConfiguration().getMapperRegistry().getMappers());

        //因为有的实体设置了@TableField(exist = false) 在TableInfo里取不到
        List fieldList = Arrays.asList(associationTableInfo.getTableClass().getDeclaredFields());

        associationTableInfo.setOneToManys(initOne2Manys(fieldList, mappers));
        associationTableInfo.setManyToOnes(initMany2Ones(fieldList, mappers));
        associationTableInfo.setManyToManys(initMany2Manys(fieldList, mappers));
        return associationTableInfo;
    }

    /**
     * 根据ManyToMany注解,初始化多对多字段
     *
     * @param fieldList 字段列表
     * @return 字段关系列表
     */
    private static List initMany2Manys(List fieldList, List> mappers) throws TalonsException {
        List many2Manys = new ArrayList<>();
        //取所有多对多字段
        List m2mTableFields = fieldList.stream().filter(field -> ObjectUtils.isNotEmpty(field.getAnnotation(ManyToMany.class)))
                .collect(Collectors.toList());
        for (Field field : m2mTableFields) {
            AssociationFieldInfo associationField = new AssociationFieldInfo();
            associationField.initManyToMany(field,
                    AnnotationUtils.getAnnotation(field, ManyToMany.class),
                    AnnotationUtils.getAnnotation(field, JoinTable.class),
                    mappers);
            many2Manys.add(associationField);
        }
        return many2Manys;
    }

    /**
     * 根据ManyToOne注解,初始化多对一字段
     *
     * @param fieldList 字段列表
     * @return 字段关系列表
     */
    private static List initMany2Ones(List fieldList, List> mappers) throws TalonsException {
        List many2Ones = Lists.newArrayList();
        //取所有多对多字段
        List manyToOneTableFields = fieldList.stream().filter(field -> ObjectUtils.isNotEmpty(field.getAnnotation(ManyToOne.class)))
                .collect(Collectors.toList());
        for (Field field : manyToOneTableFields) {
            AssociationFieldInfo associationField = new AssociationFieldInfo();
            associationField.initManyToOne(field,
                    AnnotationUtils.getAnnotation(field, ManyToOne.class),
                    AnnotationUtils.getAnnotation(field, JoinColumn.class), mappers);
            many2Ones.add(associationField);
        }
        return many2Ones;
    }

    /**
     * 根据OneToMany注解,初始化一对多字段
     *
     * @param fieldList 字段列表
     * @return 字段关系列表
     */
    private static List initOne2Manys(List fieldList, List> mappers) throws TalonsException {
        List one2Manys = Lists.newArrayList();
        //取所有一对多字段
        List oneToManyTableFields = fieldList.stream().filter(field -> ObjectUtils.isNotEmpty(field.getAnnotation(OneToMany.class)))
                .collect(Collectors.toList());
        for (Field field : oneToManyTableFields) {
            AssociationFieldInfo associationField = new AssociationFieldInfo();
            associationField.initOneToMany(field,
                    AnnotationUtils.getAnnotation(field, OneToMany.class),
                    AnnotationUtils.getAnnotation(field, JoinTable.class),
                    AnnotationUtils.getAnnotation(field, JoinColumn.class), mappers);
            one2Manys.add(associationField);
        }
        return one2Manys;
    }

    public List getAnnotations() {
        List annotations = new ArrayList<>();
        annotations.addAll(oneToManys);
        annotations.addAll(manyToOnes);
        annotations.addAll(manyToManys);
        return annotations;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy