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

com.github.aly8246.autoConfiguration.CollectionAutoConfiguration Maven / Gradle / Ivy

Go to download

Collection Util is a mybatis plug-in designed to solve the problem of N + 1 in mysql. When you use Mysql to query linked tables and pages, there will be n + 1 problem. This plug-in can help you!

The newest version!
package com.github.aly8246.autoConfiguration;

import com.github.aly8246.collectionutil.core.PageInterface;
import com.github.aly8246.collectionutil.exception.CollectionException;
import com.github.aly8246.collectionutil.main.CollectionInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 自动装配
 *
 * @author 南有乔木
 * @version v.1.0.2
 * @see PageInterface
 * @see CollectionProperties
 * @see CollectionInterceptor
 * @see SqlSessionFactory
 * @see MybatisAutoConfiguration
 */
@Configuration
@ConditionalOnBean({SqlSessionFactory.class})
@EnableConfigurationProperties(value = CollectionProperties.class)
@AutoConfigureAfter({MybatisAutoConfiguration.class})
@Slf4j
public class CollectionAutoConfiguration {
    @Resource
    private CollectionProperties collectionProperties;
    @Resource
    private List sqlSessionFactoryList;

    /**
     * 将拦截器放置到mybatis的拦截器链
     *
     * @author 南有乔木
     * @see CollectionInterceptor
     */
    @PostConstruct
    public void addPageInterceptor() {
        Supplier interceptorSupplier = CollectionInterceptor::new;
        CollectionInterceptor interceptor = interceptorSupplier.get();
        Supplier propertiesSupplier = Properties::new;
        Properties properties = propertiesSupplier.get();
        properties.putAll(properties);
        properties.putAll(this.collectionProperties.getProperties());
        interceptor.setProperties(properties);
        log.info("CollectionInterceptor init!");
        this.sqlSessionFactoryList.forEach(sqlSessionFactory -> {
            log.debug("CollectionUtil addPageInterceptor");
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        });

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy