Introduction of depend on

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>2.0.1</version>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

<version>1.1.14</version>

</dependency>



<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

Copy the code

Modifying a Configuration File

spring:

datasource:

Use druid connection pool

druid:

The name of data source 1

one:

driver-class-name: com.mysql.jdbc.Driver

url: JDBC: mysql: / / 192.168.211.128:3306 / test? useUnicode=true&characterEncoding=UTF-8&useSSL=false

username: root

password: root

Name of data source 2

two:

driver-class-name: com.mysql.jdbc.Driver

url: JDBC: mysql: / / 192.168.211.129:3306 / test? useUnicode=true&characterEncoding=UTF-8&useSSL=false

username: root

password: root

Copy the code

Mybatis configuration class

If the configurations of multiple data sources are similar, modify the parameters



@Configuration

// Specify the DAO package and SqlSession instance to scan

@MapperScan(basePackages = "demo.springboot.multipledatasource.dao1", sqlSessionTemplateRef="oneSqlSessionTemplate")

public class DataSource1Config {



/ * *

* connection pool

* @return

* /


@Bean

@Primary

@ConfigurationProperties("spring.datasource.druid.one")

public DataSource oneDataSource(a){

return DruidDataSourceBuilder.create().build();

}



/ * *

* SqlSessionFactory

* @param dataSource

* @return

* @throws Exception

* /


@Bean

@Primary

public SqlSessionFactory oneSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {

SqlSessionFactoryBean bean = new SqlSessionFactoryBean();

bean.setDataSource(dataSource);

// Mapper file location

bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/one/*.xml"));

return bean.getObject();

}



/ * *

* Transaction manager

* note@Primary: when used in a program@Transactional, the transaction manager is used preferentially.

* If not used@PrimaryNotes, need to be in@Transactional(value="oneTransactionManager") Specifies the transaction manager

* @param dataSource

* @return

* /


@Bean

@Primary

public DataSourceTransactionManager oneTransactionManager(@Qualifier("oneDataSource")DataSource dataSource){

return new DataSourceTransactionManager(dataSource);

}



/ * *

* SqlSession instance

* @param sqlSessionFactory

* @return

* /


@Bean

@Primary

public SqlSessionTemplate oneSqlSessionTemplate(@Qualifier("oneSqlSessionFactory")SqlSessionFactory sqlSessionFactory) {

return new SqlSessionTemplate(sqlSessionFactory);

}

}

Copy the code

DAO and entity classes

public interface User1DAO {



List<User> selectAll(a);

}

@Data

public class User {



private String username;



private String age;

}

Copy the code

test

@RunWith(SpringRunner.class)

@SpringBootTest

public class MultipleDatasourceApplicationTests {



@Autowired

private User1DAO user1DAO;



@Autowired

private User2DAO user2DAO;



@Test

public void test(a) {

List<User> users1 = user1DAO.selectAll();

System.out.println(users1);

List<User> users2 = user2DAO.selectAll();

System.out.println(users2);

}

}

Copy the code

Project path


The author blog

Author’s official account