Querydsl使用

来源:互联网 发布:如何管理淘宝客服 编辑:程序博客网 时间:2024/05/21 14:02

Querydsl是一个Java开源框架用于构建类型安全的SQL查询语句。它采用API代替拼凑字符串来构造查询语句。可跟 Hibernate 和 JPA 等框架结合使用。

基本查询:

?
1
2
3
4
5
6
JPAQuery query = new JPAQuery(entityManager);
List<Person> persons = query.from(person)
  .where(
    person.firstName.eq("John"),
    person.lastName.eq("Doe"))
  .list(person);

子查询:

?
1
2
3
4
5
List<Person> persons = query.from(person)
  .where(person.children.size().eq(
    new JPASubQuery().from(parent)
        .uniqueResult(parent.children.size().max())
  )).list(person);

排序:

?
1
2
3
4
List<Person> persons = query.from(person)
  .orderBy(person.lastName.asc(), 
           person.firstName.desc())
  .list(person);
0 0
原创粉丝点击