Grails 对于多数据源的增删改查操作
多数据源配置成功后,需要在Domain的mapping这种指定要映射的数据库表,可选则映射到所有库,也可指定某一个数据库,方式如下:
假设:有两个数据源配置:dataSource和extraSource,相关配置参考:Grails 多数据源相关配置说明
Configuring Domain Classes
If a domain class has no DataSource
configuration, it defaults to the standard 'dataSource'
. Set the datasource
property in the mapping
block to configure a non-default DataSource
. For example, if you want to use the ZipCode
domain to use the 'lookup'
DataSource
, configure it like this:
class ZipCode {
String code
static mapping = {
datasource 'lookup'
}
}
A domain class can also use two or more data sources. Use the datasources
property with a list of names to configure more than one, for example:
class ZipCode {
String code
static mapping = {
datasources(['lookup', 'auditing'])
}
}
If a domain class uses the default DataSource
and one or more others, use the special name 'DEFAULT'
to indicate the default DataSource
:
class ZipCode {
String code
static mapping = {
datasources(['lookup', 'DEFAULT'])
}
}
If a domain class uses all configured data sources, use the special value 'ALL'
:
class ZipCode {
String code
static mapping = {
datasource 'ALL'
}
}
下面是例子,以两个数据库为例子:
Namespaces and GORM Methods
If a domain class uses more than one DataSource
then you can use the namespace implied by each DataSource
name to make GORM calls for a particular DataSource
. For example, consider this class which uses two data sources:
class ZipCode {
String code
static mapping = {
datasources(['lookup', 'auditing'])
}
}
The first DataSource
specified is the default when not using an explicit namespace, so in this case we default to 'lookup'
. But you can call GORM methods on the 'auditing' DataSource
with the DataSource
name, for example:
def zipCode = ZipCode.auditing.get(42)
...
zipCode.auditing.save()
control和service中获取数据源的方式:
1.DataSource dataSource 获取默认数据源
2. DataSource dataSource_lookup 获取其他数据源
获取数据源之后可直接对数据库进行增删改查操作:
classTestService{
def dataSource_gibmsSource
def insertTest(){
Sql sql =newSql(dataSource_gibmsSource)
def gibmsSql ="insert into test_gibms(code) values ('000')"
def result = sql.executeInsert(gibmsSql)
sql.close()
}
}
参考文档:http://yongfengsun.blog.163.com/blog/static/792553852016217112851842/
相关推荐
直接导入运行,带MYSQL数据库 我学GRAILS时候做的增删改查! 总共两个表一个是管理员的 一个是 用户表
简明易学的grails中文资料,希望对大家有帮助
Grails Grails Grails Grails Grails
NULL 博文链接:https://niuniufei.iteye.com/blog/2292361
Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南
3.4 数据源配置 3.4.1 支持的数据库 3.4.2 配置自定义方言(dialect) 3.5 引导grails应用程序 3.6 配置日志(logging) 3.6.1 启用sql日志 3.6.2 日志记录(logging)和环境 ...
Grails入门指南_源代码_part2 http://download.csdn.net/source/309290
Grails入门指南_源代码_part1 http://download.csdn.net/source/309290
Grails超全中文操作手册,无论是大神还是初学者都很适用的操作文档。很全很详细,希望能帮到学习grails框架的你。
Grails项目的应用越来越多,而对于初学者来说,在Eclipse下搭建Grails项目是一个难题,这个文档将教会你如何搭建Grails项目,希望对你有所帮助。
简单介绍grails的查询操作,已经类似与hibnatate的查询方式
Grails1.1中文文档
grails-2.1.zip.001
grails+Xfire webservice
Grails入门指南中文pdf -- 针对grails1.0.4更新,附加idea8 开发grails的流程
用grails开发过程中,项目优化配置,系统间调用等的总结。
grails-1.3.0.RC2 源代码
Grails尽量为更多现有的Java项目创建一个全面的框架(不仅局限于视图处理),这和当前一些Java框架提供给用户的一种AnemicAPI形成了明显的对比。Grails的出现并不是偶然的,而是随着Web应用的日趋复杂及Web2.0和Ajax...
Grails 中文 参考手册
3.3.1 数据源和环境 3.3.2 JNDI数据源 3.3.3 自动数据库移植 3.4 外部配置 3.5 定义版本 4. 命令行 4.1 创建Gant脚本 4.2 可复用的Grails脚本 4.3 脚本中的事件 4.4 Ant和Maven 5. 对象关系映射(GORM) 5.1 快速指南 ...