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 }