package com.fhjony.ocbtimport grails.web.servlet.mvc.GrailsParameterMapclass MemberService {def save(GrailsParameterMap params) { Member member = new Member(params) def response = AppUtil.saveResponse(false, member) if (member.validate()) { member.save(true) if (!member.hasErrors()){ response.isSuccess = true } } return response}def update(Member member, GrailsParameterMap params) { member.properties = params def response = AppUtil.saveResponse(false, member) if (member.validate()) { member.save(flush: true) if (!member.hasErrors()){ response.isSuccess = true } } return response}def getById(Serializable id) { return Member.get(id)}def list(GrailsParameterMap params) { params.max = params.max ?: GlobalConfig.itemPerPage() List<Member> memberList = Member.createCriteria().list(params) { if (params?.colName && params?.colValue) { like(params.colName, "%" + params.colValue + "%") } if (!params.sort) { order("id", "desc") } } return [list: memberList, count: memberList.totalCount]}def delete(Member member) { try { member.delete(flush: true,failOnError:true) } catch (Exception e) { println(e.getMessage()) return false } return true}}
1 回答
猛跑小猪
TA贡献1858条经验 获得超8个赞
您希望数据库交互发生在事务上下文中。其中一个简单的部分是您可以使用 标记您的服务类别@grails.gorm.transactions.Transactional。
除此之外,这与您的问题并不真正相关,但是将GrailsParameterMapmap 作为方法参数传递是一件不寻常的事情。正确的做法取决于应用程序中的某些因素,您可能希望将值传递到服务中而不是整个地图中,但如果您确实想要服务中的整个地图,实现这一点的一种方法是通过WebAttributes.
import grails.gorm.transactions.Transactional
import grails.web.api.WebAttributes
@Transactional
class MemberService implements WebAttributes {
def serviceMethod() {
// you can access params here because
// WebAttributes provides access to it
Member member = new Member(params)
// ...
}
}
添加回答
举报
0/150
提交
取消