在一个put请求中更新多个字段

wqnecbli  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(293)

我有一个 Client 中的表 Oracle 使用以下字段及其值,

+----------+--------------+----------------------+------------------+-----------+----------+
|client_id |  client_name |         url          |  connection_pool | username  | password |
+----------+--------------+----------------------+------------------+-----------+----------+
|  test    |   clientDb   | http://clienturl.com |       QA_DB      |  user     |  admin   |
--------------------------------------------------------------------------------------------

我创造了一个 PUT 我的请求 Java 更新字段的应用程序。

@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Boolean updateClient(@PathParam("id") String id, Client client) throws DaoException {
    this.setupController();
    return controller.updateClientDetails(id, client);
}

客户端控制器:

public Boolean updateClientDetails(String id, Client client) throws DaoException {
    Client dbClient = clientDao.getClient(id)); //returns client present in table on basis of client_id
    if (!client.getName().equals(dbClient.getName())) {
        return clientDao.updateClientName(id, client.getName());
    }
    if (!client.getUrl().equals(dbClient.getUrl())) {
        return clientDao.updateClientUrl(id, client.getUrl());
    }
    else {
        LOGGER.info("Unable to update client {}", id);
        return false;
    }
}

客户道:我正在更新 client_name 以及 url 分别如下:

public Boolean updateClientName(String id, String name) throws DaoException {
    final String sql = "UPDATE client SET client_name = ? WHERE client_id = ?";
    LOGGER.debug(SQL_STRING.toLogback(sql), name, id);
    try (final Connection connection = dataSource.getConnection();
            final PreparedStatement statement = connection.prepareStatement(sql)) {
        statement.setString(1, name);
        statement.setString(2, id);
        statement.executeUpdate();
        return true;
    } catch (SQLException ex) {
        LOGGER.debug("Exception while updating client {} ", id, ex);
        throw new DaoException(ex);
    }
}

//updateClientUrl(String id, String url) method - similar logic as above for updating url value

在发送 PUT 请求时,它一次更新一个字段,即,如果在 Client 对象,它更新第一个请求中的一个字段。对于要更新的第二个属性,我需要再次发送请求。
我想知道有没有一种方法可以在一个请求中更新多个值,而不是发送多个请求并为每个字段使用多个更新方法?
解决方案可能很简单,但它不是点击我!感谢您的帮助。

uurv41yg

uurv41yg1#

如果我理解正确,您希望一次更新一个或多个字段。为此,您必须根据您想要更改哪个部分/列的条件构建查询。所以你的这部分:

final String sql = "UPDATE client SET client_name = ? ";

需要改变。你必须建立你的 sql 像这样的查询(例如,您有客户机对象及其字段--):

String sql = "UPDATE client SET client_name = ?";
if(client.getUrl()!=null && !client.getUrl().isEmpty())
  sql+=", set url=?";
// similar for other fields you want to update
at last, concate your condition:
sql+=" WHERE client_id = ?";

现在,您必须提供那些已动态设置为要更新的查询的字段:

statement.setString(1, name);
if(client.getUrl()!=null && !client.getUrl().isEmpty())
{
  statement.setString(2, client.getUrl());
  statement.setString(3, id);
}
else
    statement.setString(2, id);

对于查询位置的索引,最好使用“indexvariable”根据条件自动跟踪其值。
如果这不符合你的问题,请打电话。

相关问题