zoukankan      html  css  js  c++  java
  • 多数据源配置

    package com.wzxl.assess.base.dataSource;

    import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
    import org.apache.ibatis.session.SqlSessionFactory;
    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;


    /**
    * Author: ljf
    * Date: 2019-12-23 11:58
    **/
    @Configuration
    @MapperScan(basePackages = "com.wzxl.assess.modules.*.mapper",sqlSessionFactoryRef = "dataSource1SqlSessionFactory")
    public class DataSource1Config {


    // 表示这个数据源是默认数据源
    @Primary
    @Bean(name = "dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource.datasource1")
    public DataSource getDateSource1() {
    return DataSourceBuilder.create().build();
    }


    @Bean(name = "dataSource1SqlSessionFactory")
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory dataSource1SqlSessionFactory(@Qualifier("dataSource1") DataSource datasource)
    throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(datasource);
    bean.setMapperLocations(
    // 设置mybatis的xml所在位置
    new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*/*.xml"));
    return bean.getObject();
    }

    @Bean(name = "dataSource1TransactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource1") DataSource datasource){
    return new DataSourceTransactionManager(datasource);
    }

    // @Bean("dataSource1SqlSessionTemplate")
    // // 表示这个数据源是默认数据源
    // @Primary
    // public SqlSessionTemplate dataSource1SqlSessionTemplate(
    // @Qualifier("dataSource1SqlSessionFactory") SqlSessionFactory sessionFactory)
    // {
    // return new SqlSessionTemplate(sessionFactory, ExecutorType.BATCH);
    // }


    }



    package com.wzxl.assess.base.dataSource;

    import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
    import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
    import org.apache.ibatis.plugin.Interceptor;
    import org.apache.ibatis.session.ExecutorType;
    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.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.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;

    import javax.sql.DataSource;

    /**
    * @author yanxu
    * @version V1.0
    * @Description: TODO
    * @package com.wzxl.bigdata.base.dataSource
    * @date 16/10/2020
    * @classname OpinionDataSource
    */
    @Configuration
    @MapperScan(basePackages = "com.wzxl.assess.modules.opinion.shyq.mapper",sqlSessionFactoryRef = "publicOpinionDataSourceSqlSessionFactory")
    public class OpinionDataSource {

    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @Bean(name = "publicOpinionDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.opinion")
    public DataSource getQuartzDataSource() {
    return DataSourceBuilder.create().build();
    }


    @Bean
    public SqlSessionFactory publicOpinionDataSourceSqlSessionFactory(@Qualifier("publicOpinionDataSource") DataSource datasource)
    throws Exception {
    MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
    bean.setDataSource(datasource);
    bean.setPlugins(new Interceptor[]{paginationInterceptor()});
    bean.setMapperLocations(
    // 设置mybatis的xml所在位置
    new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*.xml"));
    return bean.getObject();
    }

    @Bean(name = "publicOpinionDataSourceTransactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("publicOpinionDataSource") DataSource datasource){
    return new DataSourceTransactionManager(datasource);
    }

    @Bean("publicOpinionDataSourceSqlSessionTemplate")
    public SqlSessionTemplate publicOpinionDataSourceSqlSessionTemplate(
    @Qualifier("publicOpinionDataSourceSqlSessionFactory") SqlSessionFactory sessionFactory)
    {
    return new SqlSessionTemplate(sessionFactory, ExecutorType.BATCH);
    }

    // 分页拦截器
    @Bean
    public PaginationInterceptor paginationInterceptor(){
    return new PaginationInterceptor();
    }

    }
  • 相关阅读:
    前端优化分析 之 javascript放在底部
    HTML、SHTML、XHTML、DHTML、XML有什么不同
    web项目开发流程及规范
    sql developer连接mysql数据库
    hibernate自动创建表报错,提示不存在
    04关键字和标识符
    03注释与API文档
    WEB应用中的路径问题及乱码问题
    01java开发环境配置
    Eclipse Java EE IDE for Web Developers 4.5.1 安装hibername tools 插件
  • 原文地址:https://www.cnblogs.com/spx88/p/14582381.html
Copyright © 2011-2022 走看看