spring boot + mybatis详细使用(五)——多数据库使用

1. 配置文件:java

# 多数据源配置
# 后台用DB
spring.datasource.management.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.management.jdbc-url=jdbc:mysql://localhost:3306/management?serverTimezone=GMT%2B8&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
spring.datasource.management.username=root
spring.datasource.management.password=password
# 教材中心用DB
spring.datasource.jczx.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.jczx.jdbc-url=jdbc:mysql://localhost:3306/jczx?serverTimezone=GMT%2B8&useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
spring.datasource.jczx.username=root
spring.datasource.jczx.password=password
##############################################################################################
# 统一使用配置类进行配置实现;																			##
# application.properties中的数据源配置,spring加载时默认是单数据源配置,									##
# 因此相关的配置都注释掉,统一使用Config配置类进行配置!														##
# mybatis																					##
#mybatis.type-aliases-package=com.wyait.manage.pojo											##
#mybatis.mapper-locations=classpath:mapper/*.xml											##
# 开启驼峰映射																					##
#mybatis.configuration.map-underscore-to-camel-case=true									##
##############################################################################################

2. java配置类:mysql

/**
 * Title: JczxDataSourceConfig.java
 * Description: 
 * Company: 长江数字
 * @author JIMO
 * @date 2019年4月26日
 * @version 1.0
 */
package com.cjsz.management.config;

import javax.sql.DataSource;

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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * Title: JczxDataSourceConfig
 * Description: 
 * @author JIMO
 * @date 2019年4月26日
 */
@Configuration
//指明了扫描dao层,而且给dao层注入指定的SqlSessionTemplate
@MapperScan(basePackages = "com.cjsz.management.jczx.mybatis.mapper", sqlSessionTemplateRef  = "jczxSqlSessionTemplate")
public class JczxDataSourceConfig {

	/**
     * 建立datasource对象
     * @return
     */
    @Bean(name = "jczxDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.jczx")// prefix值必须是application.properteis中对应属性的前缀
    //@Primary
    public DataSource jczxDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 建立sql工程
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "jczxSqlSessionFactory")
    //@Primary
    public SqlSessionFactory jczxSqlSessionFactory(@Qualifier("jczxDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //对应mybatis.type-aliases-package配置
        bean.setTypeAliasesPackage("com.cjsz.management.jczx.mybatis.model");
        //对应mybatis.mapper-locations配置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:jczxMapping/*.xml"));
        //开启驼峰映射
        bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }

    /**
     * 配置事务管理
     * @param dataSource
     * @return
     */
    @Bean(name = "jczxTransactionManager")
    //@Primary
    public DataSourceTransactionManager jczxTransactionManager(@Qualifier("jczxDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * sqlSession模版,用于配置自动扫描pojo实体类
     * @param sqlSessionFactory
     * @return
     * @throws Exception
     */
    @Bean(name = "jczxSqlSessionTemplate")
    //@Primary
    public SqlSessionTemplate jczxSqlSessionTemplate(@Qualifier("jczxSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
	
}
/**
 * Title: DataSourceConfig.java
 * Description: 
 * Company: 长江数字
 * @author JIMO
 * @date 2019年4月26日
 * @version 1.0
 */
package com.cjsz.management.config;

import javax.sql.DataSource;

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;

/**
 * Title: ManagementDataSourceConfig
 * Description: management数据库配置
 * @author JIMO
 * @date 2019年4月26日
 */
@Configuration
//指明了扫描dao层,而且给dao层注入指定的SqlSessionTemplate
@MapperScan(basePackages = "com.cjsz.management.mybatis.mapper", sqlSessionTemplateRef  = "managementSqlSessionTemplate")
public class ManagementDataSourceConfig {
	/**
     * 建立datasource对象
     * @return
     */
    @Bean(name = "managementDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.management")// prefix值必须是application.properteis中对应属性的前缀
    @Primary
    public DataSource managementDataSource() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 建立sql工程
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "managementSqlSessionFactory")
    @Primary
    public SqlSessionFactory managementSqlSessionFactory(@Qualifier("managementDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //对应mybatis.type-aliases-package配置
        bean.setTypeAliasesPackage("com.cjsz.management.mybatis.model");
        //对应mybatis.mapper-locations配置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:managementMapping/*.xml"));
        //开启驼峰映射
        bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return bean.getObject();
    }

    /**
     * 配置事务管理
     * @param dataSource
     * @return
     */
    @Bean(name = "managementTransactionManager")
    @Primary
    public DataSourceTransactionManager managementTransactionManager(@Qualifier("managementDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * sqlSession模版,用于配置自动扫描pojo实体类
     * @param sqlSessionFactory
     * @return
     * @throws Exception
     */
    @Bean(name = "managementSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate managementSqlSessionTemplate(@Qualifier("managementSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

注:若是xml文件在其余工程或者jar包中,须要在classpath后加上*号:spring

//对应mybatis.mapper-locations配置
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:managementMapping/*.xml"));
相关文章
相关标签/搜索