时间:2021-07-01 10:21:17 帮助过:25人阅读
第二个
import com.zaxxer.hikari.HikariDataSource;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* @author PangG
* @MapperScan 扫描mapper所在路径
*/
@Configuration
@MapperScan(basePackages = "mapper.sqlserver", sqlSessionFactoryRef = "sqlserverSqlSessionFactory")
public class SqlserverDataSourceConfig {
/**
* @Bean 注册Bean对象
* @Primary 表示默认数据源
* @ConfigurationProperties 读取properties中的配置参数映射成为一个对象
*/
@Bean(name = "sqlserverDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.primary")
public HikariDataSource getSqlserverDateSource() {
return new HikariDataSource();
}
/**
* @param datasource 数据源
* @return SqlSessionFactory
* @Primary 默认SqlSessionFactory
*/
@Bean(name = "sqlserverSqlSessionFactory")
@Primary
public SqlSessionFactory sqlserverSqlSessionFactory(@Qualifier("sqlserverDataSource") DataSource datasource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
//mybatis扫描xml所在位置
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/sqlserver/*.xml"));
return bean.getObject();
}
@Bean("sqlserverSessionTemplate")
@Primary
public SqlSessionTemplate sqlserverSqlSessionTemplate(@Qualifier("sqlserverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
注意配置类配置了扫描路径一定要关闭配置文件中的的扫描
#mybatis.mapper-locations=classpath:mapper//.xml
来源:锌闻网数据库双数据源配置
标签:name 一个 UNC zone rtt perl content public ack