sprignboot+mybatis多数据源配置

9 篇文章 0 订阅
1 篇文章 0 订阅

1.先配置yml

spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/security
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver

2.配置数据源

(1)先配置一个主数据源

import javax.sql.DataSource;

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.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;


@Configuration
@MapperScan(basePackages = PrimaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "rdsSqlSessionFactory")
public class PrimaryDataSourceConfig {
    static final String PACKAGE = "com.example.many.sourt.mybatis.mapper.primary";

    @Value("${spring.datasource.primary.jdbc-url}")
    private String dbUrl;
    @Value("${spring.datasource.primary.username}")
    private String dbUser;
    @Value("${spring.datasource.primary.password}")
    private String dbPassword;

    @Bean(name = "rdsDataSource")
    @Primary
    public DataSource rdsDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPassword);
        return dataSource;
    }

    @Bean(name = "rdsTransactionManager")
    @Primary
    public DataSourceTransactionManager rdsTransactionManager() {
        return new DataSourceTransactionManager(rdsDataSource());
    }

    @Bean(name = "rdsSqlSessionFactory")
    @Primary
    public SqlSessionFactory rdsSqlSessionFactory(@Qualifier("rdsDataSource") DataSource rdsDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(rdsDataSource);
        return sessionFactory.getObject();
    }
}

(2)配置从数据源

import javax.sql.DataSource;

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.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = SecondaryDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "adsSqlSessionFactory")
public class SecondaryDataSourceConfig {
    static final String PACKAGE = "com.example.many.sourt.mybatis.mapper.secondry";

    @Value("${spring.datasource.secondary.jdbc-url}")
    private String dbUrl;
    @Value("${spring.datasource.secondary.username}")
    private String dbUser;
    @Value("${spring.datasource.secondary.password}")
    private String dbPassword;

    @Bean(name = "adsDataSource")
    public DataSource adsDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl(dbUrl);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPassword);
        return dataSource;
    }

    @Bean(name = "adsTransactionManager")
    public DataSourceTransactionManager adsTransactionManager(@Qualifier("adsDataSource") DataSource adsDataSource) {
        return new DataSourceTransactionManager(adsDataSource);
    }

    @Bean(name = "adsSqlSessionFactory")
    public SqlSessionFactory adsSqlSessionFactory(@Qualifier("adsDataSource") DataSource adsDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(adsDataSource);
        return sessionFactory.getObject();
    }
}

3.写Mapper

(1)主mapper

import com.example.many.sourt.mybatis.entity.Stu;

@Mapper
public interface StuMapper {

	@Select("SELECT * FROM Stu WHERE id = #{id}")
	@Results({
	    @Result(property = "id", column = "id")
	})//映射对应
	Stu selectStu(int id);
	
}

(2)从mapper

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.example.many.sourt.mybatis.entity.Classes;

@Mapper
public interface ClassesMapper {

	@Select("SELECT * FROM Classes WHERE id = #{id}")
	Classes selectById(int id);
	
}

4.service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.many.sourt.mybatis.entity.Classes;
import com.example.many.sourt.mybatis.entity.Stu;
import com.example.many.sourt.mybatis.mapper.primary.StuMapper;
import com.example.many.sourt.mybatis.mapper.secondry.ClassesMapper;
import com.example.many.sourt.mybatis.service.StuService;


@Service
public class StuServiceImpl implements StuService{

	@Autowired
	private StuMapper mapper;
	
	@Autowired
	private ClassesMapper classes;
	
	@Override
	public Stu selectStu() {
		return mapper.selectStu(2);
	}

	@Override
	public Classes selectByName() {
		return classes.selectById(1);
	}

}

5.controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.many.sourt.mybatis.entity.Classes;
import com.example.many.sourt.mybatis.entity.Stu;
import com.example.many.sourt.mybatis.service.StuService;

@RestController
public class StuMapperController {
	
	@Autowired
	StuService service;
	
	@GetMapping("/mapper/stu")
	@ResponseBody
	public Stu selectStu() {
		return service.selectStu();
	}

	@GetMapping("/mapper/name")
	@ResponseBody
	public Classes selectStuByName() {
		return service.selectByName();
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值