Mysql -> 达梦数据库适配

数据库连接配置
如果使用了阿里的德鲁伊连接池需要去掉Wall的过滤器,有疑问的请看
https://eco.dameng.com/document/dm/zh-cn/faq/faq-java.html#druid%20%E6%8A%A5%E9%94%99%EF%BC%9AdbType%20not%20support

无druid

spring:
  datasource:
    driver-class-name: dm.jdbc.driver.DmDriver
    # 两种都可以,推荐用第二种
    #url: jdbc:dm://ip:端口?schema=数据库
    url: jdbc:dm://ip:端口/数据库
    username: 账号
    password: '密码'

使用druid

spring:
  datasource:
    druid:
      db-type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: dm.jdbc.driver.DmDriver
      url: jdbc:dm://ip:端口/PAI_MC?autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
      username: 账号
      password: '密码'
      #最大活跃数
      max-active: 200
      #初始化数量
      initial-size: 5
      #最小空闲连接数
      min-idle: 5
      #最大连接等待超时时间
      max-wait: 10000
      #查询超时时间 单位是毫秒 这里配置30秒,如果你查询数据量大的情况下这个配置的大一点,不然查询报错抛出异常
      query-timeout: 30000
      #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      #配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
      #打开PSCache,并且指定每个连接PSCache的大小
      pool-prepared-statements: true
      max-open-prepared-statements: 20
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      async-init: true
      #配置监控统计拦截的filters,wall不支持达梦
      filters: stat
      # Druid 监控 Servlet 配置参数
      stat-view-servlet:
        allow: ''
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        login-username: 'admin'
        login-password: '不能告诉别人哦'
      # Druid 监控过滤相关配置参数
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.jpeg,*.png,*.css,*.ico,*.jsp,/druid/*'
        session-stat-max-count: 2000
        session-stat-enable: true
        principal-session-name: session_user_key
        profile-enable: true

sql语法、函数与mysql不兼容的时候

1、配置Mybatis-plus databaseId
2、复制sql 并添加属性databaseId
3、sql中databaseId没对应上的时候默认取没配置databaseId的sql

mybatis-plus:
  configuration:
  # 指定数据库别名,用于mybatis中xml的sql属性databaseId,带版本号
    database-id: dameng8

增量脚本目录规范

统一储存到 meta/sql/dameng8meta/sql则是默认的路径

例子

从json中找到对应key包含的值等于某个值

原xml

    <select id="findByTableId" resultType="cn.flyrise.mc.api.model.po.MsgUserChannelPO">
        select uc.* from msg_user_channel uc left join msg_original o on uc.msg_id = o.id
        where o.ws_content->'$.param.tableId' = #{tableId}
        <if test="userId!=null and userId!=''">
            and uc.user_id = #{userId}
        </if>
        <if test="channel!=null and channel!=''">
            and uc.send_channel = #{channel}
        </if>
        and uc.msg_read = #{read};
    </select>

由于达梦中没有 ‘->’ 这样的语法所以该sql执行会报错

### SQL: select uc.*,? from msg_user_channel uc left join msg_original o on uc.msg_id = o.id         where o.ws_content->'$.param.tableId' = ?                       and uc.user_id = ?                                 and uc.send_channel = ?                   and uc.msg_read = ?;
### Cause: dm.jdbc.driver.DMException: 第9 行附近出现错误:
不支持的语句类型
; uncategorized SQLException; SQL state [30000]; error code [-2004]; 第9 行附近出现错误:
  • 在同一个xml中再添加一个findByTableId的sql, 并设置databaseId=dameng8
  • 修改sql中 ‘->’ 转为json_value
  • id相同没关系,有警告的话是插件的问题
    <select id="findByTableId" resultType="cn.flyrise.mc.api.model.po.MsgUserChannelPO">
        select uc.*,#{_databaseId} from msg_user_channel uc left join msg_original o on uc.msg_id = o.id
        where o.ws_content->'$.param.tableId' = #{tableId}
        <if test="userId!=null and userId!=''">
            and uc.user_id = #{userId}
        </if>
        <if test="channel!=null and channel!=''">
            and uc.send_channel = #{channel}
        </if>
        and uc.msg_read = #{read};
    </select>

    <select id="findByTableId" resultType="cn.flyrise.mc.api.model.po.MsgUserChannelPO" databaseId="dameng8">
        select uc.* from msg_user_channel uc left join msg_original o on uc.msg_id = o.id
        where json_value(o.ws_content,'$.param.tableId') = #{tableId}
        <if test="userId!=null and userId!=''">
            and uc.user_id = #{userId}
        </if>
        <if test="channel!=null and channel!=''">
            and uc.send_channel = #{channel}
        </if>
        and uc.msg_read = #{read};
    </select>
文档更新时间: 2024-03-25 11:23   作者:朱灿奕