Exist与In子查询
下面测试一下exist以及in,并且添加子查询的情况。exist会使用两种写法一种为直接写sql语句,另一种为使用lamdas表达式方式。
接口代码添加
/** * 测试exist * * @return list */ List<TestFluentMybatisEntity> query5(); /** * 测试exist lamdas * * @return list */ List<TestFluentMybatisEntity> query6(); /** * 测试in lamdas * * @return list */ List<TestFluentMybatisEntity> query7();
实现代码添加
@Override public List<TestFluentMybatisEntity> query5() { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .where .age() .ge(30) .and .exists("select 1 from test_fluent_mybatis where name=?", "李四") .end()); } @Override public List<TestFluentMybatisEntity> query6() { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .where .age() .ge(30) .and .exists(q -> q.select.name().end().where.name().eq("李四").end()) .end()); } @Override public List<TestFluentMybatisEntity> query7() { return testFluentMybatisMapper.listEntity( new TestFluentMybatisQuery() .where .id() .in( new TestFluentMybatisQuery() .select .apply("id") .end() .where .age() .between(10, 50) .end()) .end()); }
控制层代码添加
@ApiOperation(value = "where-exist语法", notes = "where-exist语法") @RequestMapping(value = "/exist", method = RequestMethod.GET) @ResponseBody public Result<List<TestFluentMybatisEntity>> query5() { try { return Result.ok(whereService.query5()); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } } @ApiOperation(value = "where-exist1语法", notes = "where-exist1语法") @RequestMapping(value = "/exist1", method = RequestMethod.GET) @ResponseBody public Result<List<TestFluentMybatisEntity>> query6() { try { return Result.ok(whereService.query6()); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } } @ApiOperation(value = "where-in-sub语法", notes = "where-in-sub语法") @RequestMapping(value = "/in-sub", method = RequestMethod.GET) @ResponseBody public Result<List<TestFluentMybatisEntity>> query7() { try { return Result.ok(whereService.query7()); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } }
验证效果
前两个接口返回的结果一致,没啥问题。