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

com.ly.mybatis.mapperservice.service.builder.RepositoryTemplateBuilder Maven / Gradle / Ivy

package com.ly.mybatis.mapperservice.service.builder;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ly.mybatis.mapperservice.annotation.interceptor.MapperService;
import com.ly.mybatis.mapperservice.table.MyBatisTableUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;

import java.util.Comparator;
import java.util.Optional;

public class RepositoryTemplateBuilder {
    private final ApplicationContext applicationContext;

    private final BaseMapper mapper;
    private Class mapperClass;
    private Class entityClass;
    private Class boClass;
    private Class formClass;
    private Class conditionFormClass;
    private Object[] interceptors;

    public RepositoryTemplateBuilder(ApplicationContext applicationContext, BaseMapper mapper) {
        this.applicationContext = applicationContext;
        this.mapper = mapper;
    }

    public boolean parse() {
        parseMapperClass();
        parseEntityClass();
        if (entityClass == null) {
            return false;
        }
        parseBoClass();
        parseFormClass();
        parseConditionFormClass();
        parseInterceptors();
        return true;
    }

    public RepositoryTemplate getTemplate() {
        RepositoryTemplate template = new RepositoryTemplate();
        template.setBaseMapper(mapper)
                .setMapperClass(mapperClass)
                .setEntityClass(entityClass)
                .setBoClass(boClass)
                .setFormClass(formClass)
                .setConditionFormClass(conditionFormClass)
                .setInterceptors(interceptors);
        return template;
    }

    public Class getEntityClass() {
        return entityClass;
    }

    public Class getBoClass() {
        return boClass;
    }

    public Class getFormClass() {
        return formClass;
    }

    public Class getConditionFormClass() {
        return conditionFormClass;
    }

    private void parseMapperClass() {
        if (mapper.getClass().getName().startsWith("com.sun.proxy")) {
            for (Class anInterface : mapper.getClass().getInterfaces()) {
                if (BaseMapper.class.isAssignableFrom(anInterface)) {
                    mapperClass = anInterface;
                    break;
                }
            }
        }
    }

    private void parseEntityClass() {
        entityClass = ResolvableType.forClass(mapperClass).as(BaseMapper.class).resolveGeneric(0);
    }

    private void parseBoClass() {
        boClass = MyBatisTableUtil.getTableInfo(entityClass).getBoType();
    }

    private void parseFormClass() {
        formClass = MyBatisTableUtil.getTableInfo(entityClass).getFormType();
    }

    private void parseConditionFormClass() {
        conditionFormClass = MyBatisTableUtil.getTableInfo(entityClass).getConditionFormType();
    }

    private void parseInterceptors() {
        interceptors = applicationContext.getBeansWithAnnotation(MapperService.class).values().stream()
                                         .filter(bean -> AnnotationUtils.findAnnotation(
                                                                                bean.getClass(), MapperService.class)
                                                                        .value().isAssignableFrom(getEntityClass()))
                                         .sorted(Comparator.comparingInt(interceptor -> Optional.ofNullable(
                                                                                                        AnnotationUtils.findAnnotation(interceptor.getClass(), Order.class))
                                                                                                .map(Order::value)
                                                                                                .orElse(Ordered.LOWEST_PRECEDENCE)))
                                         .toArray();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy