You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.9 KiB
51 lines
1.9 KiB
package me.zhengjie.config.mybatis;
|
|
|
|
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.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
|
|
|
@Configuration
|
|
@MapperScan(basePackages = "me.zhengjie.mapper.db1.**", sqlSessionFactoryRef = "primarySqlSessionFactory")
|
|
public class DataSourceConfigPrimary {
|
|
/**
|
|
* 配置主数据源
|
|
*
|
|
* @return
|
|
*/
|
|
@Bean(name = "primaryDataSource")
|
|
@Primary
|
|
@ConfigurationProperties("spring.datasource.druid")
|
|
public DataSource dataSource() {
|
|
return DruidDataSourceBuilder.create().build();
|
|
}
|
|
|
|
@Bean(name = "primarySqlSessionFactory")
|
|
@Primary
|
|
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
|
|
throws Exception {
|
|
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
|
bean.setDataSource(dataSource);
|
|
bean.setMapperLocations(
|
|
new PathMatchingResourcePatternResolver().getResources("classpath*:me.zhengjie.mapper.db1/*.xml"));
|
|
return bean.getObject();
|
|
}
|
|
|
|
@Bean("primarySqlSessionTemplate")
|
|
@Primary
|
|
public SqlSessionTemplate sqlSessionTemplate(
|
|
@Qualifier("primarySqlSessionFactory") SqlSessionFactory sessionFactory) {
|
|
return new SqlSessionTemplate(sessionFactory);
|
|
}
|
|
}
|