springboot的数据库配置config
作者:互联网
package com.lansi.businessdivide.config.db.mybatis;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
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 org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
-
分单库数据源配置
*/
@Configuration
@MapperScan(basePackages = {“com.lansi.businessdivide.dao.bd.mapper”}, sqlSessionTemplateRef = “businessSessionTemplate”)
public class DataSourceBdConfig {@Primary
@Bean(name = “businessDataSource”)
@ConfigurationProperties(prefix = “spring.datasource.druid.business.assign.order”)
public DataSource businessDataSource() {
return DruidDataSourceBuilder.create().build();
}@Bean(name = “businessJdbcTemplate”)
public JdbcTemplate businessJdbcTemplate(@Qualifier(“businessDataSource”) DataSource businessDataSource) {
return new JdbcTemplate(businessDataSource);
}@Bean(name = “businessSqlSessionFactory”)
public SqlSessionFactory businessSqlSessionFactory(@Qualifier(“businessDataSource”) DataSource businessDataSource, @Qualifier(“pageInterceptor”) PageInterceptor pageInterceptor) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(businessDataSource);
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
bean.setMapperLocations(resolver.getResources(“classpath:bd/mapper/*.xml”));
bean.setPlugins(new Interceptor[]{pageInterceptor});
return bean.getObject();
}@Bean(name = “businessSessionTemplate”)
public SqlSessionTemplate businessSessionTemplate(@Qualifier(“businessSqlSessionFactory”) SqlSessionFactory businessSqlSessionFactory) {
//使用上面配置的Factory
return new SqlSessionTemplate(businessSqlSessionFactory);
}@Bean(name = “businessDataSourceTransactionManager”)
public DataSourceTransactionManager businessDataSourceTransactionManager(@Qualifier(“businessDataSource”) DataSource businessDataSource) {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(businessDataSource);
return dataSourceTransactionManager;
}
}
标签:springboot,数据库,Qualifier,springframework,Bean,org,import,config,businessDataSour 来源: https://blog.csdn.net/qq_42817405/article/details/120882200