springboot + mybais configure multiple data sources (sub implemented)

First, sub-ways:

1, arranged in two databases application.properties:

#druid connection pool 
#dataSoureOne (here is my local data source) 
spring.datasource.one.type = com.alibaba.druid.pool.DruidDataSource 
spring.datasource.one.driver - class -name = com.mysql.jdbc. Driver 
spring.datasource.one.jdbc -url = jdbc: MySQL: // localhost:? 3306 / = to true useUnicode the Test & characterEncoding = UTF-8 & useSSL = false & serverTimezone = UTC 
spring.datasource.one.username = root 
spring.datasource.one.password = root 
#dataSoureTwo (here is our data source server) spring.datasource.two.type
= com.alibaba.druid.pool.DruidDataSource spring.datasource.two.driver - class -name =com.mysql.jdbc.Driver spring.datasource.two.jdbc-url=jdbc:mysql://xx.xxx.xx.xxx:3306/kds_master_info?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC spring.datasource.two.username=root spring.datasource.two.password=KDSmaster123

2, the establishment of even a data source configuration file:

Note that the following are introduced DataSource package import javax.activation.DataSource;   

@Configuration 
// Configure interface class mybatis correct place @MapperScan (basePackages
= " com.example.mybatis.mapper " , sqlSessionFactoryRef = " sqlSessionFactoryOne " ) public class DataSourceConfigOne { @Bean (name = " dataSourceOne " ) @Primary // this data represents a data source is the default source // read the configuration parameters in application.properties mapped to an object, prefix represents a prefix parameter @ConfigurationProperties (prefix = " spring.datasource.one " ) public the dataSource dataSourceOne () { return DataSourceBuilder.create().build(); } @Bean(name = "sqlSessionFactoryOne") @Primary public SqlSessionFactory sqlSessionFactoryOne(@Qualifier("dataSourceOne") DataSource datasource)throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(datasource); bean.setMapperLocations( // 设置mybatis的xml所在位置 new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
  bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return bean.getObject(); } @Primary public SqlSessionTemplate sqlsessiontemplateOne(@Qualifier("sqlsessiontemplateOne") SqlSessionFactory sessionfactory) { return new SqlSessionTemplate(sessionfactory); } }
@Configuration 
@MapperScan (basePackages = " com.example.mybatis.mapper2 " , sqlSessionFactoryRef = " sqlSessionFactoryTwo " )
 public  class DataSourceConfigTwo { 
    @Bean (name = " dataSourceTwo " )
     // read the configuration parameters are mapped to a application.properties objects, prefix represents a prefix parameter 
    @ConfigurationProperties (prefix = " spring.datasource.two " )
     public the DataSource dataSourceTwo () {
         return   DataSourceBuilder.create () Build ();. 
    } 

    @Bean (name= "sqlSessionFactoryTwo")
    public SqlSessionFactory sqlSessionFactoryTwo(@Qualifier("dataSourceTwo") DataSource datasource)throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource((javax.sql.DataSource) datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*.xml"));
  bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return bean.getObject(); } public SqlSessionTemplate sqlsessiontemplateTwo(@Qualifier("sqlsessiontemplateTwo") SqlSessionFactory sessionfactory) { return new SqlSessionTemplate(sessionfactory); } }

note:

1, @ Primary must add this comment, because without that you could not tell if spring will be the main data source (the default data source)

2, mapper interface, and XML form dao requires two separate layers, as shown in FIG directories: 

 

 

 

3、bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“XXXX”));

xml file location mapper form must be configured, otherwise the error: no statement (this error may also be in the xml mapper,

Path namespace to the project caused by the inconsistency)

4, in the service layer injection dao different according to different business layers:

 

 

 

 

 

 5. I began to start the project and access interface will complain, only to find half an hour to see, is underlined with the hump mapping fails, this should be

and adding a line inside sqlSessionFactoryOne sqlSessionFactoryTwo bean.getObject () getConfiguration () setMapUnderscoreToCamelCase (true)..; 

You can then continue to visit, and error Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.

CommunicationsException: Communications link failure.

Later inquiries that, to be in the above database connection url & useSSL = true & useSSL = false changed

 

The last test, two userInfo database tables and user data are displayed: the

userInfo:

 

 

user:

 

 

 

Finally, there is a mistake forgot to add, add in here, my springboot is a 2.x, in a single data source configuration

When, url database connection is spring.datasource.url = xxx, this is no problem, but when configuring multiple data sources

spring.datasource.one.url和spring.datasource.two.url会报错jdbcUrl is required with driverClassName.

The spring.datasource.one.url spring.datasource.two.url and the url into spring.datasource.one.jdbc-url,

That is the url jdbc-url can be changed.

Guess you like

Origin www.cnblogs.com/red-star/p/12529100.html