简体   繁体   English

Spring JDBC数据驱动的更新

[英]Spring JDBC Data Driven Update

I have a Spring application with a update API endpoint for a Postgres database. 我有一个Spring应用程序,它具有Postgres数据库的更新API端点。 The user can submit information and updates will be reflected in the database. 用户可以提交信息,更新将反映在数据库中。 The user only submits what they have to update. 用户只提交他们必须更新的内容。 For example consider the following object: 例如,考虑以下对象:

class Dog {
    String name;
    int age;
    String breed;
    // Attributes and getters/setters...
}

When the user submits a update request, they only send the information they wish to update, such as only name and breed. 当用户提交更新请求时,他们只发送他们希望更新的信息,例如仅发送名称和品种。 I have the following function that updates the database with information: 我有以下功能,用信息更新数据库:

public void update(String name, int age, String breed, JdbcTemplate template) {
    UpdateBuilder query = new UpdateBuilder();
    query.from("DogTable");
    boolean updated = false;
    if (name != null) {
        query.set("name" + " = '" + name + "'");
        updated = true;
    }
    if (age != null) {
        query.set("age" + " = '" + age + "'");
        updated = true;
    }
    if (breed != null) {
        query.set("breed" + " = '" + breed + "'");
        updated = true;
    }
    // And so on...
    if (updated) {
        query.set("UpdatedTime" + " = '" + new Date() + "'");
    }
    query.where("someKey" + " = '" + someId + "'");

    template.update(query.toString());
}

(The query.set() stuff is just a helper class that builds a query string) query.set()东西只是一个构建查询字符串的辅助类)

As you can see, this gets messy with all the "is the name given, is the age given?" 正如你所看到的那样,所有“给出的名字是给定的年龄?”会变得混乱。 checks. 检查。 That leads to my question: Is there a data driven approach to do this? 这引出了我的问题:是否有数据驱动的方法来做到这一点? What I would like to be able to do is: 我希望能做的是:

myJdbcTemplate.update(ListOfObjectsToUpdate, "TableName");

Simply, the JDBC template would see what I have provided it, and would proceed to update the provided table with that information. 简单地说,JDBC模板将看到我提供的内容,并将继续使用该信息更新提供的表。 Is this even possible? 这甚至可能吗? I realize that building queries using strings is bad, but PreparedStatements don't look much better in code (not to mention, they don't solve this issue). 我意识到使用字符串构建查询很糟糕,但PreparedStatements在代码中看起来并不好(更不用说,它们没有解决这个问题)。

You can use the COALESCE function for this purpose - add user value as well and existing value and if the user value is not null (intended update) it well be set as the new value. 您可以为此目的使用COALESCE函数 - 添加用户值和现有值,如果用户值不为null(预期更新),则可以将其设置为新值。

Similar to this - 与此类似 -

UPDATE "user" SET alternate_contact_name = COALESCE(<user value>, alternate_contact_name)

This is a MySQL query but COALESCE works same way in Postgresql 这是一个MySQL查询,但COALESCE在Postgresql中的工作方式相同

The user value will be set and new value if it is not null. 如果user value不为null,则将设置user value和新值。 When it is null and original value of column is not null, the original value if picked. 如果为null且column的原始值不为null,则选择原始值。 If both are null then it doesn't matter. 如果两者都为null则无关紧要。

WIth this you can simply pass all parameters and avoid building query in an untidy way. 通过这种方式,您可以简单地传递所有参数,避免以不整齐的方式构建查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM