MISBoot快速开发平台

多数据源操作

一、增加多数据源

第一步,以system项目为案例增加连接emis-office,emis-office数据源;
第二步,修改个人仓库system配置文件;
datasource:
        url: jdbc:mysql://${server.database-host}:3329/emis_system?characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai
        default:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://${server.database-host}:3329/emis_system?characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai
            username: 123456
            password: 123456
        erp:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://${server.database-host}:3329/emis_erp?characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai
            username: 123456
            password: 123456
        office:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://${server.database-host}:3329/emis_office?characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai
            username: 123456
            password: 123456
    mybatis:
        mapper-locations:mapper-locations: classpath*:cn/ewsd/mapper/**/*.xml
第三步,在system项目下config文件下新建如下类;
默认system数据源配置
package cn.ewsd.system.config;

    import com.alibaba.druid.pool.DruidDataSource;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    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 tk.mybatis.spring.annotation.MapperScan;

    import javax.sql.DataSource;
    @Configuration
    @MapperScan(basePackages = "cn.ewsd.system.mapper.system", sqlSessionTemplateRef  = "defaultSqlSessionTemplate")
    public class DataSourceConfigurer {
        @Bean(name = "defaultDataSource")
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource.default")
        public DataSource dataSource() {
            return  new DruidDataSource();
        }

        @Bean(name = "defaultSqlSessionFactory")
        @Primary
        public SqlSessionFactory sqlSessionFactory(@Qualifier("defaultDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
            configuration.setMapUnderscoreToCamelCase(true);
            bean.setConfiguration(configuration);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:cn/ewsd/mapper/system/*.xml"));
            return bean.getObject();
        }

        @Bean(name = "defaultTransactionManager")
        @Primary
        public DataSourceTransactionManager transactionManager(@Qualifier("defaultDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }

        @Bean(name = "defaultSqlSessionTemplate")
       @Primary
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("defaultSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
                throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
erp数据源配置
package cn.ewsd.system.config;

    import com.alibaba.druid.pool.DruidDataSource;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    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 tk.mybatis.spring.annotation.MapperScan;

    import javax.sql.DataSource;
    @Configuration
    @MapperScan(basePackages = "cn.ewsd.system.mapper.erp", sqlSessionTemplateRef  = "erpSqlSessionTemplate")
    public class ErpSourceConfigurer {
        @Bean(name = "erpDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.erp")
        public DataSource dataSource() {
            return  new DruidDataSource();
        }

        @Bean(name = "erpSqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("erpDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
            configuration.setMapUnderscoreToCamelCase(true);
            bean.setConfiguration(configuration);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:cn/ewsd/mapper/erp/*.xml"));
            return bean.getObject();
        }

        @Bean(name = "erpTransactionManager")
        public DataSourceTransactionManager transactionManager(@Qualifier("erpDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }

        @Bean(name = "erpSqlSessionTemplate")
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("erpSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
                throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
office数据源配置
package cn.ewsd.system.config;

    import com.alibaba.druid.pool.DruidDataSource;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    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 tk.mybatis.spring.annotation.MapperScan;

    import javax.sql.DataSource;

    @Configuration
    @MapperScan(basePackages = "cn.ewsd.system.mapper.office", sqlSessionTemplateRef  = "officeSqlSessionTemplate")
    public class OfficeSourceConfigurer {
        @Bean(name = "officeDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.office")
        public DataSource dataSource() {
            return  new DruidDataSource();
        }

        @Bean(name = "officeSqlSessionFactory")
        public SqlSessionFactory sqlSessionFactory(@Qualifier("officeDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
            configuration.setMapUnderscoreToCamelCase(true);
            bean.setConfiguration(configuration);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:cn/ewsd/mapper/office/*.xml"));
            return bean.getObject();
        }

        @Bean(name = "officeTransactionManager")
        public DataSourceTransactionManager transactionManager(@Qualifier("officeDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }

        @Bean(name = "officeSqlSessionTemplate")
        public SqlSessionTemplate sqlSessionTemplate(@Qualifier("officeSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
                throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
第四步,再在mapper文件下新建system,erp,office文件即可;
QQ在线咨询
客服 QQ
251122361
微信/手机
18973127736
办公电话
0731-89565588