这一篇快速过一遍这套框架有什么特性…
Controllers Usage
- actionName
- controllerName
吃瓜群众不明白
- allowedMethods (约束请求方法)
- bindData
关于 bindDate 的使用
- chain
- defaultAction
- errors
- flash
- forward
- grailsApplication
- hasErrors
- namespace
- params
- redirect
- render
关于 render 的使用
从官网来看 http://docs.grails.org/latest/ref/Controllers/render.html
返回的类型主要有:可指定编码格式的文本、模板的渲染 template [model、collection、bean]、直接渲染视图、markup、XML、JSON、HTTP状态码、文件
// Automatic marshalling of XML and JSON
import grails.converters.*
…
render Book.list(params) as JSON
render Book.get(params.id) as XML
// render a file
render(file: new File(absolutePath), fileName: "book.pdf")
- request
- respond
- response
- responseFormats
- scope
- servletContext
- session
- withForm
- withFormat
从domain中获取数据并遍历
上一个教程完整地建立了一个domain(其实我改了很多东西了..)
Service Usage
我们使用 Service 进行操作我们的domain , 需要注意的是
- In Grails a service is a class with a name that ends in the convention “Service” and lives in the grails-app/services directory.)
上面的意思大概就是你的 Service 必须命名于 Service 后缀(使用插件就自动帮你补全) 且 放在 /services 中.
Domain Usage
关于高级的使用(高质量的干货) : http://sysgears.com/articles/advanced-gorm-features-inheritance-embedded-data-maps-and-lists-storing/
addTo
处理表之间的关系 one-to-many or many-to-many relationship
def fictBook = new Book(title: "IT") def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft") def a = new Author(name: "Stephen King") .addToFiction(fictBook) .addToNonFiction(nonFictBook) .save() class Author { String name static hasMany = [fiction: Book, nonFiction: Book] } class Book { String title static belongsTo = [author: Author] }
attach
配合 isAttached() ,关联已经被 discard() 的实例到当前的会话中
def b = Book.get(1) b.title = "Blah" b.save(flush:true) b.discard() … if (!b.isAttached()) { b.attach() }
belongsTo
一对多
clearErrors
清楚错误的信息,用于 validate() 之后
def b = new Book(title: "The Shining") b.validate() if (b.hasErrors()) { // clear the list of errors b.clearErrors() // fix the validation issues b.author = "Stephen King" // re-validate b.validate() }
constraints
声明验证约束
static constraints = { title blank: false, size: 5..150 author nullable: true }
count
总计对象的行数
countBy
这个是动态查询,能够无限的延长(怎么用就不知道了),科科,大概就是三十一个…
createCriteria
详细操作 http://docs.grails.org/latest/ref/Domain%20Classes/createCriteria.html
delete
冬眠实例..并立刻删除
book.delete(flush: true)
discard
不被保存
embedded
将同一张表的内容进行划分,便于理解与操作
errors
配合validate使用
def user = new User(params) if (user.validate()) { // do something with user } else { user.errors.allErrors.each { println it } }
executeQuery
执行 HQL
executeUpdate
exists
fetchMode
find
findAll
findAllBy
findAllWhere
findBy
findOrCreateBy
findOrCreateWhere
findOrSaveBy
findOrSaveWhere
findWhere
first
get
getAll
getDirtyPropertyNames
getPersistentValue
hasErrors
hasMany
hasOne
ident
instanceOf
isAttached
isDirty
last
list
listOrderBy
load
lock
mapWith
mappedBy
mapping
merge
namedQueries
properties
read
refresh
removeFrom
save
transients
validate
where
whereAny
withCriteria
withNewSession
withSession
withTransaction