Skip to main content
Hint

This article is generated by AI translation.

BaseMapper Interface

BaseMapper is a common Mapper interface provided by dbVisitor, which has built-in common CRUD methods. By inheriting this interface, you can directly obtain basic operation capabilities for ElasticSearch indices without writing any SQL or Elastic commands.

Define Mapper

Define an interface inheriting BaseMapper<T>, where T is your entity class. And mark the interface with @SimpleMapper annotation.

Define Mapper Interface
@SimpleMapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {
// You can add custom Mapper methods here
}

Use Mapper

Create a Mapper instance via Session, then directly call built-in methods.

Use Mapper for CRUD
// Initialize Session (usually managed by 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); // Update non-null fields by primary key

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

Supported Methods

BaseMapper provides rich methods, including but not limited to:

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

When using BaseMapper, please ensure your entity class is correctly configured with @Table and @Column annotations, especially the primary key configuration.