有的时候安全起见需要修改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)