我们在 Grails 中创建了两个不同的域对象,并尝试从两个不同的模式进行访问。
方法一:
例如:
学生.groovy
class Students {
String id
String name
String address
Static mapping = {
schema: 'student_details'
}
}
客户.groovy
class Customer {
String firstName
String lastName
String address
Static mapping = {
schema: 'customer_details'
}
}
应用程序.yml
environments:
development:
dataSource:
dbCreate: update
url: jdbc:mysql://localhost:3306/
如果我在 url 连接字符串中提供默认架构,它总是引用该默认架构,而不管域类中定义的架构并抛出异常,找不到表。如果我从 url 连接字符串中删除默认架构,我会在日志中收到“未选择数据库”错误。
方法二:
我们尝试在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/
任何人都可以提出解决方案吗?