# apijson-column **Repository Path**: APIJSON/apijson-column ## Basic Information - **Project Name**: apijson-column - **Description**: 腾讯 APIJSON 的字段插件,支持 !key 反选字段 和 字段名映射。原仓库见 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: https://github.com/APIJSON/apijson-column - **GVP Project**: No ## Statistics - **Stars**: 9 - **Forks**: 4 - **Created**: 2021-04-05 - **Last Updated**: 2025-04-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: APIJSON ## README # apijson-column [![](https://jitpack.io/v/APIJSON/apijson-column.svg)](https://jitpack.io/#APIJSON/apijson-column) 腾讯 [APIJSON](https://github.com/Tencent/APIJSON) 4.6.6+ 的字段插件,支持 !key 反选字段 和 字段名映射,可通过 Maven, Gradle 等远程依赖。
A column plugin for Tencent [APIJSON](https://github.com/Tencent/APIJSON) 4.6.6+ , support Column Inverse and Column Mapping. ![image](https://user-images.githubusercontent.com/5738175/113572899-ab903380-964b-11eb-9f3c-69f3437d8a54.png) ![image](https://user-images.githubusercontent.com/5738175/113572926-b77bf580-964b-11eb-8a17-10917669c2aa.png) ## 添加依赖 ## Add Dependency ### Maven #### 1. 在 pom.xml 中添加 JitPack 仓库 #### 1. Add the JitPack repository to pom.xml ```xml jitpack.io https://jitpack.io ``` ![image](https://user-images.githubusercontent.com/5738175/167261814-d75d8fff-0e64-4534-a840-60ef628a8873.png)
#### 2. 在 pom.xml 中添加 apijson-column 依赖 #### 2. Add the apijson-column dependency to pom.xml ```xml com.github.APIJSON apijson-column LATEST ``` ![image](https://user-images.githubusercontent.com/5738175/167261792-7635c4b6-83a4-4d37-b0e5-e8455fdbed62.png)
https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/pom.xml

### Gradle #### 1. 在项目根目录 build.gradle 中最后添加 JitPack 仓库 #### 1. Add the JitPack repository in your root build.gradle at the end of repositories ```gradle allprojects { repositories { maven { url 'https://jitpack.io' } } } ```
#### 2. 在项目某个 module 目录(例如 `app`) build.gradle 中添加 apijson-column 依赖 #### 2. Add the apijson-column dependency in one of your modules(such as `app`) ```gradle dependencies { implementation 'com.github.APIJSON:apijson-column:latest' } ```


## 初始化 ## Initialization #### 1.在你项目继承 AbstractSQLConfig 的子类 static {} 代码块配置映射关系 #### 1.Configure mappings in static {} of your SQLConfig extends AbstractSQLConfig ```java static { Map> tableColumnMap = new HashMap<>(); tableColumnMap.put("User", Arrays.asList(StringUtil.split("id,sex,name,tag,head,contactIdList,pictureList,date"))); ColumnUtil.VERSIONED_TABLE_COLUMN_MAP.put(null, tableColumnMap); Map userKeyColumnMap = new HashMap<>(); userKeyColumnMap.put("gender", "sex"); // gender -> sex Map> keyColumnMap = new HashMap<>(); keyColumnMap.put("User", userKeyColumnMap); ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, keyColumnMap); ColumnUtil.init(); } ``` ![image](https://user-images.githubusercontent.com/5738175/167261660-f22a65a1-41ec-41c2-a97e-f4e809a3ddc9.png)
#### 2.在你项目继承 AbstractSQLConfig 的子类重写方法 setColumn, getKey #### 2.Override setColumn, getKey in your SQLConfig extends AbstractSQLConfig ```java @Override public AbstractSQLConfig setColumn(List column) { return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), getMethod())); } @Override public String getKey(String key) { return super.getKey(ColumnUtil.compatInputKey(key, getTable(), getMethod())); } ``` ![image](https://user-images.githubusercontent.com/5738175/167261697-48d54c3f-2913-4e07-8e41-80058688ac8b.png)
#### 3.在你项目继承 AbstractSQLExecutor 的子类重写方法 getKey #### 3.Override getKey in your SQLExecutor extends AbstractSQLExecutor ```java @Override protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table, int columnIndex, Map childMap) throws Exception { return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod()); } ``` ![image](https://user-images.githubusercontent.com/5738175/167261741-7d9436bc-bd12-447c-bfa5-20631497164f.png)

#### 见 [ColumnUtil](/src/main/java/apijson/column/ColumnUtil.java) 的注释及 [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot) 的 [DemoSQLConfig](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLConfig.java) 和 [DemoSQLExecutor](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLExecutor.java)
#### See document in [ColumnUtil](/src/main/java/apijson/column/ColumnUtil.java) and [DemoSQLConfig](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLConfig.java), [DemoSQLExecutor](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLExecutor.java) in [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot) ```java static { // 反选字段配置 Map> tableColumnMap = new HashMap<>(); tableColumnMap.put("User", Arrays.asList(StringUtil.split("id,sex,name,tag,head,contactIdList,pictureList,date"))); // 需要对应方法传参也是这样拼接才行,例如 ColumnUtil.compatInputColumn(column, getSQLDatabase() + "-" + getSQLSchema() + "-" + getTable(), getMethod()); tableColumnMap.put("MYSQL-sys-Privacy", Arrays.asList(StringUtil.split("id,certified,phone,balance,_password,_payPassword"))); ColumnUtil.VERSIONED_TABLE_COLUMN_MAP.put(null, tableColumnMap); // 字段名映射配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Map> tableKeyColumnMap = new HashMap<>(); Map userKeyColumnMap = new HashMap<>(); userKeyColumnMap.put("gender", "sex"); userKeyColumnMap.put("createTime", "date"); tableKeyColumnMap.put("User", userKeyColumnMap); Map privacyKeyColumnMap = new HashMap<>(); privacyKeyColumnMap.put("rest", "balance"); // 需要对应方法传参也是这样拼接才行,例如 ColumnUtil.compatInputKey(super.getKey(key), getSQLDatabase() + "-" + getSQLSchema() + "-" + getTable(), getMethod()); tableKeyColumnMap.put("MYSQL-sys-Privacy", privacyKeyColumnMap); ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, tableKeyColumnMap); // 字段名映射配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ColumnUtil.init(); } ```


## 使用 ## Usage ### 1.反选字段 ### 1.Column Inverse "@column": "!columnKey" // 返回排除 columnKey 后的全部其它字段
"@column": "!columnKey" // return all columns except for columnKey ```js { "User": { // id,sex,name,tag,head,contactIdList,pictureList,date "id": 82001, "@column": "!contactIdList" // -> id,sex,name,tag,head,pictureList,date } } ``` ![image](https://user-images.githubusercontent.com/5738175/113572899-ab903380-964b-11eb-9f3c-69f3437d8a54.png) ### 2.字段名映射 ### 2.Column Mapping "@column": "showKey" // 隐藏了数据库的对应真实字段名
"@column": "showKey" // the real column name is hidden ```js { "User": { // id,sex,name,tag,head,contactIdList,pictureList,date "id": 82001, "@column": "gender" // -> sex } } ``` ![image](https://user-images.githubusercontent.com/5738175/113572926-b77bf580-964b-11eb-8a17-10917669c2aa.png) 注意:[APIAuto](https://github.com/TommyLemon/APIAuto) 不能自动获取并展示对应映射字段 showKey 的类型、长度、注释等文档,只能通过手写注释来实现
Note: [APIAuto](https://github.com/TommyLemon/APIAuto) cannot automatically get and show the document for the showKey, you can add comment manually.
有问题可以去 Tencent/APIJSON 提 issue
https://github.com/Tencent/APIJSON/issues/36

#### 点右上角 ⭐Star 支持一下,谢谢 ^_^ #### Please ⭐Star this project ^_^ https://github.com/APIJSON/apijson-column