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
| Operation | Mapped Map Mode | Freedom Map Mode |
|---|---|---|
| Insert | lambda.insert(User.class).asMap() | lambda.insertFreedom("users") |
| Update | lambda.update(User.class).asMap() | lambda.updateFreedom("users") |
| Delete | lambda.delete(User.class).asMap() | lambda.deleteFreedom("users") |
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
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();
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.
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.
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
| Method | Semantics | Common Use |
|---|---|---|
updateToSample(map) | Sample-style update; skips fields that do not participate | Partial update from submitted form data |
updateRow(map) | Row-data update; generates update fields from row data | Sync a whole row or overwrite row values |
updateTo("field", value) | Explicitly update one field | Small 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.
int rows = lambda.delete(User.class)
.asMap()
.eq("id", 1001)
.doDelete();
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.
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
- Mapped Map Mode, Map operations that reuse object mapping.
- Freedom Map Mode, Map operations without object mapping.
- LambdaTemplate Update, update semantics in Entity Mode.