Hibernate 查询

1、or 查询

        //两个条件
        criteria.add(Restrictions.or(Restrictions.ilike("name", keyword, MatchMode.ANYWHERE), 
                Restrictions.ilike("phone", keyword, MatchMode.ANYWHERE)));
        
        //多个or条件
        criteria.add(Restrictions.disjunction()
                .add(Restrictions.ilike("name", keyword, MatchMode.ANYWHERE))
                .add(Restrictions.ilike("phone", keyword, MatchMode.ANYWHERE)));

 

2、统计查询

        criteria.createAlias("member", "member", CriteriaSpecification.INNER_JOIN);
        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property("member.id").as("id"));
        projectionList.add(Projections.property("member.loginName").as("loginName"));
        projectionList.add(Projections.property("name"));
        projectionList.add(Projections.property("phone"));
        projectionList.add(Projections.sum("money"));
        projectionList.add(Projections.sum("remainMoney"));
        //sqlProjection的使用
        projectionList.add(Projections.sqlProjection("sum(case end_time < '"+dateTime+"' when 1 then remain_money else 0 end) as expired_money", new String[]{"expired_money"}, new Type[]{ new DoubleType()}));
        projectionList.add(Projections.property("redEnvelope.id"));
        projectionList.add(Projections.groupProperty("member.loginName"));
        criteria.setProjection(projectionList);

 

posted @ 2015-01-28 14:50  rubekid  阅读(293)  评论(0编辑  收藏  举报