return CRUD vertx 后的数据进入处理程序以再次使用它
return data after CRUD vertx into handler to use it again
public static void addMessage(RoutingContext routingContext) {
System.out.println("Add Message");
String message = routingContext.getBodyAsJson().getString("message");
JsonObject params = new JsonObject();
params.put("QUERY", Queries.DB_INSERT_MESSAGE);
params.put("PARAMS", new JsonArray().add(message));
DB.insert(params, res -> {
if (res.succeeded()) {
ClientCallBack.sendJsonResponce(routingContext, res.result().getJsonArray("rows"));
} else {
ClientCallBack.sendErrorResponce(routingContext, res.cause().getMessage());
}
});
她我的DB.Insert方法
public static void insert (JsonObject reqParams, Handler<AsyncResult<JsonObject>> handler){
insert(config.getJsonObject(CONS_CONFIG.DATABASE).getString(CONS_CONFIG.DEFAULT_DATABASE), reqParams, handler);
}
public static void insert (String DB_PREFIX, JsonObject reqParams, Handler<AsyncResult<JsonObject>> handler){
if(CONS_CONFIG.ORACLE.equalsIgnoreCase(DB_PREFIX)){
oracle.updateWithParams(reqParams, handler);
}else{
mysql.updateWithParams(reqParams, handler);
}
}
和 updateWithParams
public void updateWithParams(JsonObject paramsQuery, Handler<AsyncResult<JsonObject>> resultHandler) {
logger.debug("START WITH PARAMS : "+paramsQuery.encode());
String sql = paramsQuery.getString("QUERY");
JsonArray params = paramsQuery.getJsonArray("PARAMS");
getConnection(connection->{
if (connection.succeeded()) {
connection.result().updateWithParams(sql, params, query -> {
if (query.succeeded()) {
resultHandler.handle(Future.succeededFuture(query.result().toJson()));
} else {
logger.debug(query.cause().getMessage());
resultHandler.handle(Future.failedFuture(Msg.UPDATE_WITH_PARAMS_FAIL));
}
});
}else{
logger.debug(connection.cause().getMessage());
resultHandler.handle(Future.failedFuture(Msg.CONNECT_TO_DATA_BASE_FAIL));
}
connection.result().close();
});
}
我的问题是即使数据已在数据库中注册,但处理程序 return 为空值。每次 CRUD 操作后有什么办法 return 结果吗?
res.result().getJsonArray("rows") 在我的方法中 addMessage return null。
谢谢
正如@Paulo Lopes 所建议的,您应该使用 .getKeys() 方法:
mySQLClient.getConnection((res)->{
if (res.succeeded()) {
SQLConnection conn = res.result();
JsonArray params = new JsonArray().add(UUID.randomUUID().toString()).add("A").add("abcd");
conn.updateWithParams("insert into user (email, name, password) values (?, ?, ?)", params,
(r) -> {
if (r.succeeded()) {
// Your IDs
JsonArray keys = r.result().getKeys();
// This should work for MySQL
//String q = "select * from user where id in (?)";
// This should work with Oracle, since Oracle returns ROWID (string), not ID (integer)
String q = "select id from user where rowid in (?)";
conn.queryWithParams(q, keys, (result) -> {
// Return it instead of printing
if (result.succeeded()) {
System.out.println(result.result().toJson());
}
});
}
else {
// Handle error
}
});
}
else {
res.cause().printStackTrace();
}
// Deal with errors
});
public static void addMessage(RoutingContext routingContext) {
System.out.println("Add Message");
String message = routingContext.getBodyAsJson().getString("message");
JsonObject params = new JsonObject();
params.put("QUERY", Queries.DB_INSERT_MESSAGE);
params.put("PARAMS", new JsonArray().add(message));
DB.insert(params, res -> {
if (res.succeeded()) {
ClientCallBack.sendJsonResponce(routingContext, res.result().getJsonArray("rows"));
} else {
ClientCallBack.sendErrorResponce(routingContext, res.cause().getMessage());
}
});
她我的DB.Insert方法
public static void insert (JsonObject reqParams, Handler<AsyncResult<JsonObject>> handler){
insert(config.getJsonObject(CONS_CONFIG.DATABASE).getString(CONS_CONFIG.DEFAULT_DATABASE), reqParams, handler);
}
public static void insert (String DB_PREFIX, JsonObject reqParams, Handler<AsyncResult<JsonObject>> handler){
if(CONS_CONFIG.ORACLE.equalsIgnoreCase(DB_PREFIX)){
oracle.updateWithParams(reqParams, handler);
}else{
mysql.updateWithParams(reqParams, handler);
}
}
和 updateWithParams
public void updateWithParams(JsonObject paramsQuery, Handler<AsyncResult<JsonObject>> resultHandler) {
logger.debug("START WITH PARAMS : "+paramsQuery.encode());
String sql = paramsQuery.getString("QUERY");
JsonArray params = paramsQuery.getJsonArray("PARAMS");
getConnection(connection->{
if (connection.succeeded()) {
connection.result().updateWithParams(sql, params, query -> {
if (query.succeeded()) {
resultHandler.handle(Future.succeededFuture(query.result().toJson()));
} else {
logger.debug(query.cause().getMessage());
resultHandler.handle(Future.failedFuture(Msg.UPDATE_WITH_PARAMS_FAIL));
}
});
}else{
logger.debug(connection.cause().getMessage());
resultHandler.handle(Future.failedFuture(Msg.CONNECT_TO_DATA_BASE_FAIL));
}
connection.result().close();
});
}
我的问题是即使数据已在数据库中注册,但处理程序 return 为空值。每次 CRUD 操作后有什么办法 return 结果吗? res.result().getJsonArray("rows") 在我的方法中 addMessage return null。 谢谢
正如@Paulo Lopes 所建议的,您应该使用 .getKeys() 方法:
mySQLClient.getConnection((res)->{
if (res.succeeded()) {
SQLConnection conn = res.result();
JsonArray params = new JsonArray().add(UUID.randomUUID().toString()).add("A").add("abcd");
conn.updateWithParams("insert into user (email, name, password) values (?, ?, ?)", params,
(r) -> {
if (r.succeeded()) {
// Your IDs
JsonArray keys = r.result().getKeys();
// This should work for MySQL
//String q = "select * from user where id in (?)";
// This should work with Oracle, since Oracle returns ROWID (string), not ID (integer)
String q = "select id from user where rowid in (?)";
conn.queryWithParams(q, keys, (result) -> {
// Return it instead of printing
if (result.succeeded()) {
System.out.println(result.result().toJson());
}
});
}
else {
// Handle error
}
});
}
else {
res.cause().printStackTrace();
}
// Deal with errors
});