springboot整合多个达梦数据源

直接上干货!

pom.xml文件

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.dameng</groupId>
            <artifactId>Dm8JdbcDriver18</artifactId>
            <version>8.1.1.49</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>

yaml文件配置:

spring:
  datasource:
    dynamic:
      primary: db_realname # 配置默认数据库
      datasource:
        db_realname:
          driver-class-name: dm.jdbc.driver.DmDriver
          type: com.alibaba.druid.pool.DruidDataSource
          url: jdbc:dm://localhost:5237?schema=smartsitetest2
          username: TEST
          password: 123456789
          #连接池
          druid:
            # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
            connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
            # 初始化大小,最小,最大
            initial-size: 5
            max-active: 50
            min-idle: 5
            # 配置获取连接等待超时时间
            max-wait: 80000
            # 打开PSCache,并且指定每个连接上PSCache的大小
            pool-prepared-statements: true
            max-pool-prepared-statement-per-connection-size: 20
            validation-query: SELECT 'x'
            validation-query-timeout: 60000
            test-on-borrow: true
            test-on-return: true
            test-while-idle: true
            # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            time-between-eviction-runs-millis: 60000
            min-evictable-idle-time-millis: 300000
            log-abandoned: true
            remove-abandoned: true
            remove-abandoned-timeout: 1800
            filters: stat
        db_iot:
          driver-class-name: dm.jdbc.driver.DmDriver
          type: com.alibaba.druid.pool.DruidDataSource
          url: jdbc:dm://localhost:5237?schema=smartsiteiottest
          username: TEST
          password: 123456789
          #连接池
          druid:
            # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
            connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
            # 初始化大小,最小,最大
            initial-size: 5
            max-active: 50
            min-idle: 5
            # 配置获取连接等待超时时间
            max-wait: 80000
            # 打开PSCache,并且指定每个连接上PSCache的大小
            pool-prepared-statements: true
            max-pool-prepared-statement-per-connection-size: 20
            validation-query: SELECT 'x'
            validation-query-timeout: 60000
            test-on-borrow: true
            test-on-return: true
            test-while-idle: true
            # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            time-between-eviction-runs-millis: 60000
            min-evictable-idle-time-millis: 300000
            log-abandoned: true
            remove-abandoned: true
            remove-abandoned-timeout: 1800
            filters: stat
  autoconfigure:
    exclude:  com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure # 去除druid配置
#mybatis
mybatis-plus:
  mapper-locations: classpath*:/mapper/*.xml
  #实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: com.xxx.entity
  global-config:
    #数据库相关配置
    db-config:
      #主键类型  AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
      id-type: AUTO
      #字段策略 IGNORED:"忽略判断",NOT_NULL:"非 NULL 判断"),NOT_EMPTY:"非空判断"
      field-strategy: NOT_NULL
      #驼峰下划线转换
      column-underline: true
      logic-delete-value: -1
      logic-not-delete-value: 0
    banner: false
  #原生配置
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    call-setters-on-nulls: true
    jdbc-type-for-null: 'null'
    #打印SQL语句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#日志级别
logging:
  level:
    root: INFO
    org.springframework.web.servlet.DispatcherServlet: DEBUG
  file:
    path: logs



SQL语句符合DM的语法就行,本人亲测通过,记录一下,帮助别人。

### Spring Boot 集成达梦数据库 DM8 的配置教程 #### 1. 环境准备与依赖配置 为了在Spring Boot项目中集成达梦数据库DM8,首先需要准备好必要的环境以及引入所需的依赖项。以下是具体的步骤: - **下载达梦数据库驱动** 达梦数据库的官方文档指出,在Windows环境下可以使用DMB8开发版(适用于64位系统)。此版本提供了两个必需的JAR文件:`DmJdbcDriver18.jar` 和 `DmDialect-for-hibernate5.3.jar`[^4]。 - **添加Maven依赖** 尽管这些JAR文件未被上传至公共Maven仓库,但仍可以通过手动方式将其加入到项目的构建工具中。例如,通过以下命令将它们安装到本地Maven库: ```bash mvn install:install-file -Dfile=DmJdbcDriver18.jar -DgroupId=com.dameng -DartifactId=dmjdbcdriver -Dversion=1.0 -Dpackaging=jar ``` 接下来,在`pom.xml`文件中声明如下依赖关系: ```xml <dependency> <groupId>com.dameng</groupId> <artifactId>dmjdbcdriver</artifactId> <version>1.0</version> </dependency> ``` #### 2. 数据源配置 完成上述准备工作之后,需对Spring Boot数据源进行适当调整以适配达梦数据库的具体需求。如果涉及多个数据源,则应排除默认自动配置类`DruidDataSourceAutoConfigure.class`而非普通的`DataSourceAutoConfiguration.class`[^3]。 编辑`application.properties`或`application.yml`文件中的相关内容如下所示: ```properties spring.datasource.driver-class-name=com.dameng.jdbc.Driver spring.datasource.url=jdbc:dameng://localhost:5236/testdb spring.datasource.username=testuser spring.datasource.password=testpassword ``` 其中,URL部分指定了主机地址、端口号以及目标数据库名称;用户名和密码则依据实际部署情况填写相应值。 #### 3. MyBatis整合示例 当基础设置完成后,可进一步实现MyBatis框架下的SQL映射功能。假设我们继续沿用之前提到过的SYSTEM表内的LPS模式下Student表作为例子[^1],那么对应的Mapper接口定义可能类似于这样: ```java @Mapper public interface StudentMapper { @Select("SELECT * FROM LPS.STUDENT WHERE ID=#{id}") Student findById(Integer id); } ``` 同时也要记得扫描该Mapper所在的包路径以便于程序能够正常加载它: ```java @SpringBootApplication(scanBasePackages = {"com.example.mapper"}) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 以上即为整个流程概述,涵盖了从初始搭建直至基本查询操作的部分。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿熊跃晖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值