mybatis配置的逻辑删除不好使了
在使用mybatisplus中,可使用逻辑删除。案例中,使用mybatisplus逆向生成model,使用delete_status为识别逻辑删除字段。
springboot 中配置启动逻辑删除
mybatis-plus: global-config: db-config: id-type: auto logic-delete-value: 1 logic-not-delete-value: 0
在生成的model实体类中配置逻辑删除字段注解 @TableLogic
public class User extends Model<User> { private static final long serialVersionUID=1L; /** * 主键id */ @TableId(value = "id", type = IdType.AUTO) private Integer id; /** * 逻辑删除值,1删除,0未删除 */ @TableLogic private Boolean deleteStatus; ... }
1.使用mybatisplus的api查询
@Test public void testDeleteStatus() { List<User> list = userService.list(new QueryWrapper<User>()); System.out.println(list); }
在控制台打印的sql自动拼接delete_status = 0
Consume Time:24 ms 2020-03-27 19:20:39 Execute SQL:SELECT id,user_name,true_name,avatar,indentity_crad,email,mobile,wechat_openid,unionid,password,gender,
last_login_ip,last_login_time,login_times,user_from,reg_ip,delete_status,create_time,update_time FROM user WHERE delete_status=0
2.自己自定义一个xml文件,编写一个查询
<mapper namespace="com.finance.admin.mapper.UserMapper"> <select id="testDelete" resultType="com.finance.admin.vo.UserDetailsVO"> select u.user_name as userName, u.mobile as mobile, u.create_time as createTime, u.last_login_time as last_loginTime from `user` order by create_time desc </select> </mapper>
添加mapper执行结果:
Consume Time:14 ms 2020-04-21 16:33:24 Execute SQL:select u.user_name as userName, u.mobile as mobile, u.create_time as createTime, u.last_login_time as last_loginTime
from `user` u order by create_time desc
结果来看,使用自定义的sql为拼接逻辑删除字段。需要我们自己sql中拼接上去。