mybatis-plus实例化一个实现类时同时填充主键id
时间: 2024-01-27 20:30:38 浏览: 106
在MyBatis-Plus中,可以使用`@TableId`注解来标记实体类的主键字段,并且MyBatis-Plus会自动生成主键的值。下面是一个示例:
首先,在实体类中使用`@TableId`注解标记主键字段,同时指定主键策略为`IdType.AUTO`,表示自动增长主键:
```java
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
public class ImplementClass {
@TableId(type = IdType.AUTO)
private Long id;
// other properties and constructors
}
```
然后,在进行实例化时,可以直接创建对象并赋值其他属性,主键`id`会自动生成:
```java
ImplementClass instance = new ImplementClass();
instance.setName(serviceName); // 设置其他属性的值
// id 属性会在插入数据库时自动生成
```
在上述示例中,通过使用`@TableId`注解标记了`id`属性,并指定了主键策略为自动增长。在实例化`ImplementClass`对象时,直接设置其他属性的值,主键`id`会在插入数据库时自动生成。
请注意,上述示例假设你已经正确配置了MyBatis-Plus框架和数据库连接。具体的配置和使用方法可以参考MyBatis-Plus官方文档。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)