grails - 使用列索引而不是列名对Grails域列表进行排序

标签 grails gorm

我使用类似以下的代码通过域检索数据库中的行:
def DomainList = DomainClass.list(sort: orderBy, order: "asc")
假设orderBy变量是String,它是域的column之一的名称。但是,如果我想按列索引对它排序,该怎么办:

SELECT * FROM `table` ORDER BY 2 DESC

我该如何在Grails上做到这一点?

最佳答案

一段时间后,我放弃寻找动态解决方案。相反,我在代码的开头使用了if-else陷阱,因为每个索引都对应于特定的列:

def orderBy = params?.orderBy
def orderByPolicy = params?.orderByPolicy

if(orderBy.equals("0")) {
    orderBy = "name_of_index_0_column"
}
else if(orderBy.equals("1")) {
    orderBy = "name_of_index_1_column"
}
else if(orderBy.equals("2")) {
    orderBy = "name_of_index_2_column"
}
else if(orderBy.equals("3")) {
    orderBy = "name_of_index_3_column"
}

if(!orderByPolicy?.equalsIgnoreCase("desc")) {
    orderByPolicy = "asc"
}

def DomainList = DomainClass.list(sort: orderBy, order: orderByPolicy)

请注意,这不是动态的。它不能容纳在DomainClass上添加新列;您还必须手动编辑if-else陷阱。

关于grails - 使用列索引而不是列名对Grails域列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30726999/

相关文章:

java - Spring LDAP 示例

session - Grails session 范围的服务 - 不工作

hibernate - Grails 3.2.1 WAR无法以缺少org.hibernate.boot.registry.selector.StrategyRegistrationProvider开头

grails - 在gorm数据模型中初始化数据

grails - GORM复合映射不起作用

grails - 命名查询中的Grails GORM计数功能

hibernate - 在MySQL数据库中手动创建行而不是通过Hibernate这样做是否安全?

javascript - 使用 javascript 动态聚焦于某个元素

performance - Grails审核日志记录插件-性能

mysql - 在数据库中插入乌尔都语文本,但只在数据库中插入 "??????"。