Last updated by admin 4 years ago
??? ??? ?? ??? ? ??
??
errors
??
?? ??? ??? "validate"? "save" ???? ?? ??? ?? ????
def b = new Book(title:"The Shining") if(!b.validate()) { b.errors.each { println it } }
constraints
??
??? ???? constraints ??? ?? ??? ?? ?????. ??? ? ??? org.codehaus.groovy.grails.validation.ConstrainedProperty? ???????. (??? ??)??
def b = new Book(title:"The Shining") b.constraints.each { println it.name println it.maxLength }
properties
??
??? ???? ?? ??? ?? Map ???. HTTP ?? ??? ??? ???? ???? ???? ?? ??? ?? ???? ?????(Allows access to the domain class properties as a map and perform types conversion when set allowing properties to be set from request parameters for example).??
def b = new Book(title:"The Shining") b.properties = this.params
???
add
??
??? ?? ??? ??? ???? ??? ??? ?????.??
- to - ????? ?? hasMany ?? ??? ?????.
??
def a = new Author(name:"Stephen King") .add(to:"fiction", new Book(title:"IT")) .add(to:"non-fiction", new Book(title:"On Writing: A Memoir of the Craft")) .save()
add*
??
??? ?? ??? ??? ???? ??? ??? ?????. ??? ??? ???(suffix)? ??? ??? ??? ?? ?????.??
def a = new Author(name:"Stephen King") .addBook(new Book(title:"IT")) .addBook(new Book(title:"The Stand")) .save()
delete
??
??? ??? ????? DB?? ?????.??
def b = Book.get(1) b.delete()
discard
??
?? ? ??? ?? ?? ??? ????. ? ???? ???? ?? ??? ?? ?? ??????? ????. ?? ??? ?? DB? ???? ??? ?? ????.??
def b = Book.get(1)
b.title = "Blah"
b.discard() // ?? ??? ???? ????.discard
??
??? ?? ??? ?????. ?? ??? ???? ??? ???? ???? ?? ??? Grails? ???? ??? ??? DB? ???? ?? ??? ???? ?? ?????.??
def b = Book.get(1)
b.title = "Blah"
b.discard() // ?? ?? ??? ???? ?? ????.hasErrors
??
?? ??? ??? "validate"? "save"? ??, ??? ??? ?? ?(true)? ?????.??
def b = new Book(title:"The Shining") b.validate() if(b.hasErrors()) { b.errors.each { println it } }
ident
??
??? ???? ?? ??? ??? ???? ?? ?? ??(??? id? ?? ????), ?? ??? ?? ?????.??
def b = new Book(title:"The Shining") b.save()println b.ident()
merge
??
Hibernate3? merge? ?? ??? ?????.refresh
??
??? ???? ??? DB??? ?? ?????.??
def b = Book.get(1) b.refresh()
save
??
??? ??? ????? DB? ?????. ??? ?? ? ????? ?? ?? ????? ????? DB? ?????. ???? ???? ????? DB? ???? ???? false? ?????.??
- validate (optional) - ???? ?? ?? ??? ??? false? ?????.
??
def b = new Book(title:"The Shining") if( !b.save() ) { b.errors.each { println it } }
validate
??
??? ????? ??? ??? ???? ?? ???? ?????. (??? ??)??
def b = new Book(title:"The Shining") if( !b.validate() ) { b.errors.each { println it } }
?? ???
count
??
DB? ??? ????? ??? ?????.??
????
def noOfBooks = Book.count()
countBy
??
Grails ?? ??? ???? ?? ?? ??? ???? ????? ??? ???? ?? ?????
class Book {
Long id
Long version
String title
Date releaseDate
String author
}def c = Book.countByTitle("The Shining") c = Book.countByTitleAndAuthor("The Sum of All Fears", "Tom Clancy") c = Book.countByReleaseDateBetween(firstDate, new Date()) c = Book.countByReleaseDateGreaterThanOrEqual(firstDate) c = Book.countByTitleLike("%Hobbit%") c = Book.countByTitleNotEqual("Harry Potter") c = Book.countByReleaseDateIsNull() c = Book.countByReleaseDateIsNotNull()
createCriteria
??
??? ???? ?? grails.orm.HibernateCriteriaBuilder ????? ?????. (?? ??)??
def c = Account.createCriteria()
def results = c {
like("holderFirstName", "Fred%")
and {
between("balance", 500, 1000)
eq("branch", "London")
}
maxResults(10)
order("holderLastName", "desc")
}exists
??
??? id? ?? ????? DB? ????? ?????. ?? ?? true? ?????.??
- id (??) - ????? ????? id
??
if(Account.exists(1)) { // do something }
executeQuery
??
??? ???? ?? HQL ??? ?????.??
- query (??) - HQL ??? ??
- params (????) - ??? ?? ?? ?? ??
??
Account.executeQuery( "select distinct a.number from Account a where a.branch = ?", 'London' ); Account.executeQuery( "select distinct a.number from Account a where a.branch = ? and a.created > ?", ['London',lastMonth] );
find
??
??? ??? ?? ??? ??? ?????. ??? ?? ?? null? ?????.??
- query (??) - HQL ?? ?? ??? ?? ??(query by example)? ???? ?? ??? ???
- arguments (????) - ??? ?? ?? ?? ??
??
Book.find("from Book as b where b.author='Dan Brown'") // Dan brown's first book Book.find("from Book as b where b.author=?",['Dan Brown']) // with a positional parameterdef b = new Book(author:"Dan Brown") Book.find(b) // query by example
findAll
??
??? ??? ???? ?? ??? ??? ????? ?????.??
- query (????) - HQL ?? ?? ??? ?? ??(query by example)? ???? ?? ??? ???
- arguments (????) - ??? ?? ?? ?? ??
- max (????) - ??? ?? ? ?
??
Book.findAll() // everything Book.findAll("from Book as b where b.author='Dan Brown'",10) // The first 10 books from Dan Brown Book.findAll("from Book as b where b.author=?",['Dan Brown'],10) // with a named parameterdef b = new Book(author:"Dan Brown") Book.findAll(b) // query by example
findBy
??
Grails ?? ??? ???? ?? ??? ?? ??? ???? ??? ????? ?????.??
class Book {
Long id
Long version
String title
Date releaseDate
String author
}def b = Book.findByTitle("The Shining") b = Book.findByTitleAndAuthor("The Sum of All Fears", "Tom Clancy") b = Book.findByReleaseDateBetween(firstDate, new Date()) b = Book.findByReleaseDateGreaterThanOrEqual(firstDate) b = Book.findByTitleLike("%Hobbit%") b = Book.findByTitleNotEqual("Harry Potter") b = Book.findByReleaseDateIsNull() b = Book.findByReleaseDateIsNotNull()
findAllBy
??
Grails ?? ??? ???? ?? ??? ?? ??? ???? ?? ????? ?????.??
class Book {
Long id
Long version
String title
Date releaseDate
String author
}def results = Book.findAllByTitle("The Shining", [max:10, sort:"title", order:"desc", offset:100] ) results = Book.findAllByTitleAndAuthor("The Sum of All Fears", "Tom Clancy") results = Book.findAllByReleaseDateBetween(firstDate, new Date()) results = Book.findAllByReleaseDateGreaterThanOrEqual(firstDate) results = Book.findAllByTitleLike("%Hobbit%") results = Book.findAllByTitleNotEqual("Harry Potter") results = Book.findAllByReleaseDateIsNull() results = Book.findAllByReleaseDateIsNotNull()
findWhere
??
??? ???? ?? ??? ???? ??? ???? ??? ?? ?? ???? ??? ??? ?????.??
class Book {
Long id
Long version
String title
Date releaseDate
String author
}def b = Book.findWhere(title:"The Shining", author:"Stephen King")
findAllWhere
??
??? ???? ?? ??? ???? ??? ???? ??? ?? ?? ???? ?? ??? ?????.??
class Book {
Long id
Long version
String title
Date releaseDate
String author
}def books = Book.findAllWhere(author:"Stephen King")get
??
??? id? ?? ??? ??? ????? ?????. ?? id? ????? ???? ??? null? ?????.??
def b = Book.get(1)
list
??
??? ???? ?? ????? ?????.??
- max - ??? ?? ??
- offset - ??? ??? ???
- order - ?? ??. "desc" ?? "asc"
- sort - ??? ??? ?? ??
??
def results = Book.list() // everything def results = Book.list(max:10) // 10 results def results = Book.list(max:10, offset:100) // 10 results, offset by 100 def results = Book.list(max:10, offset:100, sort:"title", order:"desc") // 10 results, offset by 100, orderd by title in descending order
listOrderBy
??
??? ???? ??? ?? ?? ?? ??? ?? ??? ??? ????? ??? ?????.??
- max - ??? ?? ??
??
def results = Book.listOrderByAuthor() // everything
def results = Book.listOrderByTitle(max:10) // 10 results
def results = Book.listOrderByTitle(max:10, offset:100, order:"desc") // 10 results, offset from 100withCriteria
??
???? ?? Criteria? ??? ??? ???? ????. (0.4 ??)??
- arguments (????) - Criteria ????? ??? ??? ??? Map
- closure - Criteria? ???? ???
??
def results = Book.withCriteria {
def now = new Date()
between('releaseDate', now-7, now)
like('title', '%Groovy%')
}withTransaction
??
Spring? TransactionTemplate? ???? ?? ?????. (0.4 ??)??
- closure - savepoints (SavepointManager ??) ? ???? ??? ?? ???? ?? Spring TransactionStatus? ???? ? ???
??
Book.withTransaction { tx ->
def b = new Book(title:"Groovy in Action") b.save() // ???? ??? ???? tx.setRollbackOnly()}


