Skip to main content

Update

Hint

This article is generated by AI translation.

Use LambdaTemplate to update data in three ways, and mix them as needed:

Tip

For building conditions during updates, see where builder.

Update fields

Basic usage 1: Lambda
LambdaTemplate lambda = ...;
int result = lambda.update(User.class)
.eq(User::getId, 1) // match condition
.updateTo(User::getName, "Mary") // update field via Lambda
.updateTo(User::getStatus, 2) // chain multiple field updates
.doUpdate();
// result is the affected row count
Basic usage 2: String property
LambdaTemplate lambda = ...;
int result = lambda.update(User.class)
.eq(User::getId, 1) // match condition
.updateToUsingStr("name", "Mary") // update field via property name
.updateToUsingStr("status", 2) // chain multiple field updates
.doUpdate();
// result is the affected row count

Sample-driven

When many fields need updating, build a sample object to avoid long updateTo chains.

Use a Bean as the sample
User sample = new User();
sample.setName("new name");
sample.setStatus(2);

LambdaTemplate lambda = ...;
int result = lambda.update(User.class)
.ne(User::getStatus, 2) // match records whose status != 2
.updateToSample(sample) // update name and status
.doUpdate();
// result is the affected row count
Use a Map as the sample
Map<String, Object> sample = new HashMap<>();
sample.put("name", "new name");
sample.put("status", 2);

LambdaTemplate lambda = ...;
int result = lambda.update(User.class)
.ne(User::getStatus, 2) // match records whose status != 2
.updateToSampleMap(sample) // update name and status
.doUpdate();
// result is the affected row count
Usage notes
  • updateToSample only looks at non-null sample properties:
    • Set fields you do not want to update to null in the sample.
    • Avoid primitive properties (byte, short, int, long, float, double, char) in samples.
    • To update a field to null, add field updates.
  • If updateToSample is called multiple times, later samples override overlapping fields.

Replace row

replace the entire row with the provided data.

Use a Bean
User user = new User();
user.setName("new name");
user.setStatus(2);

LambdaTemplate lambda = ...;
int result = lambda.update(User.class)
.ne(User::getStatus, 2) // match records whose status != 2
.updateRow(user) // all columns use values from user
.doUpdate();
// result is the affected row count

In the example above:

  • User has four properties: id, name, status, age.
  • Two properties are set on the update object.
  • id is the primary key.

The final update sets name, status, and age; age becomes null.

Update primary key

Important

Primary key columns are excluded by default. Therefore:

  • updateToSample and updateRow ignore primary keys unless allowed.
  • Updating a primary key elsewhere throws an exception.

Call allowUpdateKey to permit updating primary keys.

LambdaTemplate lambda = ...;
int result = lambda.update(User.class)
.eq(User::getId, 1) // match condition
.allowUpdateKey() // allow updating primary key
.updateTo(User::getId, 2) // update primary key
.doUpdate();
// result is the affected row count

Empty conditions

Updates without any conditions are dangerous and blocked by default.

To update the entire table, call allowEmptyWhere for this operation.

LambdaTemplate lambda = ...;
int result = lambda.update(User.class)
.allowEmptyWhere() // allow no conditions
.updateTo(User::getStatus, 2) // set status to 2 for the whole table
.doUpdate();
// result is the affected row count