Grails获取域类的表列

本文介绍在Grails应用中如何遍历并注册所有域类,同时展示如何获取并打印每个域类的属性细节,包括使用正确的方法通过getKey()函数获取属性键名。
    def grailsApplication


        grailsApplication.getDomainClasses().each { it ->
            def domain = "${it.packageName}.${it.name}"
            println("注册:${domain}")
            def ee = EngineeringElement.findByNameAndDataTypeCode(domain, headerCode)
            if (!ee) {
                ee = new EngineeringElement(name: domain, dataTypeCode: headerCode, parentElement: null)
                engineeringElementService.save(ee)
                def item = it.clazz //.newInstance()
                def colums = GrailsDomainBinder.getMapping(item).columns
                colums.each { ite ->
                    println("填写:${ite}")
                }
            }
        }

结果:

创建基础的DataTypeCode: 域类标题
注册:cn.edu.cup.system.UserRole
填写:user=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:false, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
填写:role=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:false, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
注册:cn.edu.cup.engineering.DataTypeCode
填写:name=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:true, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
填写:description=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:false, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
填写:parent=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:false, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
注册:cn.edu.cup.physical.PhysicalQuantity
填写:quantityName=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:true, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
填写:englishName=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:true, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
填写:symbol=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:true, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
填写:unitNameISO=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:true, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]
填写:unitSymbolISO=property[type:null, lazy:null, columns:[column[name:null, index:null, unique:true, length:-1, precision:-1, scale:-1]], insertable:true, updateable:true]

正确的方法:

                def columns = GrailsDomainBinder.getMapping(item).columns
                columns.each { ite ->
                    println("填写:${ite.getKey()}")
                }

关键是getKey()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值