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

io.github.dengchen2020.jdbc.base.BaseJdbcRepositoryFactoryBean Maven / Gradle / Ivy

package io.github.dengchen2020.jdbc.base;

import com.querydsl.sql.Configuration;
import com.querydsl.sql.MySQLTemplates;
import com.querydsl.sql.SQLQueryFactory;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory;
import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactoryBean;
import org.springframework.data.mapping.callback.EntityCallbacks;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.lang.NonNull;
import javax.sql.DataSource;
import java.io.Serializable;

/**
 * @author dengchen
 * @since 2024/6/16
 */
public class BaseJdbcRepositoryFactoryBean, S, ID extends Serializable> extends JdbcRepositoryFactoryBean {

    @Resource
    private ApplicationEventPublisher publisher;
    @Resource
    private BeanFactory beanFactory;
    @Resource
    private RelationalMappingContext mappingContext;
    @Resource
    private JdbcConverter converter;
    @Resource
    private DataAccessStrategy dataAccessStrategy;
    private final QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
    @Resource
    private NamedParameterJdbcOperations operations;

    private EntityCallbacks entityCallbacks;
    @Resource
    private Dialect dialect;

    @Resource
    private DataSource dataSource;
    private static SQLQueryFactory queryFactory;

    /**
     * Creates a new {@link JdbcRepositoryFactoryBean} for the given repository interface.
     *
     * @param repositoryInterface must not be {@literal null}.
     */
    public BaseJdbcRepositoryFactoryBean(Class repositoryInterface) {
        super(repositoryInterface);
    }

    @Override
    public void afterPropertiesSet() {
        if (beanFactory != null) {
            entityCallbacks = EntityCallbacks.create(beanFactory);
        }
        super.afterPropertiesSet();
    }

    @NonNull
    @Override
    protected RepositoryFactorySupport doCreateRepositoryFactory() {
        if(queryFactory == null) queryFactory = new SQLQueryFactory(new Configuration(new MySQLTemplates()),dataSource);
        BaseRepositoryFactory jdbcRepositoryFactory = new BaseRepositoryFactory(dataAccessStrategy, mappingContext,
                converter, dialect, publisher, operations, entityCallbacks, queryFactory);
        jdbcRepositoryFactory.setQueryMappingConfiguration(queryMappingConfiguration);
        jdbcRepositoryFactory.setEntityCallbacks(entityCallbacks);
        jdbcRepositoryFactory.setBeanFactory(beanFactory);
        return jdbcRepositoryFactory;
    }

    //创建一个内部类,该类不用在外部访问
    private static class BaseRepositoryFactory
            extends JdbcRepositoryFactory {

        private final NamedParameterJdbcOperations operations;

        private final ApplicationEventPublisher publisher;

        private final RelationalMappingContext context;

        private final JdbcConverter converter;

        private final DataAccessStrategy accessStrategy;

        private final EntityCallbacks entityCallbacks;

        private final SQLQueryFactory queryFactory;

        public BaseRepositoryFactory(DataAccessStrategy dataAccessStrategy, RelationalMappingContext context, JdbcConverter converter, Dialect dialect, ApplicationEventPublisher publisher, NamedParameterJdbcOperations operations, EntityCallbacks entityCallbacks, SQLQueryFactory queryFactory) {
            super(dataAccessStrategy, context, converter, dialect, publisher, operations);
            this.operations = operations;
            this.publisher = publisher;
            this.context = context;
            this.converter = converter;
            this.accessStrategy = dataAccessStrategy;
            this.entityCallbacks = entityCallbacks;
            this.queryFactory = queryFactory;
        }

        @NonNull
        @Override
        protected Object getTargetRepository(@NonNull RepositoryInformation repositoryInformation) {
            JdbcAggregateTemplate template = new JdbcAggregateTemplate(publisher, context, converter, accessStrategy);

            if (entityCallbacks != null) {
                template.setEntityCallbacks(entityCallbacks);
            }

            RelationalPersistentEntity persistentEntity = context
                    .getRequiredPersistentEntity(repositoryInformation.getDomainType());

            return getTargetRepositoryViaReflection(repositoryInformation, template, persistentEntity,
                    converter, operations, queryFactory);
        }

        //设置具体的实现类的class
        @NonNull
        @Override
        protected Class getRepositoryBaseClass(@NonNull RepositoryMetadata metadata) {
            return BaseJdbcRepositoryImpl.class;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy