版本: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