mybatisplus使用getById
时间: 2023-09-23 19:07:48 浏览: 256
MyBatis Plus is a powerful library that enhances the functionality of MyBatis, a Java-based persistence framework. To retrieve a record by its primary key using MyBatis Plus, you can use the `getById` method provided by the `BaseMapper` interface.
Here's an example of how to use the `getById` method:
```java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
public class YourMapperClass {
@Autowired
private BaseMapper<YourEntityClass> baseMapper;
public YourEntityClass getEntityById(Long id) {
return baseMapper.getById(id);
}
}
```
In the above example, `YourMapperClass` is the class where you define your mapper methods. `YourEntityClass` represents the entity class for which you want to retrieve a record. You can autowire the `BaseMapper` interface using the `@Autowired` annotation.
Then, you can use the `getById` method of `BaseMapper` by passing the primary key value to retrieve the corresponding record. The method will return an instance of `YourEntityClass` if found, or `null` if no matching record is found.
Remember to replace `YourMapperClass` and `YourEntityClass` with your own mapper and entity classes accordingly.
阅读全文