腾讯 APIJSON 4.6.6+ 的字段插件,支持 !key 反选字段 和 字段名映射,可通过 Maven, Gradle 等远程依赖。
A column plugin for Tencent APIJSON 4.6.6+ , support Column Inverse and Column Mapping.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.APIJSON</groupId>
<artifactId>apijson-column</artifactId>
<version>LATEST</version>
</dependency>
https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/pom.xml
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.APIJSON:apijson-column:latest'
}
static {
Map<String, List<String>> 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<String, String> userKeyColumnMap = new HashMap<>();
userKeyColumnMap.put("gender", "sex"); // gender -> sex
Map<String, Map<String, String>> keyColumnMap = new HashMap<>();
keyColumnMap.put("User", userKeyColumnMap);
ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, keyColumnMap);
ColumnUtil.init();
}
@Override
public AbstractSQLConfig setColumn(List<String> column) {
return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), getMethod()));
}
@Override
public String getKey(String key) {
return super.getKey(ColumnUtil.compatInputKey(key, getTable(), getMethod()));
}
@Override
protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table,
int columnIndex, Map<String, JSONObject> childMap) throws Exception {
return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod());
}
见 ColumnUtil 的注释及 APIJSONBoot 的 DemoSQLConfig 和 DemoSQLExecutor
See document in ColumnUtil and DemoSQLConfig, DemoSQLExecutor in APIJSONBoot
static {
// 反选字段配置
Map<String, List<String>> 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<String, Map<String, String>> tableKeyColumnMap = new HashMap<>();
Map<String, String> userKeyColumnMap = new HashMap<>();
userKeyColumnMap.put("gender", "sex");
userKeyColumnMap.put("createTime", "date");
tableKeyColumnMap.put("User", userKeyColumnMap);
Map<String, String> 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();
}
"@column": "!columnKey" // 返回排除 columnKey 后的全部其它字段
"@column": "!columnKey" // return all columns except for columnKey
{
"User": { // id,sex,name,tag,head,contactIdList,pictureList,date
"id": 82001,
"@column": "!contactIdList" // -> id,sex,name,tag,head,pictureList,date
}
}
"@column": "showKey" // 隐藏了数据库的对应真实字段名
"@column": "showKey" // the real column name is hidden
{
"User": { // id,sex,name,tag,head,contactIdList,pictureList,date
"id": 82001,
"@column": "gender" // -> sex
}
}
注意:APIAuto 不能自动获取并展示对应映射字段 showKey 的类型、长度、注释等文档,只能通过手写注释来实现
Note: APIAuto cannot automatically get and show the document for the showKey, you can add comment manually.
有问题可以去 Tencent/APIJSON 提 issue
Tencent/APIJSON#36