改进groovyq的这篇文章:
http://www.groovyq.net/content/%E8%BF%94%E5%9B%9E%E9%83%A8%E5%88%86doaminclass#comment-179
感谢groovyq对Grails新人的指点.
def excludedProps = [Events.ONLOAD_EVENT,
Events.BEFORE_DELETE_EVENT, Events.AFTER_DELETE_EVENT,
Events.BEFORE_INSERT_EVENT, Events.AFTER_INSERT_EVENT,
Events.BEFORE_UPDATE_EVENT, Events.AFTER_UPDATE_EVENT]
grailsApplication.domainClasses.each{ domainClass ->
/**
* 添加取domain部分属性的功能
* 参数[include:[],except:[],relationship:[]]
*/
domainClass.metaClass.part = {m=[:]->
def map= [id:delegate.id]
if(m.'include'){
m.'include'.each{
map[it] = delegate."$it"
}
}else{
domainClass.persistentProperties.each{
if(!(m.'except' && it.name in m.'except') && !(it.name in excludedProps) && !it.isAssociation()){
map[it.name]= delegate."${it.name}"
}
}
}
m.'relationship'?.each{key,value=null->
def mapKey = value?:key
key.split(/\./).each{propName->
map[mapKey] = (map[mapKey]?:delegate)."$propName"
}
}
return map
}
使用方法:
def instance = SomeDomain.get(0);
//输出所有的非关联属性,以及指定的关联的某几个属性
instance.part( relationship:['bureau.id','bureau.name','bureau.code'] ) as JSON
//{id:1,"code":"AA","descript":null,"floorHeight":null,"floorLoad":null,"floorNo":null,"name":"AAA","bureau.id":0,"bureau.name":"A","bureau.code":"AAA"}
//仅输出指定的非关联属性以及指定的关联属性
instance.part(
include:['code','name'],
relationship:['bureau.name']
)
//{"id":0,code:"aaa","name":"AAA","bureau.name":"A"}
//输出除了id和name外的所有非关联属性以及指定的关联属性.
instance.part(
except:['name'],
relationship:['bureau.name']
)
//{id:0,"code":"AA","descript":null,"floorHeight":null,"floorLoad":null,"floorNo":null,"bureau.name":"A"}
//指定关联属性的命名.
instance.part(
except:['name'],
relationship:['bureau.id':'bureauId','bureau.name':'bureauName']
)
//{id:0,"code":"AA","descript":null,"floorHeight":null,"floorLoad":null,"floorNo":null,"bureauId":"A","bureauName":"A"}
分享到:
相关推荐
The.Definitive.Guide.to.Grails.Second.Edition.2009
Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.2008英文原版
Definitive.Guide.to.Grails.part2
Groovy make java dynamic and Grails make web application more easy to produce
grails.2.5.1 环境,适用于Windows下Grails开发,具体配置请参照https://jingyan.baidu.com/article/86fae3461af6cf3c49121a91.html
Grails.2.A.Quick-Start.Guide.pdf
精通grails.rar
grails3.2.8.7z.002 PART2
Grails in Action is a comprehensive introduction to the Grails framework covering the nuts and bolts of all core Grails components: controllers, views, services, webflows, taglibs, Ajax, and plugins. ...
Definitive Guide to Grails
grails开发实践,欢迎下载使用 grails开发实践,欢迎下载使用 grails开发实践,欢迎下载使用
Agile Development with Groovy and Grails.pdf
Apress.Beginning.Groovy.and.Grails.From.Novice.to.Professional.Jun.2008 grails_programming Grails1.1中文文档-----2009.3.25 Grails入门指南(第二版) Groovy经典入门 Programming Groovy 2 合集,超实惠哦
该插件与Grails 2.5.x(支持JDK8的Grails的第一个版本)兼容。 提供将输入绑定到Java 8 Temporal的功能。 支持Java 8时间类型的JSON和XML呈现。 添加依赖项以帮助持久化Java 8 Temporal。 如何安装 在“插件”...
Groovy&Grails - 指南.pptGroovy&Grails - 指南.pptGroovy&Grails - 指南.pptGroovy&Grails - 指南.pptGroovy&Grails - 指南.ppt
精通Grails 之用 JSON 和Ajax 实现异步Grails。书中包含了全部代码。免费下载
Grails开源框架 - 使用指南,版本1.0,中文文档。 Grails开源框架 - 使用指南 作者: Graeme Rocher, Marc Palmer 版本: 1.0 Copies of this document may be made for your own use and for distribution to ...
grails-1.2.2.zip
grails-2.4.4.zip 的框架.直接集成至IDE,导入即可使用.
Grails入门快速开发指南,里面包含一个完整的工程实例,当然也有一些简单的理论,比较适合初学者