Newer
Older
zq-big-sreen / src / main / java / com / casic / config / SmartWellDataSourceConfig.java
chaizhuang on 19 Oct 2022 2 KB 没有改变
package com.casic.config;

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.beans.factory.annotation.Value;
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.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * @program:
 * @description: 数据库配置2
 * @author: cz
 * @create: 2022-08-17 09:03
 **/
@Configuration
@MapperScan(basePackages = "com.casic.dao.smartwell",sqlSessionFactoryRef = "smartwellSqlSessionFactory")
public class SmartWellDataSourceConfig {

    @Value("${spring.datasource.smartwell.driver-class-name}")
    String driverClass;
    @Value("${spring.datasource.smartwell.url}")
    String url;
    @Value("${spring.datasource.smartwell.username}")
    String userName;
    @Value("${spring.datasource.smartwell.password}")
    String passWord;

    @Bean(name = "smartwellDataSource")
    @ConfigurationProperties("spring.datasource.smartwell")
    public DataSource masterDataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(passWord);
        return dataSource;
    }

    @Bean(name = "smartwellSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("smartwellDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/smartwell/*.xml"));
        return sessionFactoryBean.getObject();
    }

    @Bean(name = "smartwelSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionFactoryTemplate(@Qualifier("smartwellSqlSessionFactory") SqlSessionFactory sqlSessionFactory ) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}