feat: 新增 database-id 配置项,适配多数据库SQL语法差异,提升MyBatis XML兼容性

Co-authored-by: Billy<378012098@qq.com>



# message auto-generated for no-merge-commit merge:
merge billy into dev

MyBatis xml 文件支持 增加针对不同数据库(mysql,pgsql)的sql 分别定义

Created-by: onekr-billy
Commit-by: Billy
Merged-by: Charles_7c
Description: <!--
  非常感谢您的 PR!在提交之前,请务必确保您 PR 的代码经过了完整测试,并且通过了代码规范检查。
-->

<!-- 在 [] 中输入 x 来勾选) -->

## PR 类型

<!-- 您的 PR 引入了哪种类型的变更? -->
<!-- 只支持选择一种类型,如果有多种类型,可以在更新日志中增加 “类型” 列。 -->

- [ ] 新 feature
- [ ] Bug 修复
- [x] 功能增强
- [ ] 文档变更
- [ ] 代码样式变更
- [ ] 重构
- [ ] 性能改进
- [ ] 单元测试
- [ ] CI/CD
- [ ] 其他

## PR 目的

新增 database-id 配置项,适配多数据库SQL语法差异,提升MyBatis XML兼容性

## 解决方案

新增databaseId配置项,通过数据库厂商标识(DatabaseIdProvider)机制,实现MyBatis XML映射文件中多数据库SQL语法的动态适配,有效提升多数据库场景下的兼容性与可维护性。

## PR 测试

<!-- 如果可以,请为您的 PR 添加或更新单元测试。 -->
<!-- 请描述一下您是如何测试 PR 的。例如:创建/更新单元测试或添加相关的截图。 -->

## Changelog

| 模块  | Changelog | Related issues |
|-----|-----------| -------------- |
|     |           |                |

<!-- 如果有多种类型的变更,可以在变更日志表中增加 “类型” 列,该列的值与上方 “PR 类型” 相同。 -->
<!-- Related issues 格式为 Closes #<issue号>,或者 Fixes #<issue号>,或者 Resolves #<issue号>。 -->

## 其他信息

<!-- 请描述一下还有哪些注意事项。例如:如果引入了一个不向下兼容的变更,请描述其影响。 -->

## 提交前确认

- [x] PR 代码经过了完整测试,并且通过了代码规范检查
- [ ] 已经完整填写 Changelog,并链接到了相关 issues
- [x] PR 代码将要提交到 dev 分支

See merge request: continew/continew-admin!5
This commit is contained in:
onekr-billy
2025-07-02 19:38:42 +08:00
committed by Charles_7c
parent e4d582b742
commit 151a0faeb0
3 changed files with 61 additions and 7 deletions

View File

@@ -17,7 +17,14 @@
LEFT JOIN sys_message_log AS t2 ON t2.message_id = t1.id
<where>
<if test="query.userId != null">
(t1.scope = 1 OR (t1.scope = 2 AND JSON_EXTRACT(t1.users, "$[0]") = CAST(#{query.userId} AS CHAR)))
<choose>
<when test="_databaseId == 'mysql'">
(t1.scope = 1 OR (t1.scope = 2 AND JSON_EXTRACT(t1.users, "$[0]") = CAST(#{query.userId} AS CHAR)))
</when>
<when test="_databaseId == 'pgsql'">
(t1.scope = 1 OR (t1.scope = 2 AND t1.users::jsonb @> jsonb_build_array(#{query.userId}::text)))
</when>
</choose>
</if>
<if test="query.title != null and query.title != ''">
AND t1.title LIKE CONCAT('%', #{query.title}, '%')
@@ -54,7 +61,15 @@
t1.*
FROM sys_message AS t1
LEFT JOIN sys_message_log AS t2 ON t2.message_id = t1.id AND t2.user_id = #{userId}
WHERE (t1.scope = 1 OR (t1.scope = 2 AND JSON_CONTAINS(t1.users, CONCAT('"', #{userId}, '"'))))
WHERE
<choose>
<when test="_databaseId == 'mysql'">
(t1.scope = 1 OR (t1.scope = 2 AND JSON_CONTAINS(t1.users, CONCAT('"', #{userId}, '"'))))
</when>
<when test="_databaseId == 'pgsql'">
(t1.scope = 1 OR (t1.scope = 2 AND t1.users::jsonb @> jsonb_build_array(#{userId}::text) ))
</when>
</choose>
AND t2.read_time IS NULL
</select>
@@ -63,7 +78,15 @@
COUNT(1)
FROM sys_message AS t1
LEFT JOIN sys_message_log AS t2 ON t2.message_id = t1.id AND t2.user_id = #{userId}
WHERE (t1.scope = 1 OR (t1.scope = 2 AND JSON_CONTAINS(t1.users, CONCAT('"', #{userId}, '"'))))
WHERE
<choose>
<when test="_databaseId == 'mysql'">
(t1.scope = 1 OR (t1.scope = 2 AND JSON_CONTAINS(t1.users, CONCAT('"', #{userId}, '"'))))
</when>
<when test="_databaseId == 'pgsql'">
(t1.scope = 1 OR (t1.scope = 2 AND t1.users::jsonb @> jsonb_build_array(#{userId}::text) ))
</when>
</choose>
AND t2.read_time IS NULL
<if test="type != null">
AND t1.type = #{type}

View File

@@ -24,7 +24,14 @@
LEFT JOIN sys_notice_log AS t2 ON t2.notice_id = t1.id
<where>
<if test="query.userId != null">
(t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_CONTAINS(t1.notice_users, CONCAT('"', #{query.userId}, '"'))))
<choose>
<when test="_databaseId == 'mysql'">
(t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_CONTAINS(t1.notice_users, CONCAT('"', #{query.userId}, '"'))))
</when>
<when test="_databaseId == 'pgsql'">
(t1.notice_scope = 1 OR (t1.notice_scope = 2 AND t1.notice_users::jsonb @> jsonb_build_array(#{query.userId}::text)))
</when>
</choose>
</if>
<if test="query.title != null and query.title != ''">
AND t1.title LIKE CONCAT('%', #{query.title}, '%')
@@ -46,9 +53,24 @@
t1.id
FROM sys_notice AS t1
LEFT JOIN sys_notice_log AS t2 ON t2.notice_id = t1.id AND t2.user_id = #{userId}
WHERE (t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_CONTAINS(t1.notice_users, CONCAT('"', #{userId}, '"'))))
WHERE
<choose>
<when test="_databaseId == 'mysql'">
(t1.notice_scope = 1 OR (t1.notice_scope = 2 AND JSON_CONTAINS(t1.notice_users, CONCAT('"', #{userId}, '"'))))
</when>
<when test="_databaseId == 'pgsql'">
(t1.notice_scope = 1 OR (t1.notice_scope = 2 AND t1.notice_users::jsonb @> jsonb_build_array(#{userId}::text)))
</when>
</choose>
<if test="noticeMethod != null">
AND JSON_CONTAINS(t1.notice_methods, CAST(#{noticeMethod} AS CHAR))
<choose>
<when test="_databaseId == 'mysql'">
AND JSON_CONTAINS(t1.notice_methods, CAST(#{noticeMethod} AS CHAR))
</when>
<when test="_databaseId == 'pgsql'">
AND (t1.notice_methods::jsonb @> jsonb_build_array(#{noticeMethod}::text))
</when>
</choose>
</if>
AND t2.read_time IS NULL
</select>
@@ -60,7 +82,14 @@
FROM sys_notice
WHERE status = 3
<if test="userId != null">
AND (notice_scope = 1 OR (notice_scope = 2 AND JSON_CONTAINS(notice_users, CONCAT('"', #{userId}, '"'))))
<choose>
<when test="_databaseId == 'mysql'">
AND (notice_scope = 1 OR (notice_scope = 2 AND JSON_CONTAINS(notice_users, CONCAT('"', #{userId}, '"'))))
</when>
<when test="_databaseId == 'pgsql'">
AND (notice_scope = 1 OR (notice_scope = 2 AND notice_users::jsonb @> jsonb_build_array(#{userId}::text)))
</when>
</choose>
</if>
ORDER BY is_top DESC, publish_time DESC
LIMIT 5