publicclassMenuQueryextendsPageQuery { @NestedQueries({ @NestedQuery(select ="parent_id", from ="menu") })privateboolean onlyParent;}
编写一个单元测试看下效果:
@TestpublicvoidtestNestedQuery() {MenuQuery menuQuery =MenuQuery.builder().onlyParent(true).build();ArrayList<Object> argList =newArrayList<>();String sql =BuildHelper.buildWhere(menuQuery, argList);String expected =" WHERE id IN (SELECT parent_id FROM menu)";assertEquals(expected, sql);assertTrue(argList.isEmpty());}
测试通过。
在MenuQuery中,@NestedQueries注解中的column和op字段的默认值合起来为"id IN",@NestedQuery(select = "parent_id", from = "menu")括号内的内容去掉符号后就是:(select parent_id from menu),两条语句合起来再将关键字修改为大写,即为:id IN (SELECT parent_id FROM menu)。