`

hibernate Restrictions用法 MatchMode.ANYWHERE

 
阅读更多

方法

说明

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限定查询

 

 

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;

分享到:
评论

相关推荐

    hibernate-shards.jar

    To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain ...

    Restrictions用法.pdf

    。。。

    Restrictions用法.docx

    。。。

    Porting GCC for Dunces

    Contents 1 Legal notice 11 2 Introduction 13 2.1 Background . . . . . . ....2.1.1 Compilers ....2.2 This work ....2.2.3 Restrictions in this document . . . . . . . . . . . . . . . . 14 3 The t

    (3.0版本)自己写的struts2+hibernate+spring实例

    jar包:项目中除了使用了struts2,hibernate3.0和spring1.8以外(用spring2.0的包也可以.不能低于1.8的包.)还是用了junit,ajax,第三方的table组件.等等.所以需要下载相对应的包. 为了上传jar.我专门申请了一个网盘.所有...

    photoscan-软件说明书.pdf

    Restrictions of the Demo mode ..................................................................................... 2 2. Capturing photos .................................................................

    Debugging with GDB --2007年

    Table of Contents Summary of GDB . . . . . . . . ....Free software ....Contributors to GDB ....A Sample GDB Session ....Loading the Executable ....Setting Display width....Setting Breakpoints ....Running the executable ...

    eCall-ERA-GLONASS_ApplicationNote_(UBLOX)

    4.1.1 MSD transfer in push mode ......................................................................................................... 25 4.1.2 MSD update in TX idling mode ...........................

    Perl Security pdf

    Enabling taint mode.........................................................................................................18 Taint and the environment ..................................................

    PDF.Password.Remover.3.0

    Adobe Acrobat Reader) without any restrictions -- i.e. with edit/copy/print functions enabled. All versions of Adobe Acrobat (including 7.x, which features 128-bit encryption) are supported.

    EMC AutoStart 5.3 SP2 管理员指南.pdf

    Working in Advanced user mode ........................................................................ 70 Setting values for attributes and variables.....................................................

    Oracle_start_with_connect_by_prior_用法[文].pdf

    Oracle_start_with_connect_by_prior_用法[文].pdf

    Advanced.PDF.Password.Recovery.v1.46

    Adobe Acrobat Reader) without any restrictions -- i.e. with edit/copy/print/annotate functions enabled. Please note that APDFPR doesn't work with documents which have user-level passwords (preventing...

    hibernate ogm

    Hibernate Object/Grid Mapper (OGM) aims at providing Java Persistence (JPA) support for NoSQL solutions. It reuses Hibernate Core's engine but persists entities into a NoSQL data store instead of a ...

    Springer.The.Developer’s.Guide.to.Debugging.2008.pdf

    2.3.8 Match the Tool to the Bug . . . . . . . . . 12 2.3.9 One Change at a Time . . . . . . . . . 12 2.3.10 Keep an Audit Trail . . . . . . . . 12 2.3.11 Get a Fresh View . . . . . . . . . . . 13 ...

    dynamic-ip-restrictions

    Dynamic IP Restrictions: 保護 IIS 網站的流量守門員 iis 插件

    PDF密码移除工具

    Adobe Acrobat Reader) without any restrictions -- i.e. with edit/copy/print functions enabled. All versions of Adobe Acrobat (including 7.x, which features 128-bit encryption) are supported.

    A-PDF Restrictions Remover

    PDF文档有时不能复制,粘贴,可能是因为屏蔽了复制功能,A-PDF Restrictions Remover可以解除这种屏蔽

    ORACLE OSB开发指南

    Resource Naming Restrictions ......................................................................................................... 2-2 Editing Resources...............................................

Global site tag (gtag.js) - Google Analytics