第八章 springboot + mybatis + 多數據源2(解決循環引用)


解決了循環引用

1.application.properties

#the first datasource
jdbc.names:1,2
jdbc1.driverClassName = com.mysql.jdbc.Driver
jdbc1.url = jdbc:mysql://192.168.37.102:3306/demo_ds_1?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
jdbc1.username = root
jdbc1.password = root

#the second datasource
jdbc2.driverClassName = com.mysql.jdbc.Driver
jdbc2.url = jdbc:mysql://192.168.37.102:3306/demo_ds_2?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
jdbc2.username = root
jdbc2.password = root

mybatis.typeAliasesPackage=com.example.abstractroutingdatasource.entity
mybatis.mapperLocations:classpath*:mapper/*.xml  //在resource文件夾下

2.MyBatisConfig

package com.example.abstractroutingdatasource.config;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * springboot集成mybatis的基本入口 1)創建數據源(如果采用的是默認的tomcat-jdbc數據源,則不需要)
 * 2)創建SqlSessionFactory 3)配置事務管理器,除非需要使用事務,否則不用配置
 */
@Configuration // 該注解類似於spring配置文件
@MapperScan(basePackages = "com.example.abstractroutingdatasource.mapper")
public class MyBatisConfig3 {

    @Autowired
    private Environment env;


    /**
     * @Primary 該注解表示在同一個接口有多個實現類可以注入的時候,默認選擇哪一個,而不是讓@autowire注解報錯
     * @Qualifier 根據名稱進行注入,通常是在具有相同的多個類型的實例的一個注入(例如有多個DataSource類型的實例)
     */
    @Bean
    @Primary
//此方法的解決了循環引用
    public DynamicDataSource dataSource()  throws Exception {

        Map<Object, Object> targetDataSources = new HashMap<>();
        String jdbcNames=env.getProperty("jdbc.names"); 
for (String s:jdbcNames.split(",")) { 
    Properties props = new Properties();
    props.put("driverClassName", env.getProperty("jdbc"+s+".driverClassName"));
    props.put("url", env.getProperty("jdbc"+s+".url"));
    props.put("username", env.getProperty("jdbc"+s+".username"));
    props.put("password", env.getProperty("jdbc"+s+".password"));
    targetDataSources.put(s,DruidDataSourceFactory.createDataSource(props) );
}
        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSources);// 該方法是AbstractRoutingDataSource的方法
        dataSource.setDefaultTargetDataSource(targetDataSources.get("1"));// 默認的datasource設置為myTestDbDataSource
        return  dataSource;
    }

    /**
     * 根據數據源創建SqlSessionFactory
     */
//    @Bean
//    public SqlSessionFactory sqlSessionFactory(@Qualifier("myTestDbDataSource") DataSource myTestDbDataSource,
//                                               @Qualifier("myTestDb2DataSource") DataSource myTestDb2DataSource) throws Exception{
//        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
//        fb.setDataSource(this.dataSource(myTestDbDataSource, myTestDb2DataSource));
//        fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));
//        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));
//        return fb.getObject();
//    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DynamicDataSource dataSource) throws Exception{
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(dataSource);
        fb.setTypeAliasesPackage(env.getProperty("mybatis.typeAliasesPackage"));
        fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));
        return fb.getObject();
    }
    /**
     * 配置事務管理器
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DynamicDataSource dataSource) throws Exception {
        return new DataSourceTransactionManager(dataSource);
    }




}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM