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

有的时候安全起见需要修改ID的生成方式,有时候因为特定的原因对ID字段需要自定义生成,更多的时候是比较无聊。 修改ID字段的默认值

<!-- 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]
    }
}

确实够无聊的。

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

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

domain生成的表名默认和类的名字一样,字段和属性名一样。可以在domain中复写默认的设置,所有的复写都是在static mapping闭包中完成的,

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

复写表名

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

复写字段名

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

更详细的可以参考Grails1.2参考文档速读(9)

转载于:https://my.oschina.net/65304586/blog/165644

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值