当前位置:   article > 正文

分库分表插入(二)springboot分库分表插入数据库_分库分表后如何插入数据

分库分表后如何插入数据

最近在做数据的分库分表插入,考虑了几种方式,一种是spring+mybatis的动态数据源方式插入数据库,另一种是选择springboot + mycat方式,这里使用的是springboot固定配置两个数据源,一个数据库用来查询,另一个用来存储数据,相对固定没有了动态选择,也相对简单了一点。

1、首先 .yml配置数据库

  1. #spring properties
  2. spring:
  3. profiles: local
  4. application:
  5. name: syncService
  6. datasource:
  7. # master主数据源
  8. master:
  9. url: jdbc:mysql://127.0.0.1:3306/cdot_business?useUnicode=true&characterEncoding=utf-8
  10. username: root
  11. password: *******
  12. type: com.alibaba.druid.pool.DruidDataSource
  13. driver-class-name: com.mysql.jdbc.Driver
  14. initialSize: 5
  15. minIdle: 2
  16. maxActive: 20
  17. timeBetweenEvictionRunsMillis: 60000
  18. minEvictableIdleTimeMillis: 300000
  19. validationQuery: SELECT 1 FROM DUAL
  20. testWhileIdle: true
  21. testOnBorrow: false
  22. testOnReturn: false
  23. logSlowSql: true
  24. # cluster从数据源
  25. cluster:
  26. url: jdbc:mysql://192.168.1.243:8066/cdot_anyue_business?useUnicode=true&characterEncoding=utf-8
  27. username: ****
  28. password: ******
  29. type: com.alibaba.druid.pool.DruidDataSource
  30. driver-class-name: com.mysql.jdbc.Driver
  31. initialSize: 5
  32. minIdle: 2
  33. maxActive: 20
  34. timeBetweenEvictionRunsMillis: 60000
  35. minEvictableIdleTimeMillis: 300000
  36. validationQuery: SELECT 1 FROM DUAL
  37. testWhileIdle: true
  38. testOnBorrow: false
  39. testOnReturn: false
  40. logSlowSql: true

 

2、两个数据库配置:

 

 

(1)MasterSourceConfig.java

该数据库如何确定对应哪个dao下mapper呢?

@MapperScan(basePackage="com.chargedot.syncservice.dao.mapper.master")

  1. import org.apache.ibatis.session.SqlSessionFactory;
  2. import org.mybatis.spring.SqlSessionFactoryBean;
  3. import org.mybatis.spring.SqlSessionTemplate;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.context.annotation.Primary;
  9. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  10. import javax.sql.DataSource;
  11. /**
  12. * @author yanghao
  13. * @Description:
  14. * @date 2019/3/22 15:41
  15. */
  16. @Configuration
  17. @MapperScan(basePackages = "com.chargedot.syncservice.dao.mapper.master", sqlSessionTemplateRef = "masterSqlSessionTemplate")
  18. public class MasterSourceConfig {
  19. @Bean(name = "masterSqlSessionFactory")
  20. @Primary
  21. public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
  22. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  23. bean.setDataSource(dataSource);
  24. return bean.getObject();
  25. }
  26. @Bean(name = "masterTransactionManager")
  27. @Primary
  28. public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
  29. return new DataSourceTransactionManager(dataSource);
  30. }
  31. @Bean(name = "masterSqlSessionTemplate")
  32. @Primary
  33. public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  34. return new SqlSessionTemplate(sqlSessionFactory);
  35. }
  36. }

 

(2)ClusterSourceConfig.java

该数据库如何确定对应哪个dao下mapper呢?

@MapperScan(basePackage="com.chargedot.syncservice.dao.mapper.cluster")

  1. import org.apache.ibatis.session.SqlSessionFactory;
  2. import org.mybatis.spring.SqlSessionFactoryBean;
  3. import org.mybatis.spring.SqlSessionTemplate;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  9. import javax.sql.DataSource;
  10. /**
  11. * @author yanghao
  12. * @Description:
  13. * @date 2019/3/22 15:47
  14. */
  15. @Configuration
  16. @MapperScan(basePackages = "com.chargedot.syncservice.dao.mapper.cluster", sqlSessionTemplateRef = "clusterSqlSessionTemplate")
  17. public class ClusterSourceConfig {
  18. @Bean(name = "clusterSqlSessionFactory")
  19. public SqlSessionFactory sqlSessionFactory(@Qualifier("clusterDataSource") DataSource dataSource) throws Exception {
  20. SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
  21. bean.setDataSource(dataSource);
  22. return bean.getObject();
  23. }
  24. @Bean(name = "clusterTransactionManager")
  25. public DataSourceTransactionManager transactionManager(@Qualifier("clusterDataSource") DataSource dataSource) {
  26. return new DataSourceTransactionManager(dataSource);
  27. }
  28. @Bean(name = "clusterSqlSessionTemplate")
  29. public SqlSessionTemplate sqlSessionTemplate(@Qualifier("clusterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
  30. return new SqlSessionTemplate(sqlSessionFactory);
  31. }
  32. }

 

(3)MultiDataSourceConfig.java

配置两个数据源,@Primary设置主数据源,注意点:两个数据源必须两个不同的实例名,否则会被覆盖

  1. import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Primary;
  6. import javax.sql.DataSource;
  7. /**
  8. * @author yanghao
  9. * @Description:
  10. * @date 2019/3/22 15:54
  11. */
  12. @Configuration
  13. public class MultiDataSourceConfig {
  14. @Primary
  15. @Bean(name = "masterDataSource")
  16. @ConfigurationProperties("spring.datasource.master")
  17. public DataSource masterDataSource(){
  18. return DruidDataSourceBuilder.create().build();
  19. }
  20. @Bean(name = "clusterDataSource")
  21. @ConfigurationProperties("spring.datasource.cluster")
  22. public DataSource clusterDataSource(){
  23. return DruidDataSourceBuilder.create().build();
  24. }
  25. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/389541
推荐阅读
相关标签
  

闽ICP备14008679号