SpringBoot整合多数据源实现

项目架构

在这里插入图片描述

1.导入相关依赖

<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>

        <!--freemarker支持-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!-- mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

2.application.properties

## test1 数据源配置
#spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8
#spring.datasource.test1.username=root
#spring.datasource.test1.password=123


## test2 数据源配置
#spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8
#spring.datasource.test2.username=root
#spring.datasource.test2.password=123

3.创建datasource包 下TestMyBatisConfig1

@Configuration
@MapperScan(basePackages = "cn.happy.test1", sqlSessionFactoryRef =  "test1SqlSessionFactory")
public class DataSource1Config {
        @Bean(name = "test1DataSource")
        @ConfigurationProperties(prefix = "spring.datasource.test1")
        @Primary
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "test1SqlSessionFactory")
        @Primary
        public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            //读取mybatis小配置文件
           // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
            return bean.getObject();
        }

        @Bean(name = "test1TransactionManager")
        @Primary
        public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }

        @Bean(name = "test1SqlSessionTemplate")
        @Primary
        public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
}

在这里插入图片描述
@Primary注解标识默认使用的数据源

如果不加启动会报如下错误:意思是有两个数据源 不知需要使用哪个数据源

3.创建datasource包 下TestMyBatisConfig2

@Configuration
@MapperScan(basePackages = "cn.happy.test2", sqlSessionFactoryRef =  "test2SqlSessionFactory")
public class DataSource2Config {
        @Bean(name = "test2DataSource")
        @ConfigurationProperties(prefix = "spring.datasource.test2")
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "test2SqlSessionFactory")
        public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            //读取mybatis小配置文件
           // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
            return bean.getObject();
        }

        @Bean(name = "test2TransactionManager")
        public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }

        @Bean(name = "test2SqlSessionTemplate")
        public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }

    }

4.entity层

/**
 * author:  刘涛
 *
 * @create 2018-04-04 11:18
 */
public class Book {

    private Integer bookid;

    private String bookname;

    private Integer bookprice;

get  set省略。。
}

复制代码
5.test1下的dao中BookMapperTest1

public interface BookMapperTest1 {

    @Select("select * from book where bookname=#{bookname}")
    public Book findByName(@Param("bookname") String bookname);

    @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
    public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);

    @Select("select * from book")
    public Book findBook(@Param("Book") Book book);
}

复制代码
6.test2下的dao中BookMapperTest2

public interface BookMapperTest2 {

    @Select("select * from book where bookname=#{bookname}")
    public Book findByName(@Param("bookname") String bookname);

    @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
    public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);
}

7.controller层

@RestController
public class BookController {
    @Autowired
    private BookMapperTest1 bookMapperTest1;

    @Autowired
    private BookMapperTest2 bookMapperTest2;

    @RequestMapping("insert001")
    public String insert001(String bookname,Double bookprice){
        bookMapperTest1.insertBook(bookname,bookprice);
        return "success";
    }

    @RequestMapping("insert002")
    public String insert002(String bookname,Double bookprice){
        bookMapperTest2.insertBook(bookname,bookprice);
        return "success";
    }
}

8.启动项目

在这里插入图片描述

test1数据库
在这里插入图片描述

test2数据库

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值