在使用mybatisplus中,可使用逻辑删除。案例中,使用mybatisplus逆向生成model,使用delete_status为识别逻辑删除字段。

springboot 中配置启动逻辑删除

mybatis-plus:
global-config:
db-config:
id-type: auto
logic-delete-value:
logic-not-delete-value:

在生成的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: ms -- ::
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=

 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: ms -- ::
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中拼接上去。

05-04 03:43