grails编程-domain

来源:互联网 发布:centos snmp 版本 编辑:程序博客网 时间:2024/05/29 03:13

damain包  

2011-05-27 21:36:27|  分类:grails编程 |  标签:damain包  |字号 订阅


1:设置自增主键
     static mapping = {
        id generator:'hilo', params:[table:'hi_value',column:'next_value',max_lo:10000]
    }
2:设置限定条件
  static constraints = {
        lineItems nullable:true, blank:true
        number size:5..30,unique:true
        user nullable:true, blank:true
    }
3:设置父类
  static belongsTo = [父类]
4:设置一对多关系
 static hasMany=[items:Item]
5:设置排序规则
    static mapping = {
        sort 'sequence':'desc'
        id generator:'hilo', params:[table:'hi_value',column:'next_value',max_lo:10000]
    }
6:设置插入前,更新前操作
  def beforeInsert = {
        def now =  new Date()
        dateCreated = now
        lastUpdated = now
    }   
    def beforeUpdate = {
        lastUpdated = new Date()
    }
7:写一个enum(独立于类外)
 enum CouponTypeEnum {
    DISCOUNT(0,'打折'),
    PROMOTION(1,'优惠'),
    POSTAGE(2,'包邮')       
    private final Integer value;
    private final String description;   
    CouponTypeEnum(Integer value, String desc){
        this.value  = value;
        this.description = desc;
    }   
    public Integer getValue(){
        return value;
    }   
    public String getDescription() {
        return description;
    }
   public String toString(){
     return description;
   }

}

应用 if(ss.number==CouponTypeEnum.POSTAGE){}

8:两个分页查询
def queryMyCoupon = {
        def owner = springSecurityService.currentUser
        log.debug "我的优惠券共有"+Coupon.findAllByOwner(owner).size()+"张!"
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        //     def list = Coupon.findAllByOwner(owner,params)
        List freeCouponList = Coupon.findAllByOwnerAndStatus(owner,CouponStatusEnum.ASSIGNED,params)
        def c1 = Coupon.createCriteria()
        List usedCouponList = c1 {
                notEqual("status",CouponStatusEnum.ASSIGNED)
                and {
                        eq ("owner",owner)
                    }
                params
            }  
        render (view:"myCoupon" , model:[freeCouponList:freeCouponList,
                                       freeCouponNumber:freeCouponList.tatolCount()])       
    }

view中分页显示:<g:paginate total="${freeCouponNumber }"/>

原创粉丝点击