Last updated by admin 4 years ago
Grails ?? ?? ??(Grails Object Relational Mapping - GORM)
??
?? ???? ??????? ??? ??? ??? ??? ???. ??? ???? ???? ????? ?? ??? ????, ???? ??? ?????. ??? ????? ??? ?? ??? ??? ?? ???? ????.GORM? Grails? ?? ?? ??(ORM) ??????. GORM? ?? ???? ??? ???? ORM ???? Hibernate 3? ???? ???????. ??? GORM? Hibernate? ??, Groovy? ??? ???? ?? ?? ??? ?? ??? ?? ???? Grails? ??(convention) ?? ??? ???? ?? ? ??? ??(configuration)? ? ????.??? ???? Grails ??? ???? ?? ?? ????. ???? Grails ??? ???? ???, Grails? ??? ?? ????? ???? ??? ????? Hibernate? ???? ??? ?????.??? ??? ???
Grails? ??? ???? ? ?? ??? ??? ?? ??? Groovy ??????. ? ?? ???? "id" ??? "version" ??? ?????:class Book {
Long id
Long version String title
}grails create-domain-class
?? ???
??? ??? ????? ?? Set ??? ??? ???? "relatesToMany" Map ?? ??? ??? ?????? ?? ???:class Author {
Long id
Long version def relatesToMany = [ books : Book ] String name
Set books = new HashSet()
}class Book {
Long id
Long version Author author
String title
}class Book {
Long id
Long version belongsTo = Author Author author
String title
}def belongsTo = [Author,Publisher]
- ???(owner)? ???(belongings)? ?? ? ??
- ???? ??? ???? ???? ?? 'belongsTo' ??? ????
- ???? ??? ???? ?? ??? ?? ?????.
?? ??
?? ?? ? ??? A? B ??? ??? ???? ???? ???? ????.??? ???(->)??? ???(<->)? ? ??, ??? (1:1), ??? (1:m), ??? (m:1), ??? (n:m) ? ??? ???? ????. ???? ?? ??? ??????.||A:B || ??? || ??? || |1:1 | A -> B | A <-> B ; B.belongsTo = [A] | |1:m | A -> Set ; A.relatesToMany= [B] | A -> Set ; A.relatesToMany = [B]; B -> A (*) | |m:1 | A -> B | A -> B ; B -> Set ; B.relatesToMany = [A] (*g) | |n:m | n/a | n/a |(*) ??? ????? "?" ?? ?? ?????? {{belongsTo}} ??? ?? ????. (g) ()? ???? ??? ????
GORM? ?? ?? ?? ? ??? ???? ????? ??? ?????. ??? ?? ???? ? ???? ?? ?? ????? ??? ???? ???? ???:class Content {
Long id
Long version
String author
}
class BlogEntry extends Content {
URL url
}
class Book extends Content {
String ISBN
}
class PodCast extends Content {
byte[] audioStream
}def content = Content.findAll() // ??? ???, ?, ?? ???? ?? ?? def podCasts = PodCast.findAll() // ?? ???? ??
??? ??? ?? ??: ?? ????, ?? ?? ?? ? ??? ???? ????? ??? ? ??? ??? ????? ?????. class ?? ??? ??? ?? ?? ?? ???? ????? ?? ??? ???? ?? ????. ??? ?? ????? ??? ??? ??? ?? ? ??? ???? ?? ?????. ?? ???? ???? "??(required)"? ??? ? ??? ???? ?? ?? ???? ?? ???? ???? ?? ??? ???? ?? ??? nullable? ? ??? ?? ?????. ?? ???? ?? ??? ??? ???? ?? ?? ???? ??? "??(required)"? ??? ??? ? ?? ?? ????.
?? ???(optional) ??? ????(transient) ??
????? ?? ??? ???(persistent)?? ???(required)???. ? ???? ????? "optionals"? "transients"?? ??? List ??? ????? ???.class Book {
Long id
Long version optionals = [ "releaseDate" ]
transients = [ "digitalCopy" ] Author author
String title
String author
Date releaseDate
File digitalCopy
}???
?? ??? ?? ???? ??? ?? ????. ?? ??? ?????:class Book {
//omitted code...String title = ''
String author = '[author unknown]'
//etc..
}CRUD ?????
Grails ??? ???? CRUD ??(??/??/??/?? - Create/Read/Update/Delete)? ???? ?? ?? ???? ?????.??
Grails ??? ???? "save" ???? ???? ????? ?? ?? ??? ?? ????? ?? ????? ?????? ??? ? ????. ?? ????? "Author"? "save"?? ???? ??? ????? "Author"? "Book"? ????? ?? ?????:def a = new Author(name:"Stephen King") def b = new Book(title:"The Shining",author:b) a.books.add(b)// ?? a.save()
def addBook(book) {
if(!books)books = new HashSet()
book.author = this
books.add(book)
return this
}def a = new Author(name:"Stephen King") a.addBook(new Book(title:"The Shining")) .addBook(new Book(title:"IT")) .save()
??
Grails? ??? ??? ????? ???? ? ?? ??? ?????. ???? ??? ?? ? ??? ??? #??? ??? ???? ??? ?????. ??? "id"? ?? ?? ??? "get" ?? ???? ???? ???:Book.get(1)
Book.findAll() // ?? ????
Book.list(10) // ?? 10?? ????? ????
Book.listOrderByTitle() // "title" ??? ?? ??? ?? ???? ??????
??? ??? ????? ???? ?? ???? ?? ????? ?? ????. ??? ????? ???? ????? "save"? ???? ??? ??? ??? ???? ?????.def b = Book.get(1)
b.releaseDate = new Date()def b = Book.get(1) b.title = null // ??? null? ? ?? b.save() // ???? ???? ???? ????.
def b = Book.get(1) b.publisher = "Print It"if(!b.validate()) { b.publisher = Publisher.DEFAULT }
b.validate(true)def b = Book.get(1)
b.title = "A New Title"// ?? ??? ???, ???? ??? ??
b.discard()??
"delete" ???? ???? ???? DB??? ????? ??? ? ????:def b = Book.get(1) b.delete()
??? ??? ????
?? ???? ???? ????
??? ??? ????? ???? ??? ????? ????. ? ? ???? Grails ?? ???? ???? ????. ? ??? ??? ??? ??? ?? ???? ?????:def results = Book.findByTitle("The Stand")results = Book.findByTitleLike("Harry Pot%") results = Book.findByReleaseDateBetween( firstDate, secondDate ) results = Book.findByReleaseDateGreaterThan( someDate ) results = Book.findByTitleLikeOrReleaseDateLessThan( "%Something%", someDate )// find by relationship results = Book.findAllByAuthor( Author.get(1) )
??? ?? ????
find() ???? ??? ?? ??? ??? ??? ??? ??? ?? ????:def b = Book.find( new Book(title:'The Shining') )Criteria ??? ???? ????
? ??? ?? ?? ?? ?? ?? ???? ?? ??? ???? Criteria? ?????. Criteria? ?? ?? ????? ??? ?????.def c = Book.createCriteria()
def results = c {
like("author.name", "Stephen%")
between("releaseDate", firstDate, secondDate )
}HQL? ???? ????
? ?? ??? ????. Grails? ????? Hibernate? ???? ????, HQL ??? ?? ? ?? ????:def results = Book.find("from Book as b where b.title like 'Lord of the%'")def results = Book.find("from Book as b where b.title like ?", ["The Shi%"])



