不启用UserId 创建人, 使用方法说明
Description
APIJSON版本: master
不启用UserId, 大家使用可能不清楚, 写一个文档说明一下
1、将主键 和 UserId 配置成 主键字段
2、实现 FormVerifier
1) 前置: 先判断角色或者权限标识 2) 判断 是否启用 UserId, 如果不启用, 返回 true 即可
String visitorIdKey = getVisitorIdKey(config);
Object idKey = getIdKey(config.getDatabase(), config.getSchema(), config.getDatasource(), config.getTable());
Object userId = visitorIdKey.equals(idKey) ? null : visitorIdKey;
if (userId == null) { // null 无效
return true; // 不走后续流程
}
3、测试 插入数据
{
"Sys_post": {
"post_code": "001",
"post_name": "test",
"post_sort": 1,
"status": 0
},
"tag": "Sys_post",
"format": true,
"@explain": true
}
4、生成sql语句
INSERT INTO xxx.Sys_post(post_code,post_name,post_sort,status) VALUES('001','test',1,0)
如果想源码层面自动判断:
https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java#L436-L448 修改为如下代码:
Object idKey = getIdKey(config.getDatabase(), config.getSchema(), config.getDatasource(), table);
Object userId = visitorIdKey.equals(idKey) ? null : visitorIdKey;
if (userId != null) { // null 无效
List<String> nc = new ArrayList<>(c);
nc.add(visitorIdKey);
config.setColumn(nc);
List<List<Object>> nvs = new ArrayList<>();
List<Object> nvl;
for (List<Object> ovl : ovs) {
nvl = ovl == null || ovl.isEmpty() ? new ArrayList<>() : new ArrayList<>(ovl);
nvl.add(visitorId);
nvs.add(nvl);
}
config.setValues(nvs);
}
也可以在 DemoSQLConfig 配置 SIMPLE_CALLBACK 重写 getIdKey 和 getUserIdKey 统一处理
https://github.com/APIJSON/apijson-framework/blob/master/src/main/java/apijson/framework/APIJSONSQLConfig.java#L72-L83
也可以在 DemoSQLConfig 配置 SIMPLE_CALLBACK 重写 getIdKey 和 getUserIdKey 统一处理 https://github.com/APIJSON/apijson-framework/blob/master/src/main/java/apijson/framework/APIJSONSQLConfig.java#L72-L83
😁 第一步,就是重写这两个方法
