Java Grails将自定义查询检索到映射列表
我是grails的新手 我想为jquery自动完成创建一个控制器Java Grails将自定义查询检索到映射列表,java,grails,Java,Grails,我是grails的新手 我想为jquery自动完成创建一个控制器 def arrSong = Song.executeQuery("select id, title as value, concat(artist, ' - ', title) as label from ${Song.name} where title like concat('%', :paramTitle, '%')", [paramTitle:params.term?.toString()]) render arrSong
def arrSong = Song.executeQuery("select id, title as value, concat(artist, ' - ', title) as label from ${Song.name} where title like concat('%', :paramTitle, '%')", [paramTitle:params.term?.toString()])
render arrSong as JSON
通过这段代码,我得到了以下JSON:
[[1,"Mr. Brightside","The Killers - Mr. Brightside"]]
我的期望是:
[{"id":1,"value":"Mr. Brightside","label":"The Killers - Mr. Brightside"}]
任何人都可以提供帮助?
executeQuery
您编写的方式将返回一个未命名结果列表,必须使用索引检索该列表
例如:
[[1,"Mr. Brightside","The Killers - Mr. Brightside"]]
arrSong.each{println it[0]}
//Prints 1, similarly it[1] for "Mr. Brightside" and so on
您需要的是如下所示的结果集的映射
表示,然后您应该能够轻松地将映射呈现为JSON
def query = """
select new map(id as id,
title as value,
concat(artist, ' - ', title) as label)
from ${Song.name}
where title like concat('%', :paramTitle, '%')
"""
def arrSong = Song.executeQuery(query, [paramTitle: params.term?.toString()])
render arrSong as JSON
应该回来
[{"id":1,"value":"Mr. Brightside","label":"The Killers - Mr. Brightside"}]
查看AjaxDependencySelection插件。它提供gsp的自动完成功能