Skip to main content
Hint

This article is generated by AI translation.

BaseMapper interface

BaseMapper is a generic mapper with built-in CRUD. Extend it to operate on MongoDB collections without writing SQL or Mongo commands.

Define mapper

Define an interface that extends BaseMapper<T> (T is your entity) and annotate it with @SimpleMapper.

定义 Mapper 接口
@SimpleMapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {
// Add custom mapper methods if needed
}

Use the mapper

Create the mapper from Session, then call the built-ins.

使用 Mapper 进行 CRUD
// Initialize Session (typically managed by the framework)
Session session = ...;

UserInfoMapper mapper = session.createMapper(UserInfoMapper.class);

// 1. Insert
UserInfo user = new UserInfo();
user.setUid(UUID.randomUUID().toString());
user.setName("mapper_user");
mapper.insert(user);

// 2. Query
UserInfo loadedUser = mapper.selectById(user.getUid());

// 3. Update
loadedUser.setName("updated_name");
mapper.update(loadedUser); // 根据主键更新非空字段

// 4. Delete
mapper.deleteById(user.getUid());

Supported methods

BaseMapper provides many methods, including:

  • insert(T entity): Insert one.
  • deleteById(Serializable id): Delete by primary key.
  • update(T entity): Update by primary key (usually non-null fields).
  • selectById(Serializable id): Query by primary key.
  • listBySample(T sample): Query by sample object.
tip

Ensure the entity has proper @Table / @Column configuration, especially the primary key.