• 使用 dynamic-datasource 完成多数据源操作


    本次操作是将达梦数据库的数据插入到MySQL数据库中。

    准备工作

    1. 创建一个 Spring Boot 项目
    2. 添加依赖
    3. 配置数据源属性
    POM文件
    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starterartifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>mysqlgroupId>
    8. <artifactId>mysql-connector-javaartifactId>
    9. <version>8.0.32version>
    10. dependency>
    11. <dependency>
    12. <groupId>com.baomidougroupId>
    13. <artifactId>dynamic-datasource-spring-boot-starterartifactId>
    14. <version>3.5.2version>
    15. dependency>
    16. <dependency>
    17. <groupId>com.dmgroupId>
    18. <artifactId>DmJdbcDriverartifactId>
    19. <version>1.8.0version>
    20. dependency>
    21. <dependency>
    22. <groupId>com.baomidougroupId>
    23. <artifactId>mybatis-plus-boot-starterartifactId>
    24. <version>3.5.3.2version>
    25. dependency>
    26. <dependency>
    27. <groupId>org.projectlombokgroupId>
    28. <artifactId>lombokartifactId>
    29. dependency>
    30. <dependency>
    31. <groupId>org.springframework.bootgroupId>
    32. <artifactId>spring-boot-starter-testartifactId>
    33. <scope>testscope>
    34. dependency>
    35. dependencies>
    YML文件配置多数据源
    1. spring:
    2. datasource:
    3. dynamic:
    4. druid:
    5. initial-size: 5
    6. min-idle: 5
    7. maxActive: 20
    8. # 配置获取连接等待超时的时间
    9. maxWait: 60000
    10. # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    11. timeBetweenEvictionRunsMillis: 6000
    12. # 配置一个连接在池中最小生存的时间,单位是毫秒
    13. minEvictableIdleTimeMillis: 60000
    14. # 配置一个连接在池中最大生存的时间,单位是毫秒
    15. maxEvictableIdleTimeMillis: 900000
    16. validationQuery: SELECT 1 FROM DUAL
    17. testWhileIdle: true
    18. testOnBorrow: false
    19. testOnReturn: false
    20. # 打开PSCache,并且指定每个连接上PSCache的大小
    21. poolPreparedStatements: true
    22. maxPoolPreparedStatementPerConnectionSize: 20
    23. primary: dm #设置默认的数据源或者数据源组,默认值即为dm
    24. strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
    25. datasource:
    26. dm:
    27. driver-class-name: dm.jdbc.driver.DmDriver
    28. url: jdbc:dm://localhost:5236
    29. username: xxxx
    30. password: xxxxxxxxx
    31. mysql:
    32. driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
    33. url: jdbc:mysql://localhost:3306/lps?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true
    34. username: xxxxx
    35. password: xxxxxxxxxxx
    实体类
    1. @Data
    2. public class CfdData implements Serializable {
    3. /**
    4. *
    5. */
    6. @TableField(value = "TIME")
    7. private LocalDateTime time;
    8. /**
    9. *
    10. */
    11. @TableField(value = "VAL")
    12. private String val;
    13. @TableField(exist = false)
    14. private static final long serialVersionUID = 1L;
    15. }
    DAO层
    1. @Mapper
    2. public interface CfdDataMapper extends BaseMapper {
    3. void insertForMysql(CfdData cfdData);
    4. Long selectCountFromMysql();
    5. List selectFromDm();
    6. }
    XML映射层(达梦的SQL语句需要带上模式名称,而MySQL不需要)
    1. "insertForMysql">
    2. INSERT INTO CFD_DATA (TIME, VAL)
    3. VALUES (#{time}, #{val});
    Service接口层
    1. /**
    2. * @author 阿水
    3. * @ClassName CfdDataMySql
    4. * @description: TODO
    5. * @date 2023年10月21日
    6. * @version: 1.0
    7. */
    8. public interface CfdDataMySqlService extends IService {
    9. Long insertForMysql(List cfdData);
    10. Long selectCountFromMysql();
    11. }
    1. /**
    2. * @author 19449
    3. * @description 针对表【CFD_DATA】的数据库操作Service
    4. * @createDate 2023-10-21 23:32:05
    5. */
    6. public interface CfdDataService extends IService {
    7. List selectFromDm();
    8. }
    Service实现类(使用ds注解来选择数据源)
    1. @Service
    2. @DS("mysql")
    3. public class CfdDataMySqlServiceImpl extends ServiceImpl
    4. implements CfdDataMySqlService {
    5. @Resource
    6. private SqlSessionFactory sqlSessionFactory;
    7. @Resource
    8. private CfdDataMapper cfdDataMapper;
    9. @Override
    10. public Long insertForMysql(List cfdData) {
    11. SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
    12. try{
    13. CfdDataMapper mapper = session.getMapper(CfdDataMapper.class);
    14. cfdData.forEach(item->{
    15. mapper.insertForMysql(item);
    16. });
    17. session.commit();
    18. }
    19. finally {
    20. session.close();
    21. }
    22. return (long) cfdData.size();
    23. }
    24. @Override
    25. public Long selectCountFromMysql() {
    26. return cfdDataMapper.selectCountFromMysql();
    27. }
    28. }
    1. /**
    2. * @author 19449
    3. * @description 针对表【CFD_DATA】的数据库操作Service实现
    4. * @createDate 2023-10-21 23:32:05
    5. */
    6. @Service
    7. @DS("dm")
    8. public class CfdDataServiceImpl extends ServiceImpl
    9. implements CfdDataService {
    10. @Autowired
    11. private CfdDataMapper cfdDataMapper;
    12. @Override
    13. public List selectFromDm() {
    14. return cfdDataMapper.selectFromDm();
    15. }
    16. }

    测试代码

    1. @SpringBootTest
    2. class JdbcTemplateDemo1ApplicationTests {
    3. //达梦数据库
    4. @Resource
    5. CfdDataService dmService;
    6. //mysql数据库
    7. @Resource
    8. CfdDataMySqlService mySqlService;
    9. @Test
    10. void contextLoads() {
    11. List cfdData = dmService.selectFromDm();
    12. for (CfdData cfdDatum : cfdData) {
    13. System.out.println(cfdDatum);
    14. }
    15. System.out.println("本次DM数据数据条数为"+cfdData.size()+"条");
    16. Long countFromMysql = mySqlService.selectCountFromMysql();
    17. System.out.println("mysql当前条数为"+countFromMysql+"条");
    18. System.out.println("开始插入");
    19. mySqlService.insertForMysql(cfdData);
    20. System.out.println("插入完成");
    21. Long countAfterInsert = mySqlService.selectCountFromMysql();
    22. System.out.println("mysql插入完成后当前条数为"+countAfterInsert+"条");
    23. Long count = mySqlService.getBaseMapper().selectCount(null);
    24. /**
    25. * 这个走的是默认
    26. * primary: dm #设置默认的数据源或者数据源组,默认值即为dm
    27. */
    28. System.out.println("mybatisplus查询出来的count为"+count+"条");
    29. }
    30. }

    整体结构如下

    结论以及注意事项:不指定的话会走默认数据源

    注解结果
    没有@DS默认数据源
    @DS("dsName")dsName可以为组名也可以为具体某个库的名称
  • 相关阅读:
    20多年前微软曾计划收购,任天堂嘲讽道:“笑死我了”
    软件设计师第4题
    静态住宅代理是什么?为什么要选择它?
    【wpf】依赖属性三个回调详解
    curl命令获取外网ip
    机器学习集成学习进阶Xgboost算法原理
    前端的那些基本标签【a table tr caption image等等......】
    英语写作中“而不是”instead of、other than、rather than的用法(兼“选择”的表达)
    卷积公式和分布函数去解决Z=X+Y,Z=XY,Z=Y/X等Z的概率密度函数以及如何确定他们的范围
    算法 - 检查是否存在满足条件的数字组合
  • 原文地址:https://blog.csdn.net/lps12345666/article/details/133968456