springBoot多数据源

  1. 配置数据源1

 

package cn.com.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
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.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl;



@Configuration
@PropertySource({
        "classpath:jdbc.properties"
})
@MapperScan(basePackages = "cn.com.demo.dao.yun.*.mapper", sqlSessionTemplateRef = "yunSqlSessionTemplate")
public class DataSourceConfig {
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${jdbc.driver}")
    private String driverClass;

    @Bean(name = "yunDataSource")
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setUrl(url);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }

    /*
   * 分页插件,自动识别数据库类型
   * 多租户,请参考官网【插件扩展】
   */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean(name = "yunMybatisSqlSessionFactoryBean")
    public MybatisSqlSessionFactoryBean yunSqlSessionFactoryBean(@Qualifier("yunDataSource") DruidDataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/cn/com/demo/dao/*/*/mapper/*/*Mapper.xml"));

//        List<Interceptor> interceptors = new ArrayList<>();
        Interceptor[] interceptorsArr = new Interceptor[1];
        interceptorsArr[0] = paginationInterceptor();
//        interceptors.add(paginationInterceptor());
        mybatisSqlSessionFactoryBean.setPlugins(interceptorsArr);

        return mybatisSqlSessionFactoryBean;
    }



    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("yunDataSource") DruidDataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "yunSqlSessionTemplate")
    public SqlSessionTemplate yunSqlSessionTemplate(
            @Qualifier("yunMybatisSqlSessionFactoryBean") MybatisSqlSessionFactoryBean yunSqlSessionFactoryBean) throws Exception{

        return new SqlSessionTemplate(yunSqlSessionFactoryBean.getObject());
    }

    @Bean
    public JdbcDaoImpl securityJdbcDao() {
        JdbcDaoImpl jdbcDao = new JdbcDaoImpl();
        jdbcDao.setDataSource(dataSource());

        return jdbcDao;

    }
}

2.数据源2

package cn.com.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionTemplate;
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.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;


@Configuration
@PropertySource({
        "classpath:jdbc.properties"
})
//value = "cn.com.demo.dao.*.mapper*",
// basePackages 必须指定该数据源分配的包路径,意思就是再该路径下的所有数据操作都是再指定的数据源下面进行的
@MapperScan( basePackages = "cn.com.demo.dao.oa.*.mapper", sqlSessionTemplateRef = "oaSqlSessionTemplate")
public class DataSource2Config {
    @Value("${jdbc.url2}")
    private String url;
    @Value("${jdbc.username2}")
    private String username;
    @Value("${jdbc.password2}")
    private String password;
    @Value("${jdbc.driver}")
    private String driverClass;

    @Bean(name = "oaDataSource")
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setUrl(url);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClass);
        return dataSource;
    }


    @Bean(name = "oaSqlSessionFactory")
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("oaDataSource") DruidDataSource dataSource) throws Exception{
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:/cn/com/demo/dao/*/*/mapper/*/*Mapper.xml"));
//        List<Interceptor> interceptors = new ArrayList<>();
//        Interceptor[] interceptorsArr = new Interceptor[1];
//        interceptorsArr[0] = paginationInterceptor();
        interceptors.add(paginationInterceptor());
//        mybatisSqlSessionFactoryBean.setPlugins(interceptorsArr);

        return mybatisSqlSessionFactoryBean;
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("oaDataSource") DruidDataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "oaSqlSessionTemplate")
    public SqlSessionTemplate yunSqlSessionTemplate(
            @Qualifier("oaSqlSessionFactory") MybatisSqlSessionFactoryBean oaSqlSessionFactory) throws Exception{

        return new SqlSessionTemplate(oaSqlSessionFactory.getObject());
    }
}

3.测试

package cn.com.demo.test;

import cn.com.demo.dao.oa.code.entity.OaNoCode;
import cn.com.demo.dao.oa.code.mapper.OaNoCodeMapper;
import cn.com.demo.dao.yun.user.entity.User;
import cn.com.demo.service.IOaNoCodeService;
import cn.com.demo.service.IUserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestMutiDataSource {

    @Autowired
    private IUserService userService;
    @Autowired
    private IOaNoCodeService oaNoCodeService;
    @Autowired
    private OaNoCodeMapper oaNoCodeMapper;

    @Test
    public void test1() {
        EntityWrapper ew = new EntityWrapper();
        ew.setEntity(new User());
        ew.eq("user_code", "A001");
        System.out.println(ew.getSqlSegment());
        System.out.println(userService.selectList(ew).toString());

        System.out.println("------------------------------>");
        ew = new EntityWrapper();
        ew.setEntity(new OaNoCode());
        ew.eq("codeid", "1");
        System.out.println(ew.getSqlSegment());
        System.out.println(oaNoCodeService.selectList(ew).toString());
        System.out.println("*************************");
        System.out.println(oaNoCodeMapper.getAll());

    }
}

 

转载于:https://my.oschina.net/u/3711426/blog/1923317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值