Grails将自定义查询检索到Mapped List [英] Grails retrieve custom query to Mapped List

查看:85
本文介绍了Grails将自定义查询检索到Mapped List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想为我的jquery自动完成创建控制器

  def arrSong = Song.executeQuery(select id,title as value,concat(artist,' - ',title)as $ {Song.name} '%',::paramTitle,'%'),[paramTitle:params.term?.toString()])
渲染arrSong为JSON

用这段代码我得到这个JSON:

$ $ $ $ $ $ $ $ $ $ $ [$ Brightside先生,凶手 - Brightside先生]]

我的期望是:

  [{id:1,value:Brightside先生,label:The Killers  -  Mr 。Brightside}] 

任何人都可以帮忙吗?

解决方案

executeQuery 您写入的方式会返回一个未命名结果的列表,必须使用该索引来检索



例如:

  [[1,Brightside先生  The Killers  -  Mr. Brightside]] 

arrSong.each {println it [0]}
//打印1,同样的[1] Brightside等等

您需要的是 map 代表您的结果集如下,那么您应该可以毫不费力地将地图呈现为JSON。

  def查询=
选择新的地图(id为id,
title为值,
concat(艺术家,' - ',title)作为标签)
from $ {Song .name}
其中title为concat('%',:paramTitle,'%')


def arrSong = Song.executeQuery(query,[paramTitle: params.term?.toString()])

渲染arrSong为JSON

应返回

  [{id:1,value: Brightside,label:The Killers  -  Mr. Brightside}] 


i'm new in grails...

I want to create a Controller for my jquery autocomplete

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

with this code i get this JSON :

[[1,"Mr. Brightside","The Killers - Mr. Brightside"]]

my expectation is like :

[{"id":1,"value":"Mr. Brightside","label":"The Killers - Mr. Brightside"}]

anyone can help ?

解决方案

executeQuery the way you have written would return a list of unnamed results which has to be retrieved by using the index.

For example:

[[1,"Mr. Brightside","The Killers - Mr. Brightside"]]

arrSong.each{println it[0]} 
//Prints 1, similarly it[1] for "Mr. Brightside" and so on

What you need is a map representation of your result set as below, then you should be able to render the map effortlessly as a 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

should return

[{"id":1,"value":"Mr. Brightside","label":"The Killers - Mr. Brightside"}]

这篇关于Grails将自定义查询检索到Mapped List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆