springboot的数据库配置config

2021/10/21 19:13:00

本文主要是介绍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的数据库配置config的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程