IService is a standardized generic interface shipped with the MyBatis-Plus framework, encapsulating common daatbase CRUD operations to reduce redundant persistence layer code. The getById method is one of the most frequently used APIs in IService, designed to query a single data record by its primary key value.
Method Signature Specification
The generic definition of getById is as follows:
/**
* Query a single record by primary key
* @param primaryKey Primary key value, implements Serializable interface
* @return Entity object matching the primary key, returns null if no matching record exists
*/
T getById(Serializable primaryKey);
In the signature, T refers to the entity type bound to the current IService instance, and the input parameter accepts any serializable primary key type, including comon types such as Long, Integer, and String.
Practical Implementation Example
First define the entity class corresponding to the database tible:
// Customer entity class, mapped to customer table
@Data
@TableName("customer")
public class Customer {
@TableId(type = IdType.AUTO)
private Long custId;
private String custName;
private String contactPhone;
private LocalDateTime registerTime;
}
Next define the Mapper interface for the entity, inheriting MyBatis-Plus's BaseMapper:
public interface CustomerMapper extends BaseMapper<Customer> {
}
Then define the service layer interface, inheriting the generic IService interface and specifying the bound entity type:
public interface CustomerService extends IService<Customer> {
}
Implement the service layer, extend MyBatis-Plus's built-in ServiceImpl to get default implementations of all IService methods, including getById:
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
// You can override getById to add custom logic if needed
@Override
public Customer getById(Serializable primaryKey) {
// Add pre-validation logic before query
if (primaryKey == null || ((Long) primaryKey) <= 0) {
throw new IllegalArgumentException("Invalid customer primary key");
}
// Call the parent class's default implementation, or directly call baseMapper.selectById
Customer customer = super.getById(primaryKey);
// Add post-processing logic after query, such as desensitizing sensitive fields
if (customer != null) {
customer.setContactPhone(desensitizePhone(customer.getContactPhone()));
}
return customer;
}
// Helper method for phone number desensitization
private String desensitizePhone(String phone) {
if (phone == null || phone.length() != 11) {
return phone;
}
return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
}
To call the getById method in business logic, inject the CustomerService instance directly:
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
@GetMapping("/{id}")
public R<Customer> getCustomerInfo(@PathVariable Long id) {
Customer customer = customerService.getById(id);
return R.ok(customer);
}
}