Grails 2 - 如何动态调用多个数据源 [英] Grails 2 - How to dynamically call multiple datasources
问题描述
$ p $
dataSource_a {
// ...
}
dataSource_b {
// ...
}
我希望能够根据某种参数动态更改正在访问的数据源。我可以做这样的事情......
def findPeople(datasource){
if(datasource =='a'){
return Person.a.list()
} else if(datasource =='b'){
return Person.b.list()
}
}
我真的希望能够做到,虽然,是这样的...
$ pre $ def $ find
$ b $ Person $$ datasource .list()
}
不幸的是,当我尝试做这件事时, 。 致命错误发生了应用查询转换:null 1错误。
有关如何完成此操作的任何想法?或者我只是坚持使用if / switch块?
我想通了,这就是你必须这样做的。
pre $
def findPeople(datasource){
def p = People.class
p。$ {datasource }。list()
}
由于某种原因,如果你这样称呼它,它的工作原理。
I have two named data sources in my Grails app (Grails 2.0.3)...
dataSource_a {
// ...
}
dataSource_b {
// ...
}
I'd like the ability to dynamically change what datasource I'm accessing, based on some kind of parameter. I could do something like this...
def findPeople(datasource) {
if (datasource == 'a') {
return Person.a.list()
} else if (datasource == 'b') {
return Person.b.list()
}
}
What I was really hoping to be able to do, though, is something like this...
def findPeople(datasource) {
return Person."$datasource".list()
}
Unfortunately, I get an error when I try and do that. "Fatal error occurred apply query transformations: null 1 error".
Any thoughts on how to accomplish this? Or am I just stuck with if/switch blocks?
I figured it out, this is how you have to do it.
def findPeople(datasource) {
def p = People.class
p."${datasource}".list()
}
For some reason, if you call it like that, it works.
这篇关于Grails 2 - 如何动态调用多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!