@TableLogic 这个注解怎样用
时间: 2023-07-04 10:10:34 浏览: 73
@TableLogic 是 MyBatis-Plus 框架提供的注解,用于标记数据库表中的逻辑删除字段。当我们在数据库表中添加一个名为 deleted 的字段,并使用该注解标记该字段时,MyBatis-Plus 将自动为我们生成逻辑删除的 SQL 语句。
使用步骤如下:
1. 在数据库表中添加一个名为 deleted 的字段,字段类型为 int 或者 boolean。
2. 在实体类中添加 deleted 字段对应的属性,并使用 @TableLogic 注解标记该属性。
示例代码:
```java
@Data
public class User {
private Long id;
private String name;
private Integer age;
@TableLogic
private Integer deleted;
}
```
3. 在 MyBatis-Plus 配置类中开启逻辑删除功能。
示例代码:
```java
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
// 开启逻辑删除
interceptor.addInnerInterceptor(new MybatisPlusSqlInjector());
return interceptor;
}
}
```
这样,当我们使用 MyBatis-Plus 提供的删除、更新方法时,就会自动转换为逻辑删除或更新操作。