1、默认的查询逻辑

我们在使用mybatis-plus条件构造器默认查询的时候

如果给的实体有值,则会根据实体内的值用对应字段去=查询

就像这样

mpUserService.list(Wrappers.lambdaQuery(UserPO.builder().username("hino").build()));
mpUserMapper.selectList(Wrappers.lambdaQuery(UserPO.builder().username("ruben").build()));

2、使用like

如果我们想要指定默认查询为LIKE

则可以在对应属性上加上注解@TableField并指定condition = SqlCondition.LIKE,就像这样


@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class UserPO{

    @TableField(condition = SqlCondition.LIKE)
    private String username;

}

指定后我们的查询就变成了LIKE

1-切换like查询.png

3、多种方式

  • SqlCondition是一个枚举类,可以使用多种方式
/*
 * Copyright (c) 2011-2021, baomidou (jobob@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.baomidou.mybatisplus.annotation;

/**
 * SQL 比较条件常量定义类
 *
 * @author hubin
 * @since 2018-01-05
 */
public class SqlCondition {
    /**
     * 等于
     */
    public static final String EQUAL = "%s=#{%s}";
    /**
     * 不等于
     */
    public static final String NOT_EQUAL = "%s<>#{%s}";
    /**
     * % 两边 %
     */
    public static final String LIKE = "%s LIKE CONCAT('%%',#{%s},'%%')";
    /**
     * % 左
     */
    public static final String LIKE_LEFT = "%s LIKE CONCAT('%%',#{%s})";
    /**
     * 右 %
     */
    public static final String LIKE_RIGHT = "%s LIKE CONCAT(#{%s},'%%')";
}