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

com.lx.boot.db.DynamicSqlSessionFactory Maven / Gradle / Ivy

Go to download

使用文档: https://a7fi97h1rc.feishu.cn/docx/X3LRdtLhkoXQ8hxgXDQc2CLOnEg?from=from_copylink

There is a newer version: 1.1
Show newest version
package com.lx.boot.db;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.lx.constant.DefaultBaseConstant;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.io.IOException;
import java.util.Properties;

@Configuration
@ConditionalOnBean(DynamicDataSource.class)
public class DynamicSqlSessionFactory implements BeanPostProcessor {

    public DynamicSqlSessionFactory(DynamicDataSource dynamicDataSource) throws IOException {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        // 配置数据源,此处配置为关键配置,如果没有将 dynamicDataSource作为数据源则不能实现切换
        sessionFactory.setDataSource(dynamicDataSource);
    }


    private final String PROPAGATION_NAME_CHANGE = "PROPAGATION_REQUIRED,-Exception";

    @Autowired
    private DataSourceTransactionManager transactionManager;
    // 创建事务通知
    @Bean(name = "txAdvice")
    @ConditionalOnMissingBean(TransactionInterceptor.class)
    @ConditionalOnProperty(value = DefaultBaseConstant.OPEN_AUTO_TRANSACTION_INTERCEPTOR ,havingValue = "true" , matchIfMissing = true)
    public TransactionInterceptor transactionInterceptor() throws Exception {
        Properties properties = new Properties();
        properties.setProperty("add*", PROPAGATION_NAME_CHANGE);
        properties.setProperty("save*", PROPAGATION_NAME_CHANGE);
        properties.setProperty("insert*", PROPAGATION_NAME_CHANGE);
        properties.setProperty("update*", PROPAGATION_NAME_CHANGE);
        properties.setProperty("delete*", PROPAGATION_NAME_CHANGE);
        TransactionInterceptor tsi = new TransactionInterceptor(transactionManager, properties);
        return tsi;
    }
    @Bean
    @ConditionalOnBean(TransactionInterceptor.class)
    @ConditionalOnMissingBean(BeanNameAutoProxyCreator.class)
    public BeanNameAutoProxyCreator txProxy() {
        BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
        creator.setInterceptorNames("txAdvice");
        creator.setBeanNames("*Service", "*ServiceImpl");//指定后缀类加事务
        creator.setProxyTargetClass(true);
        return creator;
    }

    /** 分页配置 */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    /**  防止修改与删除时对全表进行操作 */
    @Bean
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
        return new BlockAttackInnerInterceptor();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy