Skip to main content

Map Insert, Update, And Delete

Map write and mutation operations are provided by MapInsert, MapUpdate, and MapDelete. Mapped Map Mode and Freedom Map Mode use the same API set, but interpret fields differently.

Suitable For

  • Data is already Map<String, Object> and should be written or updated directly.
  • Batch imports, sync tasks, or dynamic field writes are required.
  • Update and delete operations should keep builder conditions and unsafe-operation protection.

Not Suitable For

  • Fields are fixed and compile-time checks are preferred. Use Entity Mode.
  • Mutation SQL is complex and requires multi-table update, subqueries, or database-specific syntax. Use JdbcTemplate or Mapper Files.
  • Entity CRUD by primary key is enough. Start with BaseMapper.

Operation Modes

OperationMapped Map ModeFreedom Map Mode
Insertlambda.insert(User.class).asMap()lambda.insertFreedom("users")
Updatelambda.update(User.class).asMap()lambda.updateFreedom("users")
Deletelambda.delete(User.class).asMap()lambda.deleteFreedom("users")
Field processing difference
Mapped Map Mode
Map key -> object-mapping filtering and conversion -> database column

Freedom Map Mode
Map key -> treated as column name -> database column

Insert Maps

Mapped Map insert
Map<String, Object> data = new HashMap<>();
data.put("id", 1001);
data.put("loginName", "alice");
data.put("email", "alice@example.com");

int rows = lambda.insert(User.class)
.asMap()
.applyMap(data)
.executeSumResult();
Freedom Map insert
Map<String, Object> data = new HashMap<>();
data.put("id", 1001);
data.put("login_name", "alice");
data.put("email", "alice@example.com");

int rows = lambda.insertFreedom("users")
.applyMap(data)
.executeSumResult();

Mapped Map Mode filters and converts fields through object mapping; Freedom Map Mode generates SQL from caller-provided column names.

Update Maps

In mapped mode, updateToSample updates non-null, updatable fields. Freedom mode processes supplied keys including null values; filter the Map yourself to skip nulls.

Sample-style update
Map<String, Object> sample = new HashMap<>();
sample.put("loginName", "alice_new");
sample.put("email", null);

int rows = lambda.update(User.class)
.asMap()
.eq("id", 1001)
.updateToSample(sample)
.doUpdate();

In mapped mode, updateRow treats missing updatable properties as null; Freedom mode processes only supplied keys. Without primary-key metadata, Freedom mode requires explicit allowUpdateKey() before updateRow. Ensure your Map excludes keys that must not change.

Row-style update
Map<String, Object> row = new HashMap<>();
row.put("login_name", "alice_new");
row.put("email", "alice_new@example.com");

int rows = lambda.updateFreedom("users")
.eq("id", 1001)
.allowUpdateKey()
.updateRow(row)
.doUpdate();

Choose An Update Method

MethodSemanticsCommon Use
updateToSample(map)Sample-style update; skips fields that do not participatePartial update from submitted form data
updateRow(map)Row-data update; generates update fields from row dataSync a whole row or overwrite row values
updateTo("field", value)Explicitly update one fieldSmall field set or conditional update

If there is nothing to update, there nothing to update. is raised. UPDATE without WHERE is blocked unless allowEmptyWhere() is explicit.

Delete Maps

Map delete does not require row data. It only needs delete conditions.

Mapped Map delete
int rows = lambda.delete(User.class)
.asMap()
.eq("id", 1001)
.doDelete();
Freedom Map delete
int rows = lambda.deleteFreedom("users")
.eq("id", 1001)
.doDelete();

DELETE without WHERE is blocked unless allowEmptyWhere() is explicit.

Batch Insert

Batch insert fits import and sync tasks.

Batch insert
List<Map<String, Object>> rows = Arrays.asList(row1, row2, row3);

int total = lambda.insert(User.class)
.asMap()
.applyMap(rows)
.executeSumResult();

The field set in batch data should stay consistent. Mapped Map Mode filters fields through object mapping; Freedom Map Mode generates columns from Map keys.

Further Reading