Grails 2 - 如何动态调用多个数据源 [英] Grails 2 - How to dynamically call multiple datasources

查看:23
本文介绍了Grails 2 - 如何动态调用多个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Grails 应用程序(Grails 2.0.3)中有两个命名数据源...

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()
}

不幸的是,当我尝试这样做时出现错误.发生致命错误应用查询转换:null 1 错误".

Unfortunately, I get an error when I try and do that. "Fatal error occurred apply query transformations: null 1 error".

关于如何实现这一点的任何想法?还是我只是被 if/switch 块困住了?

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屋!

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