nose :new nose()).save()可以,而 new nose(face:new face()).save()错误 一对多 Class author{static hasMany=[books:book] String name} Class book{String title} Grails 在数据库级别讲默认使用外键映射来影射这种关联 多对多 两方都定义 hasmany 并在关联的被拥有方定义 belongsTo
Class book{static belongsTo=author static hasmany=[authors:author] String title} Class {static hasmany=[books:book] String name} 16 大部分属性都是默认必要的,如果想把某个属性定义为可选的,就要把它加入
列表里面 String postalAddress String gender Static optional=[“postalAddress”,”gender”] 17 Class RaceController{def scaffold=Race} 当 grails 控制器中碰到 scaffold 属性时, 它会动态地为特定的领域类生成控制逻辑以及必 要的视图,而所有的这些都来自于 def scaffold=race 代码 18 都写好了之后启动项目,却发现只有 id 而没有其他属性 奇怪,从新写了一个后部署运行就没事了,不知道原来的
什么毛病,也许是 controller 类 用 grails generate-all 自己创建的,不行吧 自己创建了 controller book author 两个后,在用命令 generate-all 命令来 19
没有显示书的名字 重写 book 中的 tostring 方法 String toString(){“${this.name}”} 20
常用约束 Static constraints={ name(maxLength:50,blank:false) time(min:new Date()) time2(validator:{return (it>new Date())}) state(inList:[‘CA’,’NC’,’SC’],blank:false) gender(inList:[“M”,”F”]) distance(min:3.1f,max:999.99f) dateOfBirth(nullable:false) email(maxLength:50,email:true) } 21 控制器中的 action 就是指控制器类中 def 定义的属性默认的 action 为 index 我们把显示消息赋值给 flash.message=“${params.name}updated.” Flash 是 grails 中的一个作用域, session 一样, 像 flash 对象也是一个单纯的键值对组成 的 map 22 链接 mysql 数据库 数据库的配置
dataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" username = "root" pass
word = "root" } hibernate { cache.use_second_level_cache=true cache.use_query_cache=true cache.provider_class='net.sf.ehcache.hibernate.EhCacheProvider’ } // environment specific settings environments { development { dataSource { dbCreate = "create-drop" // one of 'create', 'create-drop','update' url = "jdbc:my
sql://localhost/development" } } test { dataSource { dbCreate = "update" url = "jdbc:
mysql://localhost/test" } } production { dataSource { dbCreate = "update" url = "jdbc:mysql://localhost/production" } } } 数据库链接部分仍存在
问题 有的是这么写的 url = "jdbc:mysql://localhost/racetrack?useUnicode=true&;characterEncoding=UTF-8" grails 需要三个数据库 开发 测试 产品 racetrack