DEV Community

ts5432
ts5432

Posted on

Update record using JooqTemplate

1. update

Update records matching the given condition. Supports both Condition and List.

// Single Condition
public int update(String table, Map bean, Condition condition)
public int update(Table table, Map bean, Condition condition)
// List<Condition>
public int update(String table, Map bean, List<Condition> conditions)
public int update(Table table, Map bean, List<Condition> conditions)
Enter fullscreen mode Exit fullscreen mode

Returns: int — number of affected rows.
Example:

// Using Condition
jt.update("user_table", values, F("id").eq(user.getId()));

// Using List<Condition>
jt.update("user_table", values,
    jt.conditions("id", user.getId(), "name<>", user.getName()));

// Using Table object
jt.update(T("user_table"), values, jt.conditions("id", user.getId()));
Enter fullscreen mode Exit fullscreen mode

2. updatev (varargs conditions)

Specify update conditions via varargs without manually constructing Condition objects. Operators can be embedded in field names.

public int updatev(String table, Map bean, Object... where)
public int updatev(Table table, Map bean, Object... where)
Enter fullscreen mode Exit fullscreen mode

Returns: int — number of affected rows.
Example:

// Equality  WHERE id = ?
jt.updatev("user_table", values, "id", user.getId());

// Multiple conditions  WHERE id = ? AND name <> ?
jt.updatev("user_table", values,
    "id", user.getId(), "name<>", user.getName());

// Mix Condition
jt.updatev(T("user_table"), values,
    "id", user.getId(), "name:isnotnull");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)