简单查询

当查询对象中所有的查询条件匹配方式均为“相等”,并且字段名和实体中的字段名相同时,只需要在作为查询条件的字段上增加@QueryCondition即可。

class Foo {
    Long id;
    String name;
    String content;
}
class FooQo {
    @QueryCondition
    Long id;
    @QueryCondition
    String name;
    String content; // 不加注解,不作为查询条件
}

FooQo fooQo = new FooQo(1L, "foo", "content");
Specification<Foo> specification = SpecificationFactory.getSpecification(fooQo);
List<Foo> foos = fooRepository.findAll(specification);

生成的SQL:

SELECT DISTINCT *
FROM FOO
WHERE FOO.ID = 1
	AND LOWER(FOO.NAME) = 'foo';

字符串条件默认会忽略大小写

如果查询对象中的所有字段均作为查询条件,可以在查询对象上声明@QueryObject注解,相当于在所有字段上声明@QueryCondition注解。

class Foo {
    Long id;
    String name;
    String content;
}
@QueryObject
class FooQo {
    Long id;
    String name;
    String content;
}

FooQo fooQo = new FooQo(1L, "foo", "content");
Specification<Foo> specification = SpecificationFactory.getSpecification(fooQo);
List<Foo> foos = fooRepository.findAll(specification);

生成的SQL:

SELECT * FROM FOO
WHERE FOO.ID = 1
	AND LOWER(FOO.NAME) = 'foo'
	AND LOWER(FOO.CONTENT) = 'content';

Last updated