MyBatisPlus条件构造器Condition的用法

场景

项目搭建专栏:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37194

基础搭建:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89407994

条件构造器介绍使用:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89482201

实现

查看Condition的源码可知其继承自Wrapper方法,所以Wrapper的方法都可以使用。

它有一个获取实例的方法

/**
     * 获取实例
     */
    public static Condition create() {
        return new Condition();
    }

 

编写测试方法:

/***
  *条件构造器 Condition
  */
 @Test
 public void testConditionOrderBy() {
  
  List<Employee> employeeList=employeeMapper.selectList(Condition.create()
    .eq("gender",1)
    .like("name", "霸")
    .orderBy("age")
    .last("desc limit 1,2")
    //.orderDesc(Arrays.asList(new String[] {"age"}))
    //.orderAsc(Arrays.asList(new String[] {"age"}))
    );
  System.out.println("*******************"+employeeList);
  for (Employee employee : employeeList) {
   System.out.println(employee.getAge());
  }
 }

源码下载

https://download.csdn.net/download/badao_liumang_qizhi/11142337

Condition源码

/**
 * Copyright (c) 2011-2020, hubin (jobob@qq.com).
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 EntityWrapperS 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.mapper;

import com.baomidou.mybatisplus.toolkit.StringUtils;

/**
 * <p>
 * 条件查询构造器
 * </p>
 *
 * @author hubin Caratacus
 * @date 2016-11-7
 */
@SuppressWarnings({"rawtypes", "serial"})
public class Condition extends Wrapper {

    /**
     * 构建一个Empty条件构造 避免传递参数使用null
     */
    public static final Wrapper EMPTY = new Wrapper() {
        @Override
        public String getSqlSegment() {
            return null;
        }
    };

    /**
     * 获取实例
     */
    public static Condition create() {
        return new Condition();
    }

    /**
     * SQL 片段
     */
    @Override
    public String getSqlSegment() {
        if (SqlHelper.isEmptyOfWrapper(this)) {
            return null;
        }
        /*
         * 无条件
   */
        String sqlWhere = sql.toString();
        if (StringUtils.isEmpty(sqlWhere)) {
            return null;
        }
         /*
         * 根据当前实体判断是否需要将WHERE替换成 AND 增加实体不为空但所有属性为空的情况
   */
        return isWhere != null ? (isWhere ? sqlWhere : sqlWhere.replaceFirst("WHERE", AND_OR)) : sqlWhere.replaceFirst("WHERE", AND_OR);

    }

    /**
     * 构造一个空的Wrapper<T></>
     *
     * @param <T>
     * @return
     */
    public static <T> Wrapper<T> empty() {
        return (Wrapper<T>) EMPTY;
    }

    /**
     * 构造一个Wrapper<T></>
     *
     * @param <T>
     * @return
     */
    public static <T> EntityWrapper<T> wrapper() {
        return new EntityWrapper<>();
    }

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis-Plus 的条件构造可以让我们在进行数据库操作时,更加方便快捷地构建 SQL 语句,具有以下特点: - 支持链式调用 - 友好的 API - 安全的 SQL 拼接 - 支持 lambda 表达式 - 支持自定义 SQL 片段 下面是 MyBatis-Plus 条件构造的使用方法: 1. 导入 MyBatis-Plus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> ``` 2. 创建 QueryWrapper 对象: ```java QueryWrapper<User> queryWrapper = new QueryWrapper<>(); ``` 3. 添加查询条件: ```java // eq 等于 queryWrapper.eq("name", "张三"); // ne 不等于 queryWrapper.ne("age", 18); // gt 大于 queryWrapper.gt("age", 20); // ge 大于等于 queryWrapper.ge("age", 18); // lt 小于 queryWrapper.lt("age", 30); // le 小于等于 queryWrapper.le("age", 30); // between 区间查询 queryWrapper.between("age", 18, 30); // like 模糊查询 queryWrapper.like("name", "张"); // or 或者 queryWrapper.or(wrapper -> wrapper.eq("age", 18).ne("name", "张三")); // in 包含 queryWrapper.in("age", Arrays.asList(18, 20, 22)); // notIn 不包含 queryWrapper.notIn("age", Arrays.asList(24, 26, 28)); // isNull 空 queryWrapper.isNull("email"); // isNotNull 非空 queryWrapper.isNotNull("email"); // orderBy 排序 queryWrapper.orderByDesc("age"); // last 拼接 SQL 片段 queryWrapper.last("limit 10"); ``` 4. 执行查询: ```java List<User> userList = userMapper.selectList(queryWrapper); ``` 以上就是 MyBatis-Plus 条件构造的使用方法,更多的查询条件可以参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值