通过PostgreSQL与Vertx API的数据库连接

时间:2020-07-29 13:40:27

标签: postgresql backend vert.x dbeaver

我正在寻求帮助,实际上我正在尝试开发我的第一个网络应用程序,并且对数据库链接有疑问。

我已经使用Postgres在DBeaver中创建了一个数据库,并且还为公共用户创建了一对表,如下面的ScreeShot中所示...

enter image description here

好吧,有了这个数据库,我想将其链接到我的后端api,我在google的Vertx应用程序中找到了一些示例,但是不知道如何链接它。 这是我尝试理解代码的示例:

https://github.com/cescoffier/my-vertx-first-app/blob/master/src/main/java/io/vertx/blog/first/MyFirstVerticle.java

还有我正在工作的代码:

    private void addOne(RoutingContext routingContext) {
    final Project project = Json.decodeValue(routingContext.getBodyAsString(),
      Project.class);

    mongo.insert(COLLECTION, project.toJson(), r ->
      routingContext.response()
        .setStatusCode(201)
        .putHeader("content-type", "application/json; charset=utf-8")
        .end(Json.encodePrettily(project.setId(r.result()))));
  }

  private void updateOne(RoutingContext routingContext) {
    final String projects_id = routingContext.request().getParam("projects_id");
    JsonObject json = routingContext.getBodyAsJson();
    if (id == null || json == null) {
      routingContext.response().setStatusCode(400).end();
    } else {
      mongo.update(COLLECTION,
        new JsonObject().put("_id", id), // Select a unique document
        // The update syntax: {$set, the json object containing the fields to update}
        new JsonObject()
          .put("$set", json),
        v -> {
          if (v.failed()) {
            routingContext.response().setStatusCode(404).end();
          } else {
            routingContext.response()
              .putHeader("content-type", "application/json; charset=utf-8")
              .end(Json.encodePrettily(new Project(id, json.getString("name"), json.getString("origin"))));
          }
        });
    }
  }

我澄清这只是我用来了解如何将vertx应用程序的后端与数据库链接的一种方式。脚本说mongo的地方是我应该在哪里添加我的代码,以使用指令对数据库进行调用。

如果有人有任何建议,我会很好的!

谢谢,祝你有美好的一天!

0 个答案:

没有答案