1.不生成 version 字段sql
static mapping = { version false }
2.分頁結果集列表 PagedResultList,很是適合分頁查詢數據庫
def c = Account.createCriteria() def results = c.list (max: 50, offset: 10) { like("holderFirstName", "Fred%") and { between("balance", 500, 1000) eq("branch", "London") } order("holderLastName", "desc") } println "Rendering ${results.size()} Accounts of ${results.totalCount}"
results.size()
爲返回的 list 記錄大小,上面爲offset爲10的50條記錄app
results.totalCount
爲符合條件記錄在數據庫大小ide
3.使用proxy() 方法獲取關聯Domainhibernate
class Book{ Author author } def author=Author.proxy(1) //不會到數據去查詢實際數據 Book.findByAuthor(author)
author只是補助做用,不須要實際數據,若是用get方法效率低下code
用proxy 方法實際沒有鏈接數據庫get
4.便捷方法it
5.Domain 數據很是大,不但願直接關聯,能夠採用間接關聯io
class Book{ static transients = ['author'] Long authorId Author getAuthor(){ Author.get(authorId) } void setAuthor(Author author){ authorId=author.id } }
6.SQL Restrictionstable
def c = Person.createCriteria() def peopleWithShortFirstNames = c.list { sqlRestriction "char_length(first_name) <= 4" }
7.Query cache
修改 DataSource.groovy 使 cache.use_query_cache=true
hibernate { cache.use_second_level_cache=true cache.use_query_cache=true cache.provider_class='org.hibernate.cache.EhCacheProvider' }
使用 Query cache
def person = Person.findByFirstName("Fred", [cache: true]) def people = Person.withCriteria { like('firstName', 'Fr%') cache true }
8.Where Queries
Grails 2.0 新增的簡潔強大查詢方式
def query = Person.where { firstName == "Bart" } Person bart = query.find() class Person { static simpsons = where { lastName == "Simpson" } … } … Person.simpsons.each { println it.firstname }
9.Batch Updates and Deletes
def query = Person.where { lastName == 'Simpson' } int total = query.updateAll(lastName:"Bloggs") def query = Person.where { lastName == 'Simpson' } int total = query.deleteAll()
10.Inheritance Strategies
默認是 table-per-hierarchy (每一個層級 一個表) 策略
若是但願table-per-subclass(每一個子類一個表) 策略 使用 tablePerHierarchy false
class Payment { Integer amount static mapping = { tablePerHierarchy false } } class CreditCardPayment extends Payment { String cardNumber }