在Grails敏捷WEB开发项目的过程中,碰到几种常见查询数据的方法.以某CALL CENTER 项目业务咨询一子模块查询分析如下.
方法一:利用findAllBy*** /findBy**查询.
def search = {
if(request.method == 'POST'){
render(view:'list',model:[businessinstanceList:Business.findByTopicLike('%' +params.topic+ "%",[max:10,offset:0]),businessInstanceTotal: Business.countByTopicLike('%' +params.topic+ "%") ])
}
}
方法二: 建一Java类BusinessInfoQuery 话Src/java下,
def search = {
if (request.method == 'POST') {
BusinessInfoQuery query = new BusinessInfoQuery() //BusinessInfoQuery为新建一个Java类
bindData(query, params)
def criteria = Race.createCriteria()
def results = criteria {
and {
like('topic', '%' + query.topic + '%')
}
}
render(view:'searchresults', model:[ businessInfoList: results.adaptee ])
}
}
方法三: 建一Groovy类SearchCommand 话Src/groovy下,
def search = {
SearchCommand cmd->
if(request.method == 'POST') {
def criteria = BusinessInfo.createCriteria() //创建了一个 Hibernate 的 Criteria 查询
def resultList = criteria{
and {
if(cmd.topic)
like ('topic',"%${cmd.topic}%")
}
order("id", "asc")
}
render(view:'list' , model:[businessInfoInstanceList: resultList, businessInfoInstanceTotal: BusinessInfo.count()])
}
}
}