Project address: github.com/shizhenshua…

Development environment:

  • jdk: JDK1.8 +
  • gradle: Gradle4.6 +
  • Spring2.1.3. RELEASE +

When the database data reaches a certain number, the response of the database will be slow, and some measures will be taken. For example, read and write separation, table and library separation, master-slave services, caching technology and so on. Here is the use of multi – library multi – table. Alter table user_info from test

Alter table test2 user_info

Import dependence

  1. Compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.3.RELEASE'

  2. Compile group: 'org.springframework.boot', name: 'spring-boot-starter-JDBC ', version: '2.1.3.RELEASE'

  3. CompileOnly Group: 'org. projectLombok ', name: 'lombok', version: '1.18.6'

  4. Compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'

  5. Compile group: 'org.mybatis. Generator ', name: 'mybatis-generator-core', version: '1.3.5'

  6. Compile group: 'tk.mybatis', name: 'mapper', version: '4.0.2'

  7. Compile group: 'org.mybatis. Spring. boot', name: 'mybatis-spring-boot-starter', version: '2.0.0'

HikariCP has been introduced in Spring-boot-starter-JDBC (spring default data source)

The project structure

The configuration file configures (application.yaml) default data source information

  1. spring:
  2. datasource:
  3. Url: JDBC: mysql: / / 127.0.0.1:3306 / test? useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai&allowMultiQuer ies=true
  4. username: root
  5. password: admin
  6. sql-script-encoding: UTF-8

Read the configuration information DataSourceConfig. Java to create the default data source

  1. package utry.hikaricp.config;

  2. import com.zaxxer.hikari.HikariDataSource;

  3. import org.springframework.beans.factory.annotation.Value;

  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

  5. import org.springframework.context.annotation.Bean;

  6. import org.springframework.context.annotation.Configuration;

  7. import org.springframework.transaction.annotation.EnableTransactionManagement;

  8. import utry.hikaricp.DataSourceProvider;

  9. import utry.hikaricp.info.DataSourceInfo;

  10. import utry.hikaricp.source.MyHikariDataSource;

  11. @Configuration

  12. @EnableTransactionManagement

  13. publicclassDataSourceConfig{

  14. @Value("${spring.datasource.url}")

  15. privateString url;

  16. @Value("${spring.datasource.username}")

  17. privateString userName;

  18. @Value("${spring.datasource.password}")

  19. privateString password;

  20. @Bean

  21. @ConditionalOnMissingBean(MyHikariDataSource.class)

  22. publicMyHikariDataSource dataSource() {

  23. DataSourceInfo info = newDataSourceInfo();

  24. info.setUrl(url);

  25. info.setUsername(userName);

  26. info.setPassword(password);

  27. HikariDataSource dataSource = DataSourceProvider.create(info);

  28. MyHikariDataSource hikariCPDataSource = newMyHikariDataSource();

  29. hikariCPDataSource.updateDataSourceMap("1", dataSource);

  30. return hikariCPDataSource;

  31. }

  32. }

Create a HikariDataSource using the DataSourceProvider

  1. package utry.hikaricp;

  2. import com.zaxxer.hikari.HikariConfig;

  3. import com.zaxxer.hikari.HikariDataSource;

  4. import utry.hikaricp.info.DataSourceInfo;

  5. publicclassDataSourceProvider{

  6. publicstaticHikariDataSource create(DataSourceInfo sourceInfo) {

  7. HikariConfig hikariConfig = newHikariConfig();

  8. hikariConfig.setUsername(sourceInfo.getUsername());

  9. hikariConfig.setPassword(sourceInfo.getPassword());

  10. hikariConfig.setJdbcUrl(sourceInfo.getUrl());

  11. hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");

  12. returnnewHikariDataSource(hikariConfig);

  13. }

  14. }

The core data source class is MyHikariDataSource. Java

  1. package utry.hikaricp.source;

  2. import com.zaxxer.hikari.HikariDataSource;

  3. import org.springframework.jdbc.datasource.AbstractDataSource;

  4. import javax.sql.DataSource;

  5. import java.io.Closeable;

  6. import java.io.IOException;

  7. import java.sql.Connection;

  8. import java.sql.SQLException;

  9. import java.util.HashMap;

  10. import java.util.Map;

  11. publicclassMyHikariDataSourceextendsAbstractDataSource{

  12. privatestaticMap<String, HikariDataSource> dataSourceMap = newHashMap<>(1);

  13. @Override

  14. publicConnection getConnection() throwsSQLException{

  15. return dataSourceMap.get("1").getConnection();

  16. }

  17. @Override

  18. publicConnection getConnection(String username, String password) throwsSQLException{

  19. return dataSourceMap.get("1").getConnection(username, password);

  20. }

  21. privatevoid destroy(){

  22. DataSource dataSource = dataSourceMap.get("1");

  23. Closeable closeable = (Closeable) dataSource;

  24. if(closeable ! = null) {

  25. try{

  26. closeable.close();

  27. } catch(IOException e) {

  28. e.printStackTrace();

  29. }

  30. }

  31. }

  32. publicvoid updateDataSourceMap(String key, HikariDataSource value) {

  33. destroy();

  34. dataSourceMap.put(key, value);

  35. }

  36. }

Change the HikariDataSource in the DataSourceMap collection of this class to a new HikariDataSource (). Key =1 (key=1)


Entity class the UserInfo. Java

  1. package utry.hikaricp.model;

  2. publicclassUserInfo{

  3. privateInteger id;

  4. privateString remarks;

  5. publicUserInfo() {

  6. }

  7. publicUserInfo(Integer id, String remarks) {

  8. this.id = id;

  9. this.remarks = remarks;

  10. }

  11. publicInteger getId() {

  12. return id;

  13. }

  14. publicvoid setId(Integer id) {

  15. this.id = id;

  16. }

  17. publicString getRemarks() {

  18. return remarks;

  19. }

  20. publicvoid setRemarks(String remarks) {

  21. this.remarks = remarks;

  22. }

  23. }

Hikaricpcontroller.java:
  1. Update data source

  2. Querying User Information

  3. package utry.hikaricp.controller;

  4. import org.springframework.beans.factory.annotation.Autowired;

  5. import org.springframework.web.bind.annotation.RequestMapping;

  6. import org.springframework.web.bind.annotation.RestController;

  7. import utry.hikaricp.factory.HikariCPDataSourceFactory;

  8. import utry.hikaricp.mapper.UserInfoMapper;

  9. import utry.hikaricp.model.UserInfo;

  10. @RestController

  11. publicclassHikariCpController{

  12. @Autowired

  13. privateUserInfoMapper mapper;

  14. @Autowired

  15. privateHikariCPDataSourceFactory factory;

  16. @RequestMapping("update")

  17. publicvoid info(String username, String password, String url) {

  18. factory.reload(username, password, url);

  19. }

  20. @RequestMapping("get")

  21. publicUserInfoget() {

  22. try{

  23. UserInfo userInfo = mapper.selectByPrimaryKey(1);

  24. return userInfo;

  25. } catch(Exception e) {

  26. e.printStackTrace();

  27. }

  28. returnnull;

  29. }

  30. }

Start the project

1. Call the get interface and return data from the test library

Update request to update the data source

After the call succeeds, you can see that the previous data source has been shutdown and a new data source has been initialized

3. Call the GET request again, and you can see the data from the test2 database