Configure multiple data sources spring boot

First, the use of multiple data sources arranged jdbc

Data source configuration 1.yml

 

 

 

2. Configure class

package com.v246.common.config.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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;
importorg.springframework.core.io.support.PathMatchingResourcePatternResolver;
 Import org.springframework.jdbc.core.JdbcOperations;
 Import org.springframework.jdbc.core.JdbcTemplate;
 Import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 Import org.springframework. transaction.PlatformTransactionManager; 

Import the javax.sql.DataSource; 

// represents the class of a class configuration 
@Configuration
 public  class DatasourceConfig { 

    // this object into a container Spring 
    @Bean ( "datavisualDataSource" )
     // indicating that the data source is a The default data source 
    @Primary 
    @ Qualifier ("datavisualDataSource" )
     // read the configuration parameters application.properties mapped becomes an object
     // prefix represents a prefix parameter 
    @ConfigurationProperties (prefix = "spring.datasource.dynamic.datasource.datavisual" )
     public the DataSource datavisualDataSource () {
         return DruidDataSourceBuilder.create () Build ();. 
    } 

    @Bean ( "vlmsDataSource" ) 
    @Qualifier ( "vlmsDataSource" ) 
    @ConfigurationProperties (prefix = "spring.datasource.dynamic.datasource.vlms" )
     public the DataSource vlmsDataSource () {
         return DruidDataSourceBuilder.create().build();
    }





    @Bean("jdbcTemplate")
    public JdbcTemplate datavisualJdbcOperations(@Qualifier("datavisualDataSource") DataSource datavisualDataSource) {
        return new JdbcTemplate(datavisualDataSource);
    }
    @Bean("vlmsJdbcTemplate")
    public JdbcOperations vlmsJdbcOperations(@Qualifier("vlmsDataSource") DataSource vlmsDataSource) {
        return new JdbcTemplate(vlmsDataSource);
    }





    @Bean
    @Primary
    publicDatavisualTransactionManager the PlatformTransactionManager (@Qualifier ( "datavisualDataSource" ) the DataSource datavisualDataSource) {
         return  new new DataSourceTransactionManager (datavisualDataSource); 
    } 
    @Bean 
    public the PlatformTransactionManager vlmsTransactionManager (@Qualifier ( "vlmsDataSource" ) the DataSource vlmsDataSource) {
         return  new new DataSourceTransactionManager (vlmsDataSource); 
    } 


    @Bean ( name = "SqlSessionFactory" )
     // indicating that the data source is the default data source 
    @Primary
     // @ Qualifier represent Find Spring container DataSource name for the object of 
    publicTest1SqlSessionFactory a SqlSessionFactory (@Qualifier ( "datavisualDataSource" ) the DataSource DataSource)
             throws Exception { 
        the SqlSessionFactoryBean the bean = new new the SqlSessionFactoryBean (); 
        bean.setDataSource (DataSource); 
        bean.setMapperLocations ( 
                // set mybatis xml position where 
                new new PathMatchingResourcePatternResolver () getResources (. "CLASSPATH *:. Mapping / Mapper / * XML" ));
         return bean.getObject (); 
    } 


    @Bean (name = "vlmsSqlSessionFactory" )
     // @Qualifier a lookup Spring container DataSource object name for the 
    public SqlSessionFactory vlmsSqlSessionFactory(@Qualifier("vlmsDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/test01/*.xml"));
        return bean.getObject();
    }
}

3. Test class

public class SqlserverServiceTest {
     @Resource
     JdbcTemplate vlmsJdbcTemplate;

     @Autowired
     NamedDb db;

    @Test
    public void testTime() {


        Map<String,Object> map = vlmsJdbcTemplate.queryForMap("SELECT * FROM wx_user WHERE user_id='***'");
        System.out.println(map);
        Record rc = db.findFirst("SELECT * FROM SYSC03 WHERE CGRDM='***'");
        System.out.println(rc);

    }


}

 

Second, the use of multiple data sources arranged mybatis

Data source configuration 1.yml

 

 

 2.prom.xml Configuration

        <!--引入dynamic-datasource-spring-boot-starter。-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>

3. Test class

// default to the primary data source using @DS ( "Data Source Name") using the new additional data sources 
@DS ( "VLMs" ) public interface BiZcPlanShipmentMapper the extends IBaseMapper <BiZcPlanShipment> { Page <the Record> getDataList (@Param ( "filterPage ") Page <the Record> Page); }

The second configuration may refer to mybatis  https://mp.baomidou.com/guide/dynamic-datasource.html

 

 

 

package com.v246.common.config.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.jdbc.core.JdbcOperations ;
Import org.springframework.jdbc.core.JdbcTemplate ;
Import org.springframework. jdbc.datasource.DataSourceTransactionManager ;
Import the org.springframework.transaction.PlatformTransactionManager ;
Import the javax.sql.DataSource ; // this class represents the configuration of a class @Configuration public class DatasourceConfig { // this object into a container Spring @Bean (







"datavisualDataSource" )
// Default indicates that the data source is the data source
@Primary
@Qualifier ( "datavisualDataSource" )
// read the configuration parameters application.properties mapped becomes an object
// prefix represents a prefix parameter
@ConfigurationProperties ( prefix = "spring.datasource.dynamic.datasource.datavisual" )
public the DataSource datavisualDataSource () {
return DruidDataSourceBuilder. Create () .build () ;
}

@Bean ( "vlmsDataSource" )
@Qualifier ( "vlmsDataSource" )
@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.vlms")
public DataSource vlmsDataSource(){
return DruidDataSourceBuilder.create().build();
}





@Bean("jdbcTemplate")
public JdbcTemplate datavisualJdbcOperations(@Qualifier("datavisualDataSource") DataSource datavisualDataSource) {
return new JdbcTemplate(datavisualDataSource);
}
@Bean("vlmsJdbcTemplate")
public JdbcOperations vlmsJdbcOperations(@Qualifier("vlmsDataSource") DataSource vlmsDataSource) {
return new JdbcTemplate(vlmsDataSource);
}





@Bean
@Primary
public PlatformTransactionManager datavisualTransactionManager(@Qualifier("datavisualDataSource") DataSource datavisualDataSource) {
return new DataSourceTransactionManager(datavisualDataSource);
}
@Bean
public PlatformTransactionManager vlmsTransactionManager(@Qualifier("vlmsDataSource") DataSource vlmsDataSource) {
return new new DataSourceTransactionManager (vlmsDataSource) ;
}


@Bean ( name = "SqlSessionFactory" )
// Default indicates that the data source is the data source
@Primary
// Find @Qualifier represents Spring container DataSource object name for the
public a SqlSessionFactory test1SqlSessionFactory ( @Qualifier ( "datavisualDataSource" ) the DataSource DataSource)
throws Exception {
the SqlSessionFactoryBean the bean = new new the SqlSessionFactoryBean () ;
bean.setDataSource (DataSource) ;
bean.setMapperLocations (
// set mybatis xml position where
new new PathMatchingResourcePatternResolver () getResources. ( "CLASSPATH *:. Mapping / Mapper / xml *" )) ;
return bean.getObject () ;
}


@Bean ( name = "vlmsSqlSessionFactory" )
@ @ Qualifier a lookup name Spring container the DataSource object
public a SqlSessionFactory vlmsSqlSessionFactory ( @Qualifier ( "vlmsDataSource" ) DataSource DataSource)
throws Exception {
the SqlSessionFactoryBean the bean = new new the SqlSessionFactoryBean () ;
bean.setDataSource (DataSource);
Bean.setMapperLocations (
// set mybatis xml position where
new new PathMatchingResourcePatternResolver () getResources (. "CLASSPATH *:. Mapping / Test01 / xml *" )) ;
return bean.getObject () ;
}
}

Guess you like

Origin www.cnblogs.com/FlyingTanks/p/11647561.html