When the amount of service data reaches a certain level, dbAs need to properly configure database resources. That is, the machine configuration of the master library is high, and the core high-frequency data is placed on the master library; Put secondary data in slave library, low configuration. Open source, save money. That’s what it means. Putting data in different databases requires manipulating data through different data sources.
1. Maven package guide
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
Copy the code
2. Master library configuration
@Configuration
@ MapperScan (basePackages = ".net. Zhaoxiaobin. Mybatis. Dao. The master ", / / Mapper mapping interface, Factoryref = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
/** * Data source configuration * Multiple data source configuration must have a primary data source, use@PrimaryFlag the Bean * *@return* /
@Bean
@Primary
@ConfigurationProperties(prefix = "master.datasource")
public DataSource masterDataSource(a) {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
/** * Transaction manager * create transaction manager ** using proxy object to get data source@return* /
@Bean
@Primary
public DataSourceTransactionManager masterTransactionManager(a) {
return new DataSourceTransactionManager(masterDataSource());
}
/** * Create mybatis session factory * specify data source and Mybatis mapping file (multiple data sources need to be separated) **@return
* @throws Exception
*/
@Bean
@Primary
public SqlSessionFactory masterSqlSessionFactory(a) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource());
sessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/master/*.xml"));
returnsessionFactory.getObject(); }}Copy the code
master:
datasource:
url: JDBC: mysql: / / 148.70.153.63:3306 / TTMS? useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
username: root
password: password
driverClassName: com.mysql.cj.jdbc.Driver
initialSize: 5
maxActive: 50
minIdle: 0
maxWait: 60000
useUnfairLock: true # Disable fair locking
Copy the code
3. Configure the slave library
@Configuration
@MapperScan( basePackages = "net.zhaoxiaobin.mybatis.dao.cluster", sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class ClusterDataSourceConfig {
/** * Data source configuration **@return* /
@Bean
@ConfigurationProperties(prefix = "cluster.datasource")
public DataSource clusterDataSource(a) {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
/** * Transaction manager * create transaction manager ** using proxy object to get data source@return* /
@Bean
public DataSourceTransactionManager clusterTransactionManager(a) {
return new DataSourceTransactionManager(clusterDataSource());
}
/** * Create mybatis session factory * specify data source and Mybatis mapping file (multiple data sources need to be separated) **@return
* @throws Exception
*/
@Bean
public SqlSessionFactory clusterSqlSessionFactory(a) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource());
sessionFactory.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/cluster/*.xml"));
returnsessionFactory.getObject(); }}Copy the code
cluster:
datasource:
url: JDBC: mysql: / / 148.70.153.63:3306 / TTMS? useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
username: root
password: password
driverClassName: com.mysql.cj.jdbc.Driver
initialSize: 1
maxActive: 10
minIdle: 0
maxWait: 60000
useUnfairLock: true # Disable fair locking
Copy the code
4. Engineering structure
├ ─ ─ pom. XML └ ─ ─ the SRC └ ─ ─ the main ├ ─ ─ Java │ └ ─ ─ com │ └ ─ ─ ZXB │ └ ─ ─ mybatis │ ├ ─ ─ MybatisMutilDataSourceApplication. Java │ ├ ─ ─ the config │ │ ├ ─ ─ ClusterDataSourceConfig. Java │ │ └ ─ ─ MasterDataSourceConfig. Java │ ├ ─ ─ dao │ │ ├ ─ ─ cluster │ │ │ └ ─ ─ AuthClientServiceDao. Java │ │ └ ─ ─ master │ │ └ ─ ─ BaseUserDao. Java │ ├ ─ ─ domain │ │ └ ─ ─ bo │ │ ├ ─ ─ AuthClientServiceBO. Java │ │ └ ─ ─ BaseUserBO. Java │ ├ ─ ─ service │ │ ├ ─ ─ MutilDatasourceService. Java │ │ └ ─ ─ impl │ │ └ ─ ─ MutilDatasourceServiceImpl. Java │ └ ─ ─ web │ └ ─ ─ MutilDatasourceController. Java └ ─ ─ resources ├ ─ ─ application. Yml ├ ─ ─ Logback - spring. XML ├ ─ ─ mapper │ ├ ─ ─ cluster │ │ └ ─ ─ authMapper. XML │ └ ─ ─ master │ └ ─ ─ userMapper. XML ├ ─ ─ ├ ─ ├ ─ SQL ├.txtCopy the code
Refer to the link
- Spring Boot implements Druid multi-source configuration with Mybatis
The code address
- Github:github.com/senlinmu100…
- Gitee:gitee.com/ppbin/sprin…
Personal website
- Gitee Pages
- Github Pages