mybatis-plus query data that has been logically deleted

Business scenario: In some cases, the data referenced by our query is "soft deleted", and the direct api of mybatis-plus cannot help me query it, but I don't want to write sql by hand, I just want to use the conditional constructor.

Scenario: If the tombstone field in our mysql table is is_delete, if we directly use the api of myabtis-plus, it will automatically spell is_delete=0 for us. This is a hard-coded fixed query condition. When we use the condition constructor, it We will always bring this fixed condition, so that we want to query the "soft deleted" data, we can only use handwritten sql.

In fact, the conditional constructor can also be queried, let’s see my show operation (not recommended), try to use xml as much as possible.

1. Problem description

When you don’t write anything, if you directly use the conditional constructor queryWrapper of mybatis-plu, the logically deleted fields will be automatically spelled out, as shown in the figure: In this case,
insert image description here
most of the cases meet my needs (otherwise I will not use logically deleted @TableLogic), but, in some cases, I just need to put it 逻辑删除的数据也查询出来, and there are several solutions below.

Two, the solution

2.1 Method 1: Through the conditional constructor (clever, not recommended)

(This way of writing is risky, you need to pay attention to the version of mybaits-plus-starter)

Test version: 3.1.0 can use this method
Test version: 3.3.1 cannot use this method


    @Override
    public TyqUser getUserById(String id) {
    
    
        LambdaQueryWrapper<TyqUser> queryWrapper = Wrappers.<TyqUser>lambdaQuery();
        // 写法1:
//        queryWrapper.eq(TyqUser::getId, id);
//        queryWrapper.or().eq(TyqUser::getId, id);
        // 写法2:
//        queryWrapper.apply("id = {0} or deleted = 1 and id = {0}", id);
        // 写法3:使用@Select("select * from user where id = #{}"),等同于写法4
        // 写法4:使用 xml ,将上方语句写到 <select> 标签中
        TyqUser user = baseMapper.selectOne(queryWrapper);
        return user;
    }

2.1.1 Writing method one run sql screenshot

insert image description here

2.1.2 Writing method 2 Run sql screenshot

insert image description here

2.2 Method 2: via xml (recommended)

    <select id="getByIdXml" resultType="com.lzq.learn.domain.TyqUser">
        select * from `user` where id = #{
    
    id}
    </select>

Three, pay attention!

The essence of the above-mentioned method 1 and method 2 is that the blogger finds out or will not later 自动拼接逻辑删除=true的字段, but when the version of mybatis-plus is 3.3.1, the "Little Smart" failure scene will appear.

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
<!--            <version>3.1.0</version>-->
        </dependency>

The sql result of writing method 1 is invalid and cannot be queried, because the splicing condition is enclosed by (), and the condition of deleted=0 will always be added!
insert image description here
The sql result of writing method 2 is invalid and cannot be queried, because the splicing condition is enclosed by (), and the condition of deleted=0 will always be added!
insert image description here

Note: Whether method 1 and method 2 are valid depends on the version of myabtis-plus! To be on the safe side, let’s write it directly in xml. You will pay a price for being smart and lazy. After the version changes, it will explode directly.

Guess you like

Origin blog.csdn.net/lzq2357639195/article/details/132183298