spring-boot2.0 Mybatis多数据源配置

spring-boot2.0 Mybatis多数据源配置

spring-boot2.0 Mybatis多数据源配置

1.首先贴出pom.xml文件


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demoT002</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demoT002</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

         <!-- MyBatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
         <!-- MySql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.然后是application.properties

#数据源1

spring.datasource.titan-master.jdbc-url=jdbc:mysql://192.168.7.284:3306/account?useUnicode=true&characterEncoding=utf-8
spring.datasource.titan-master.username=uat_api_java
spring.datasource.titan-master.password=54GDfethu634tIi6778FjsB
spring.datasource.titan-master.driver-class-name=com.mysql.jdbc.Driver

#数据源2
spring.datasource.db2.jdbc-url=jdbc:mysql://192.168.7.284:3306/basic_business?useUnicode=true&characterEncoding=utf-8
spring.datasource.db2.username=uat_api_java
spring.datasource.db2.password=54GDfethurSDFvhyU5634tIFjsB
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver

注意这边不是url而使用的是jdbc-url

3.配置文件

主文件中:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//exclude 排除自动读取数据源 需要添加两个数据库
@ServletComponentScan
public class DemoT002Application {

    public static void main(String[] args) {
        SpringApplication.run(DemoT002Application.class, args);
    }
}

DataSource配置:

@Configuration
public class DataSourceConfig {
     @Bean(name = "ds1")
     @Primary 
        @ConfigurationProperties(prefix = "spring.datasource.titan-master") // application.properteis中对应属性的前缀
        public DataSource dataSource1() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "ds2")
        @ConfigurationProperties(prefix = "spring.datasource.db2") // application.properteis中对应属性的前缀
        public DataSource dataSource2() {
            return DataSourceBuilder.create().build();
        }
}

第一个连接配置:

@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.account"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {
    @Autowired
    @Qualifier("ds1")
    private DataSource ds1;

    @Bean
    @Primary 
    public SqlSessionFactory sqlSessionFactory1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds1); // 使用titan数据源, 连接titan库
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/account/*.xml"));
        return factoryBean.getObject();

    }

    @Bean
    @Primary 
    public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
        return template;
    }
}

第二个连接配置:

@Configuration
@MapperScan(basePackages = {"com.example.demo.mapper.basicbusiness"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
    @Autowired
    @Qualifier("ds2")
    private DataSource ds2;

    @Bean
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(ds2);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/com/example/demo/mapper/basicbusiness/*.xml"));

        return factoryBean.getObject();

    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
        return template;
    }
}

转载于:https://blog.51cto.com/13788390/2124422

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101628.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号