FluentMybatis Where语法(一) | FluentMybatis实践(1)

简介: FluentMybatis Where语法(一) | FluentMybatis实践

前言

这几天在出差,没时间更新FluentMybatis相关的内容。接着上一篇:FluentMybatis 聚合查询、apply方法 | FluentMybatis实践_阿良的博客-CSDN博客


继续测试fm的其他语法,where语法的使用。


GitHub仓库:github项目地址



数据准备

随便弄几条数据。


image.png


Where基本条件语法

先看看where的基本语法,例如:> , = , < , >= , <= , <> , between , not between , in , not in , is null , is not null等等。随便选几个测试一下。


接口代码


package com.hy.fmp.service;
import com.hy.fmp.fluent.entity.TestFluentMybatisEntity;
import java.util.List;
/** @Author huyi @Date 2021/11/8 19:48 @Description: where语法 */
public interface IWhereService {
  /**
   * 测试ge
   *
   * @return list
   */
  List<TestFluentMybatisEntity> query1();
  /**
   * 测试in
   *
   * @return list
   */
  List<TestFluentMybatisEntity> query2();
  /**
   * 测试between
   *
   * @return list
   */
  List<TestFluentMybatisEntity> query3();
  /**
   * 测试is null
   *
   * @return list
   */
  List<TestFluentMybatisEntity> query4();
}


实现代码

package com.hy.fmp.service.Impl;
import com.hy.fmp.fluent.entity.TestFluentMybatisEntity;
import com.hy.fmp.fluent.mapper.TestFluentMybatisMapper;
import com.hy.fmp.fluent.wrapper.TestFluentMybatisQuery;
import com.hy.fmp.service.IWhereService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/** @Author huyi @Date 2021/11/8 20:10 @Description: where语法接口实现类 */
@Slf4j
@Service
public class IWhereServiceImpl implements IWhereService {
  @Autowired private TestFluentMybatisMapper testFluentMybatisMapper;
  @Override
  public List<TestFluentMybatisEntity> query1() {
    return testFluentMybatisMapper.listEntity(
        new TestFluentMybatisQuery().where.age().ge(30).end());
  }
  @Override
  public List<TestFluentMybatisEntity> query2() {
    return testFluentMybatisMapper.listEntity(
        new TestFluentMybatisQuery().where.age().in(new int[] {30, 78, 64}).end());
  }
  @Override
  public List<TestFluentMybatisEntity> query3() {
    return testFluentMybatisMapper.listEntity(
        new TestFluentMybatisQuery().where.age().between(10, 50).end());
  }
  @Override
  public List<TestFluentMybatisEntity> query4() {
    return testFluentMybatisMapper.listEntity(
        new TestFluentMybatisQuery().where.age().isNull().end());
  }
}

控制层代码

package com.hy.fmp.ctrl;
import com.hy.fmp.dto.Result;
import com.hy.fmp.enm.ErrorCode;
import com.hy.fmp.fluent.entity.TestFluentMybatisEntity;
import com.hy.fmp.service.IWhereService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/** @Author huyi @Date 2021/11/8 20:15 @Description: where语法控制层 */
@Slf4j
@RestController
@RequestMapping("/where")
@Api(tags = "where语法")
public class WhereController {
  @Autowired private IWhereService whereService;
  @ApiOperation(value = "where-ge语法", notes = "where-ge语法")
  @RequestMapping(value = "/ge", method = RequestMethod.GET)
  @ResponseBody
  public Result<List<TestFluentMybatisEntity>> query1() {
    try {
      return Result.ok(whereService.query1());
    } catch (Exception exception) {
      return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
    }
  }
  @ApiOperation(value = "where-in语法", notes = "where-in语法")
  @RequestMapping(value = "/in", method = RequestMethod.GET)
  @ResponseBody
  public Result<List<TestFluentMybatisEntity>> query2() {
    try {
      return Result.ok(whereService.query2());
    } catch (Exception exception) {
      return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
    }
  }
  @ApiOperation(value = "where-between语法", notes = "where-between语法")
  @RequestMapping(value = "/between", method = RequestMethod.GET)
  @ResponseBody
  public Result<List<TestFluentMybatisEntity>> query3() {
    try {
      return Result.ok(whereService.query3());
    } catch (Exception exception) {
      return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
    }
  }
  @ApiOperation(value = "where-isNull语法", notes = "where-isNull语法")
  @RequestMapping(value = "/isNull", method = RequestMethod.GET)
  @ResponseBody
  public Result<List<TestFluentMybatisEntity>> query4() {
    try {
      return Result.ok(whereService.query4());
    } catch (Exception exception) {
      return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);
    }
  }
}

验证一下


image.png



image.png

没什么问题。

相关文章
|
5月前
|
SQL Java 数据库连接
JAVAEE框架技术之9-myBatis高级查询技术文档
JAVAEE框架技术之9-myBatis高级查询技术文档
113 0
JAVAEE框架技术之9-myBatis高级查询技术文档
|
11月前
|
SQL
FluentMybatis 聚合查询、apply方法 | FluentMybatis实践(2)
FluentMybatis 聚合查询、apply方法 | FluentMybatis实践(2)
|
XML Java 数据库连接
深入浅出:MyBatis的使用方法及最佳实践
深入浅出:MyBatis的使用方法及最佳实践
【测试开发】知识点-mybatis,主要使用方式:接口式编程
【测试开发】知识点-mybatis,主要使用方式:接口式编程
【测试开发】知识点-mybatis,主要使用方式:接口式编程
FluentMybatis Update语法 | FluentMybatis实践
FluentMybatis Update语法 | FluentMybatis实践
FluentMybatis Update语法 | FluentMybatis实践
FluentMybatis Where语法(一) | FluentMybatis实践(3)
FluentMybatis Where语法(一) | FluentMybatis实践
FluentMybatis Where语法(一) | FluentMybatis实践(3)
FluentMybatis Where语法(一) | FluentMybatis实践(2)
FluentMybatis Where语法(一) | FluentMybatis实践
FluentMybatis Where语法(一) | FluentMybatis实践(2)
|
SQL Java Maven
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(1)
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(1)
|
SQL Java 关系型数据库
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(2)
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践
FluentMybatis 项目构建、代码生成(一) | FluentMybatis实践(2)