Tencent / APIJSON

🏆 实时 零代码、全功能、强安全 ORM 库 🚀 后端接口和文档零代码,前端(客户端) 定制返回 JSON 的数据和结构 🏆 Real-Time coding-free, powerful and secure ORM 🚀 providing APIs and Docs without coding by Backend, and the returned JSON of API can be customized by Frontend(Client) users
http://apijson.cn
Other
17.14k stars 2.15k forks source link

APIJSON 是否可以集成 dynamic-datasource 作为多数据源的注入 #669

Closed csx-bill closed 3 months ago

csx-bill commented 8 months ago

Description

https://github.com/baomidou/dynamic-datasource

是否可以提供集成 dynamic-datasource 多数据源 管理的demo

TommyLemon commented 8 months ago

不需要,APIJSON 已支持 @datasource 来切换数据源: https://github.com/Tencent/APIJSON/issues/264

当然同时用两者也不冲突,只不过它们相互独立互不影响

csx-bill commented 8 months ago

我是想 apijson 表 作为一个库 通过apijson 来查其他库表的数据,配置多数据源, image 但是在运行后, config.getDatabase() 是 null 值,取不到 数据库配置的 quick-boot

image

csx-bill commented 8 months ago

https://github.com/Tencent/APIJSON/issues/148 我看这个评论与回复 都是需要 前端 传 Datasource 来切换数据源,我理解成 access 配置了数据库名, image config 就能获取到数据库名了

TommyLemon commented 8 months ago

Access 表 schema 字段目前还没用上 https://gitee.com/Tencent/APIJSON/issues/I835W0

需要在 AbstractSQLConfig 或子类新增 Map<String, String> TABLE_SCHEMA_MAP,然后在 getSQLSchema 中当 schema 为空时从 map 里根据 getSQLTable 来取值 https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

csx-bill commented 8 months ago

getSQLSchema() 确实可以切换数据库查询,但是在 Connection getConnection(SQLConfig config) 还是没有 schema

TommyLemon commented 8 months ago

因为没传参 @schema,并且后端也没有实现和配置 TABLE_SCHEMA_MAP

csx-bill commented 3 months ago

因为没传参 @Schema,并且后端也没有实现和配置 TABLE_SCHEMA_MAP

这个功能后期会添加吗

TommyLemon commented 3 months ago

因为没传参 @Schema,并且后端也没有实现和配置 TABLE_SCHEMA_MAP

这个功能后期会添加吗

@csx-bill 这个功能实现很简单,参考 TABLE_KEY_MAP https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java#L133-L138

image

getSQLSchema 也一样优先用 TABLE_SCHEMA_MAP 取值返回 https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java#L1353-L1365

image

apijson-framework 自动在启动时通过 Access 表里配置的 schema 和 name 来 put 到 TABLE_SCHEMA_MAP https://github.com/APIJSON/apijson-framework/blob/master/src/main/java/apijson/framework/APIJSONVerifier.java#L253

image

改好后给 APIJSON 提交 PR 贡献,谢谢,开源要大家一起参与贡献才会更美好~ image

提 PR 贡献代码的步骤可参考: https://github.com/Tencent/APIJSON/blob/master/CONTRIBUTING.md#%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%80%E5%AE%9A%E8%A6%81%E8%B4%A1%E7%8C%AE%E4%BB%A3%E7%A0%81

csx-bill commented 3 months ago

如果两个库都有 sys_user 这个表名 TABLE_SCHEMA_MAP 会不会有问题?

TommyLemon commented 3 months ago

如果两个库都有 sys_user 这个表名 TABLE_SCHEMA_MAP 会不会有问题?

不用真实表名 name,而是用对外显示的表名 alias,这样就可以通过不同的 alias 名称来区分,避免冲突

csx-bill commented 3 months ago

如果两个库都有 sys_user 这个表名 TABLE_SCHEMA_MAP 会不会有问题?

不用真实表名 name,而是用对外显示的表名 alias,这样就可以通过不同的 alias 名称来区分,避免冲突 代码如下; TABLE_SCHEMA_MAP.put(alias ,Schema ); 例如: TABLE_SCHEMA_MAP.put(Column.class.getSimpleName(), 此处应该如何写?);

csx-bill commented 3 months ago

新增处: image 修改 getSQLSchema() image

修改 : apijson-framework image

image

csx-bill commented 3 months ago

已提交PR

TommyLemon commented 3 months ago

非常感谢,已合并

https://github.com/Tencent/APIJSON/pull/734

https://github.com/APIJSON/apijson-framework/pull/27

TommyLemon commented 3 months ago

看到还有个第三方实现: https://github.com/wb04307201/apijson-dynamic-datasource

csx-bill commented 3 months ago

看到还有个第三方实现: https://github.com/wb04307201/apijson-dynamic-datasource

看了它这个,有个想法,apijson,是不是可以增加一个数据源表,然后扫描该数据源表,进行数据源动态添加, TABLE_SCHEMA_MAP 已经实现了,然后根据 TABLE_SCHEMA_MAP 取值 切换数据源进行操作

TommyLemon commented 3 months ago

确实可以这样,理论上所有代码里的配置,都可以放到数据库里,动态加载出来替代代码里的

TommyLemon commented 3 months ago

或者直接 Access 表里添加 datasource, database 等 字段也是一种实现方式

csx-bill commented 3 months ago

或者直接 Access 表里添加 datasource, database 等 字段也是一种实现方式

这种 是不是每次请求 都得重新连接数据库?而不是连接池?

TommyLemon commented 3 months ago

对,不过可以接入连接池,有几个 demo: https://github.com/APIJSON/APIJSON-Demo/tree/master/APIJSON-Java-Server#%E8%BF%9E%E6%8E%A5%E6%B1%A0-demo

csx-bill commented 3 months ago

对,不过可以接入连接池,有几个 demo: https://github.com/APIJSON/APIJSON-Demo/tree/master/APIJSON-Java-Server#%E8%BF%9E%E6%8E%A5%E6%B1%A0-demo

目前我就是这样用的,TABLE_SCHEMA_MAP 等这个版本发布了,我就更换这个版本

csx-bill commented 3 months ago

非常感谢,已合并

734

APIJSON/apijson-framework#27

这个pr ,咨询APIJSON 下一个版本 发布时间 是什么时候,坐等使用

TommyLemon commented 3 months ago

按正常节奏大概是 2-3 周后的周末发版

TommyLemon commented 3 months ago

@csx-bill 发了 7.0.2,你用用看 https://github.com/Tencent/APIJSON/releases/tag/7.0.2 https://jitpack.io/#Tencent/APIJSON/7.0.2 apijson-framework 等其它库没发新版,可以依赖 APIJSON 7.0.2 并 exclude 排除里面依赖的 APIJSON 7.0.0 等旧版,避免冲突:

        <dependency>
            <groupId>com.github.Tencent</groupId>
            <artifactId>APIJSON</artifactId>
            <version>7.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.APIJSON</groupId>
            <artifactId>apijson-framework</artifactId>
            <version>7.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.Tencent</groupId>
                    <artifactId>APIJSON</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
csx-bill commented 3 months ago

@csx-bill 发了 7.0.2,你用用看 https://github.com/Tencent/APIJSON/releases/tag/7.0.2 https://jitpack.io/#Tencent/APIJSON/7.0.2

apijson-framework 没同步发呢

TommyLemon commented 3 months ago

@csx-bill 发了 7.0.2,你用用看 https://github.com/Tencent/APIJSON/releases/tag/7.0.2 https://jitpack.io/#Tencent/APIJSON/7.0.2

apijson-framework 没同步发呢

@csx-bill 这次 7.0.2 是专为你发的,apijson-framework 等其它库没发新版,可以依赖 APIJSON 7.0.2 并 exclude 排除里面依赖的 APIJSON 7.0.0 等旧版,避免冲突:

        <dependency>
            <groupId>com.github.Tencent</groupId>
            <artifactId>APIJSON</artifactId>
            <version>7.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.APIJSON</groupId>
            <artifactId>apijson-framework</artifactId>
            <version>7.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.Tencent</groupId>
                    <artifactId>APIJSON</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.APIJSON</groupId>
            <artifactId>apijson-column</artifactId>
            <version>2.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.Tencent</groupId>
                    <artifactId>APIJSON</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.APIJSON</groupId>
            <artifactId>apijson-router</artifactId>
            <version>2.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.Tencent</groupId>
                    <artifactId>APIJSON</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
TommyLemon commented 2 months ago

@csx-bill 7.0.3 发布 https://github.com/Tencent/APIJSON/releases/tag/7.0.3 https://github.com/APIJSON/apijson-framework/releases/tag/7.0.3 https://github.com/APIJSON/apijson-router/releases/tag/2.0.3 https://github.com/APIJSON/apijson-column/releases/tag/2.0.3