使用 Grails/GORM/DataServices @Query 注释分页

Pagination with Grails/GORM/DataServices @Query annotation

如何在通过 grails.gorm.services.Query 注释使用 GORM JPA-QL 查询时添加分页

@Service(User)
abstract class UserDataService implements IUserService {

    @Query($/SELECT DISTINCT new Map(user.id as id, user.username as username)
             FROM $User user
              join $UserRelationship ur on ur.user.id = user.id or ur.manager.id = user.id
              join ur.manager manager
           /$)
   abstract List<Map> searchAllUsers(String searchString, [offset: 0, max: 10])

}

您可以在 listWithQuery 和 listWithQuery2 等方法中传递 Map args,args 需要看起来像 [offset: (rows to offset) , max: (limit of rows to return)]

@Service(User)
interface IUserService {

    List<User> findAll()

    List<User> list(Map args)

    @Query("from ${User user} where ${user.lastName} like '%halp%'")
    List<User> listWithQuery(Map args)

    @Query("from ${User} user where user.lastName like $nameLike")
    List<User> listWithQuery2(String nameLike, Map args)
}