个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaEE
操作系统
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
public class User {
// @TableId(type = IdType.ASSIGN_ID)
private Long id;
@TableField(value = "username")
private String userName;
@TableField(value = "password",select = false)
private String passWord;
private String gender;
private String addr;
@TableField(exist = false)
private Integer online;
//逻辑删除字段,标记当前记录是否被删除
//1表示删除,0表示未删除
@TableLogic(value = "0",delval = "1")
private Integer deleted;
}
mybatis-plus:
global-config:
banner: false
db-config:
logic-delete-field: deleted
logic-delete-value: 1
logic-not-delete-value: 0
执行SQL语句:UPDATE tb_user SET deleted=1 WHERE id=? AND deleted=0
执行结果:
public class User {
// @TableId(type = IdType.ASSIGN_ID)
private Long id;
@TableField(value = "username")
private String userName;
@TableField(value = "password",select = false)
private String passWord;
private String gender;
private String addr;
@TableField(exist = false)
private Integer online;
//逻辑删除字段,标记当前记录是否被删除
//1表示删除,0表示未删除
@TableLogic(value = "0",delval = "1")
private Integer deleted;
//实现乐观锁
@Version
private Integer version;
}
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mpInterceptor() {
//1.定义Mp拦截器
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
//2.添加乐观锁的拦截器
mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return mpInterceptor;
}
}
void testUpdate() {
//1.先通过要修改的数据id将当前的数据查询出来
User user = userDao.selectById(4L);
//2.将要修改的属性逐一设置进去
user.setUserName("小小");
userDao.updateById(user);
}
SELECT id,username,gender,addr,deleted,version FROM tb_user WHERE id=? AND deleted=0
UPDATE tb_user SET username=?, gender=?, addr=?, version=? WHERE id=? AND version=? AND deleted=0