springBoot 整合Mybatis 多数据源

在微服务里面,通常要用很多的数据库,所以学习了下 配置多数据源。

maven 依赖

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

扫描xml

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

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

配置application.properties

spring.datasource.member.password=root
spring.datasource.member.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.member.url=jdbc:mysql://rm-bp135e84093h525rt4o.mysql.rds.aliyuncs.com:3306/member?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=UTC
spring.datasource.member.username=root

spring.datasource.system.password=root
spring.datasource.system.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.system.url=jdbc:mysql://rm-bp135e84093h525rt4o.mysql.rds.aliyuncs.com:3306/system?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=UTC
spring.datasource.system.username=root

数据源配置

@Configuration
public class DataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.member")
    DataSource dsMember() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.system")
    DataSource dsSystem() {
        return DruidDataSourceBuilder.create().build();
    }
}
@Configuration
@MapperScan(basePackages = "com.example.myabatis.member.mapper",sqlSessionFactoryRef = "sqlSessionFactoryMember",sqlSessionTemplateRef = "sqlSessionTemplateMember")
public class MyBatisConfigMember {
    @Resource(name = "dsMember")
    DataSource dsMember;

    @Bean
    SqlSessionFactory sqlSessionFactoryMember() {
        SqlSessionFactory sessionFactory = null;
        try {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dsMember);
            sessionFactory = bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sessionFactory;
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplateMember() {
        return new SqlSessionTemplate(sqlSessionFactoryMember());
    }
}
@Configuration
@MapperScan(basePackages = "com.example.myabatis.system.mapper",sqlSessionFactoryRef = "sqlSessionFactorySystem",sqlSessionTemplateRef = "sqlSessionTemplateSystem")
public class MyBatisConfigSystem {
    @Resource(name = "dsSystem")
    DataSource dsSystem;

    @Bean
    SqlSessionFactory sqlSessionFactorySystem() {
        SqlSessionFactory sessionFactory = null;
        try {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dsSystem);
            sessionFactory = bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sessionFactory;
    }
    @Bean
    SqlSessionTemplate sqlSessionTemplateSystem() {
        return new SqlSessionTemplate(sqlSessionFactorySystem());
    }
}

创建Mapper

public interface SystemMapper {

    int systemSum();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.myabatis.system.mapper.SystemMapper">


    <select id="systemSum" resultType="int">
        select  count(*) from system_user
    </select>
</mapper>
public interface MemberMapper {

    int memberSum();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.myabatis.member.mapper.MemberMapper">

    <select id="memberSum" resultType="int">
     select  count(*) from es_member_message
    </select>
</mapper>

如图
在这里插入图片描述

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页