• Hibernate高级查询QBC条件设置——Restrictions用法 引自:http://www.cnblogs.com/evon168/archive/2010/10/29/1863059.html


    方法说明

    方法

    说明

    Restrictions.eq

    Restrictions.allEq

    利用Map来进行多个等于的限制

    Restrictions.gt

    Restrictions.ge

    >=

    Restrictions.lt

    Restrictions.le

    <=

    Restrictions.between

    BETWEEN

    Restrictions.like

    LIKE

    Restrictions.in

    in

    Restrictions.and

    and

    Restrictions.or

    or

    Restrictions.sqlRestriction

    用SQL限定查询

    2,QBC常用限定方法

    Restrictions.eq --> equal,等于.

    Restrictions.allEq --> 参数为Map对象,使用key/value进行多个等于的比对,相当于多个Restrictions.eq的效果

    Restrictions.gt --> great-than > 大于

    Restrictions.ge --> great-equal >= 大于等于

    Restrictions.lt --> less-than, < 小于

    Restrictions.le --> less-equal <= 小于等于

    Restrictions.between --> 对应SQL的between子句

    Restrictions.like --> 对应SQL的LIKE子句

    Restrictions.in --> 对应SQL的in子句

    Restrictions.and --> and 关系

    Restrictions.or --> or 关系

    Restrictions.isNull --> 判断属性是否为空,为空则返回true

    Restrictions.isNotNull --> 与isNull相反

    Restrictions.sqlRestriction --> SQL限定的查询

    Order.asc --> 根据传入的字段进行升序排序

    Order.desc --> 根据传入的字段进行降序排序

    MatchMode.EXACT --> 字符串精确匹配.相当于"like 'value'"

    MatchMode.ANYWHERE --> 字符串在中间匹配.相当于"like '%value%'"

    MatchMode.START --> 字符串在最前面的位置.相当于"like 'value%'"

    MatchMode.END --> 字符串在最后面的位置.相当于"like '%value'"

    例子 查询年龄在20-30岁之间的所有学生对象 List list = session.createCriteria(Student.class)       .add(Restrictions.between("age",new Integer(20),new Integer(30)).list(); 查询学生姓名在AAA,BBB,CCC之间的学生对象 String[] names = {"AAA","BBB","CCC"}; List list = session.createCriteria(Student.class)       .add(Restrictions.in("name",names)).list(); 查询年龄为空的学生对象 List list = session.createCriteria(Student.class)       .add(Restrictions.isNull("age")).list(); 查询年龄等于20或者年龄为空的学生对象 List list = session.createCriteria(Student.class)       .add(Restrictions.or(Restrictions.eq("age",new Integer(20)),                  Restrictions.isNull("age")).list();

    -------------------------------------------------------------------- 使用QBC实现动态查询 public List findStudents(String name,int age){

    Criteria criteria = session.createCriteria(Student.class); if(name != null){   criteria.add(Restrictions.liek("name",name,MatchMode.ANYWHERE)); } if(age != 0){   criteria.add(Restrictions.eq("age",new Integer(age))); } criteria.addOrder(Order.asc("name"));//根据名字升序排列 return criteria.list(); }

    -----------------------------------------------------------------------------------

    今天用了写hibernate高级查询时用了Restrictions(当然Expression也是可以以的)这个类.感觉不错. 下面的代码写的不易读.其实核心就是一句 Restrictions.or(Restrictions.like(),Restrictions.or(Restrictions.like,........)) 里面的or可以无限加的.还是比较好用
    Session session = getHibernateTemplate().getSessionFactory()                 .openSession();         Criteria criteria = session.createCriteria(Film.class);         List<Film> list = criteria.add(             Restrictions.or(Restrictions.like("description", key,MatchMode.ANYWHERE),             Restrictions.or(Restrictions.like("name", key,MatchMode.ANYWHERE),                 Restrictions.or(    Restrictions.like("direct", key,MatchMode.ANYWHERE),                 Restrictions.or(Restrictions.like("mainplay",key,MatchMode.ANYWHERE),                         Restrictions.like("filearea", key,MatchMode.ANYWHERE)))))).list();
            session.close();         return list;

    查询总条数:

    return (Integer) c.setProjection(Projections.rowCount())
         .uniqueResult();

    c.add(Restrictions.in("status", new String[]{"1","3","12"}));

    c.add(Restrictions.in("areaNo", areaList));

    ---查询结果只有一条记录或null,否则抛出异常

    return (P2pLimitAmountInfo) c.uniqueResult();

    SQL限制

    Criteria c =c.add(Restrictions.sqlRestriction("to_char(create_date,'yyyy-MM-dd') = '"+ time + "'"));

  • 相关阅读:
    POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题
    POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题
    POJ 2195 Going Home 最小费用流 裸题
    POJ 3368 Frequent values RMQ 训练指南 好题
    POJ 3187 杨辉三角+枚举排列 好题
    POJ 2393 贪心 简单题
    系统监控
    系统的初始化和服务
    vi与vim
    正文处理命令及tar命令
  • 原文地址:https://www.cnblogs.com/Defry/p/4516436.html
Copyright © 2020-2023  润新知