springboot 第十九节 starter and muti_datasource 多数据源

本文详细介绍如何在SpringBoot项目中配置并使用多个数据源,包括数据库连接设置、MyBatis整合、事务管理及测试案例。通过具体代码示例,展示如何在不同数据源间进行操作,适用于复杂业务场景下的数据隔离需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot 第十九节 starter and muti_datasource 多数据源

1、

spring.datasource.spring.driverClassName=com.mysql.jdbc.Driver
spring.datasource.spring.jdbcUrl=jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=GMT%2B8
spring.datasource.spring.username=root
spring.datasource.spring.password=root

spring.datasource.spring2.driverClassName=com.mysql.jdbc.Driver
spring.datasource.spring2.jdbcUrl=jdbc:mysql://127.0.0.1:3306/springcloud?serverTimezone=GMT%2B8
spring.datasource.spring2.username=root
spring.datasource.spring2.password=root

2、

package com.tx.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;


@Configuration
@MapperScan(basePackages = "com.tx.dao.orders", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
    @Bean(name = "test2DataSource")
  /*  主要是这里,配置了数据源2*/
    @ConfigurationProperties(prefix = "spring.datasource.spring2")
    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);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/orders/*.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);
    }

}

3、

package com.tx.service.impl;




import com.tx.dao.orders.OrdersMapper;
import com.tx.dao.users.UsersMapper;
import com.tx.model.Orders;
import com.tx.model.Users;
import com.tx.service.IOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class OrderServiceImpl  implements IOrderService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private OrdersMapper ordersMapper;

    @Override
   @Transactional
    public void addOrder(Orders orders, Users users) {
        usersMapper.insertSelective(users);
      
        ordersMapper.insertSelective(orders);
    }
}

4、最后测试:


import com.tx.App;
import com.tx.model.Orders;
import com.tx.model.Users;
import com.tx.service.IOrderService;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest(classes = {App.class})
@RunWith(SpringRunner.class)
public class Test {

    @Resource
    private IOrderService iOrderService;

    @org.junit.Test
    public void test1() {
        Users users = new Users();
        users.setUsername("enjoy34");
        users.setPasswd("haha34");
        users.setId(34);

        Orders orders = new Orders();
        orders.setAccount(34);
        orders.setName("娃娃34");
        orders.setUserId(34);
        iOrderService.addOrder(orders,users);
    }
}

代码示例:https://gitee.com/dgx555/springboot/tree/master/springboot_enjoy_03_starter_andmuti_datasource

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值