Groovy&Grails-代码剪辑-修改主键

有的时候安全起见须要修改ID的生成方式,有时候由于特定的缘由对ID字段须要自定义生成,更多的时候是比较无聊。 修改ID字段的默认值node

<!-- lang: groovy -->
class Foo {
    static mapping = {
        id column: 'foo_id',generator:'hilo',params:[
            table: 'keygen',column: 'next', max_lo: 1000
        ]
}

使用UUID方式生成ID安全

<!-- lang: groovy -->
class Bar {
    String id
    static mapping = {
        id generator: 'assigned'
    }
}

自定义ID的值闭包

<!-- lang: groovy -->
class Foo implements Serializable {
    string code1,code2

    static mapping = {
        id composite: [code1,code2]
    }
}

确实够无聊的。app

附带一些问题解答: domain生成表以后会自动生成一个version字段,用来记录版本号,若是不想要这个字段,能够这么设置一下dom

<!-- lang: groovy -->
static mapping = {
    version false
}

domain生成的表名默认和类的名字同样,字段和属性名同样。能够在domain中复写默认的设置,全部的复写都是在static mapping闭包中完成的,.net

<!-- lang: groovy -->
class Todo {
    static mapping = {
        //这里插入代码
    }
}

复写表名code

<!-- lang: groovy -->
table 'todo_tbl'

复写字段名文档

<!-- lang: groovy -->
columns {
    name column:'name_str'
    note column:'note_str'
}

更详细的能够参考Grails1.2参考文档速读(9)get

相关文章
相关标签/搜索