我们在Grails中创建了两个不同的域对象,并尝试从两个不同的架构进行访问。
方法1:
例如:
Student.groovy
class Students {
String id
String name
String address
Static mapping = {
schema: 'student_details'
}
}
Customer.groovy
class Customer {
String firstName
String lastName
String address
Static mapping = {
schema: 'customer_details'
}
}
application.yml
environments:
development:
dataSource:
dbCreate: update
url: jdbc:mysql://localhost:3306/
如果我在url连接字符串中提供默认架构,它总是引用该默认值,而不管域类中定义的架构是什么,抛出异常,找不到表。如果我从url连接字符串中删除默认架构,我会在日志中找到"未选择数据库" 错误。
方法2:
我们尝试在 application.yml 中配置具有多个数据源的每个架构选项,如下所示:
dataSource:
pooled: true
dbCreate: update
url: jdbc:mysql://localhost:3306/sample_grails
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: root
password: ''
secondary:
pooled: true
dbCreate: update
url: jdbc:mysql://localhost:3306/grails_mapping
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: root
password: ''
将域类用作 Customer.groovy
class Customer {
String firstName
String lastName
String address
Static mapping = {
datasource 'secondary'
}
}
我收到错误
Caused by: org.grails.datastore.mapping.core.exceptions.ConfigurationException: DataSource not found for name [secondary] in configuration. Please check your multiple data sources configuration and try again.
我们为多个架构访问引用了以下链接:
https://objectpartners.com/2016/03/09/using-secondary-datasources-in-grails-3/
Creating a Domain Class with schema in Grails
有人可以建议解决这个问题吗?
答案 0 :(得分:0)
在您的application.yml文件中,以这种方式在默认数据源下使用 datasources::
dataSource:
pooled: true
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
driverClassName: com.mysql.jdbc.Driver
dbCreate: update
url: jdbc:mysql://localhost:3306/default_schema
username: root
password: ''
datasources:
source1:
dialect: org.hibernate.dialect.MySQLInnoDBDialect
driverClassName: com.mysql.jdbc.Driver
username: root
password: ''
url: mysql://localhost:3306/source1
dbCreate: update
现在,您的Customer类应该看起来像
class Customer {
String firstName
String lastName
String address
Static mapping = {
datasource 'source1'
}
}
答案 1 :(得分:0)
您的方法2几乎就在那里,我认为您缺少的是 - 您需要在“数据源”下定义“辅助” - 请参阅 - http://docs.grails.org/latest/guide/conf.html#multipleDatasources