多数据源使用

版本:3.0.9
第一步:
build.gradle: 格式不能变(空格要求很是严格)

dataSources:
    dataSource:
        pooled: true
        jmxExport: true
        driverClassName: org.postgresql.Driver
        username: postgres
        password: 111111
    secondDataSource:     #数据源首字母可以大写
        driverClassName: org.postgresql.Driver
        username: sa
        password: 123456
    thirdDataSource:
        driverClassName: org.postgresql.Driver
        username: root
        password: 123456

environments:
    development:
        dataSources:
            dataSource:
                dbCreate: create-drop
                url: jdbc:postgresql://127.0.0.1:5432/multiple_dataSources_test1
            secondDataSource:
                dbCreate: create-drop
                url: jdbc:postgresql://127.0.0.1:5432/multiple_dataSources_test2
            thirdDataSource:
                dbCreate: create-drop
                url: jdbc:postgresql://127.0.0.1:5432/multiple_dataSources_test2

第二步:domain类:

static mapping = {
    //     datasource "secondDataSource"       //domain只在指定的DB中生成             
    //     datasource "ALL"
           datasources(['dataSource','secondDataSource'])    
    //     datasources(['DEFAULT','secondDataSource'])
    }

BootStrap、Service中的应用:
1、 对象.数据源名字.save()
    entity.thirdDataSource.save() 注意:数据源首字母必须小写
2、类名.数据源名字.方法
    Partner.thirdDataSource.createCriteria().get{}
    Partner.thirdDataSource.findByName(‘xx’)

控制器、服务中: (未测试)

import groovy.sql.Sql
class TestController {
    def dataSource_lookup
    def index() {}
    def testname(){
        def sql=new Sql(dataSource_lookup)
        def a=sql.rows("select * from T_BASE where sf_id='650203199109142128'")
    }
}

参考资料:
1、官方文档:
2、http://blog.csdn.net/karamay2012/article/details/46592625

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值