Restrictions.like(),Restrictions.in(),Restrictions.eq()等方法




 

Restrictions.like(属性名, 查询条件的值, 匹配方式):

Restrictions.in(属性名, 查询条件的值, 匹配方式):

Restrictions.eq(属性名, 查询条件的值, 匹配方式):

 

Criteria Query常用的查询限制方法


Restrictions.eq() equal,=
Restrictions.allEq() 参数为Map对象,使用key/value进行多个等于的对比,相当于多个Restrictions.eq()的效果
Restrictions.gt() greater-than, >
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,否则返回false
Restrictions.isNotNull() 与Restrictions.isNull()相反
Order.asc() 根据传入的字段进行升序排序
Order.desc() 根据传入的字段进行降序排序
MatchMode.EXACT 字符串精确匹配,相当于“like 'value'”
MatchMode.ANYWHERE 字符串在中间位置,相当于“like '%value%'”
MatchMode.START 字符串在最前面的位置,相当于“like 'value%'”
MatchMode.END 字符串在最后面的位置,相当于“like '%value'”

 

public List search(TblFwxx condition) {

  Session session = this.getSession();

  Criteria c = session.createCriteria(TblFwxx.class);

 

  if (null != condition) {

    if (condition.getTitle() != null && !condition.getTitle().equals("")) {

      c.add(Restrictions.like("title", condition.getTitle(), MatchMode.ANYWHERE));   

    }

 

 

  c.addOrder(Order.asc("fwid"));

  return c.list();

 

}

 

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

例1:

表中的数据为:
userid name username password age
------------------------------------------------
    张三  zhang3   zhang3   20
    李四   li4      li4     21
    your  test     test     30

 

Session session = HibernateSessionFactory.getSession();
Criteria criteria = session.createCriteria(Test.class);
List<Test> list = criteria.add(Restrictions.like("name", "%your%"))
.add(Restrictions.like("username", "test"))
.add(Restrictions.eq("age", 30))
.list();

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

例2:

 

private Session session;

public List criteria(SalChance salChance) {
    List result = null;   
        try { 
          session = super.getSession(); 
                Criteria cri = session.createCriteria(SalChance.class);
                if(salChance.getChcCustName()!=null&&salChance.getChcCustName()!=""){
                  cri.add(Restrictions.like("chcCustName", salChance.getChcCustName(),MatchMode.ANYWHERE));
                }
                if(salChance.getChcTitle()!=null&&salChance.getChcTitle()!=""){
                  cri.add(Restrictions.like("chcTitle", salChance.getChcTitle(),MatchMode.ANYWHERE));
                }
                if(salChance.getChcLinkman()!=null&&salChance.getChcLinkman()!=""){
                  cri.add(Restrictions.like("chcLinkman", salChance.getChcLinkman(),MatchMode.ANYWHERE));
                }
                if(salChance.getChcStatus()!=null&&salChance.getChcStatus()!=""){
                  //System.out.println("状态为:"+salChance.getChcStatus());
                  if(salChance.getChcStatus()=="all"){
                    cri.add(Restrictions.in("chcStatus", new Object[]{"2","3","4"}));
                  }else{
                    cri.add(Restrictions.eq("chcStatus", salChance.getChcStatus()));
                  }
                }
                result=cri.list();
                       
        } catch (Exception e) { 
          e.printStackTrace();
        }finally {     
                      session.close();
               
              return result;



  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值