Springboot + Druid + Mybatis多数据源配置

配置多个数据源,mysql和pgsql,用mybatis去连接

1、pom引入

 
        //mybatis 可换mybatis-plus
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        //druid alibaba数据库连接池
        
           com.alibaba
           druid-spring-boot-starter
           1.1.9
           
        
        //postgresql
        
            org.postgresql
            postgresql
            runtime
        
        //mysql
        
            mysql
            mysql-connector-java
            runtime
        

2、配置文件

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author lizhengmin
 * @date 2021/8/30 14:39
 * @description
 */
@Configuration
@MapperScan(basePackages = OtherDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "otherSqlSessionFactory")
public class OtherDataSourceConfig {

    public final static String PACKAGE = "com.szu.analysis.mapper.dataSource";

    public final static String MAPPER_LOCATION = "classpath:mybatis/dataSourceMapper/*.xml";

    /**
     * 连接数据库信息 这个其实更好的是用配置中心完成
     */
    @Value("${other.datasource.url}")
    private String url;

    @Value("${other.datasource.username}")
    private String username;

    @Value("${other.datasource.password}")
    private String password;

    @Value("${other.datasource.driver-class-name}")
    private String driverClassName;

    @Bean("otherDataSource")
    public DataSource otherDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    @Bean(name = "otherTransactionManager")
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(otherDataSource());
    }

    @Bean(name = "otherSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("otherDataSource") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(OtherDataSourceConfig.MAPPER_LOCATION));
        //sessionFactory.setTypeAliasesPackage(PACKAGE);
        //设定SQL语句输出
//        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
//        configuration.setLogPrefix(PACKAGE);
//        configuration.setLogImpl(StdOutImpl.class);
//        sessionFactory.setConfiguration(configuration);
        return sessionFactory.getObject();
    }


}

需要注意的就是不同数据源,最好分开包存放。pgsql的JDBC包为

org.postgresql.Driver

如果xml文件在pom引入的Module中,mapper地址则写成下方这种即可

classpath*:mybatis/*.xml

3、Mapper类和mybatis的xml文件:

照普通mybatis使用即可,记得不同数据源放入不同的包,并在配置文件中指定

你可能感兴趣的:(java,springcloud,postgresql)