`
newleague
  • 浏览: 1472264 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

hibernate模糊查询-Restrictions.ilike & Expression.like

阅读更多

 Criteria criteria = session.createCriteria(TaDiagnoseSystem.class);
   //增加查询条件
   if(StringUtils.isNotEmpty(systemName)){
//    criteria.createCriteria("taDiagnoseSystemSubs").add(Restrictions.ilike("systemName", systemName, MatchMode.ANYWHERE));
    criteria.createCriteria("taDiagnoseSystemSubs").add(Expression.like("systemName", "%"+systemName+"%"));
   }

 

 

今天的项目中在用Hibernate3.0进行模糊查询的时候,出现中文乱码,在网上查了好久资料,终于解决了,现在把整理后的贴出来。
Hibernate中实现模糊查询,可有以下三种方式:

第一种方式:QBC查询
String name = "", info = "";
if (sub != null && sub.getSubname() != null) {
name = sub.getSubname();
}
if (sub != null && sub.getSubinfo() != null) {
info = sub.getSubinfo();
}
Criteria cr = session.createCriteria(Subject.class);
cr.add(Expression.like("subname","%"+name+"%"));
cr.add(Expression.like("subinfo","%"+info+"%"));

第二种方式:HQL查询语句
String hql = "from Subject as s where s.subname like :name and s.subinfo like :info";
// 调用session的获得数据列表方法,传递HQL查询语句
String  subnamevalue="要查询的中文值";
String   sql="";
sql   = "from Pfapp  where name like :subname ";

Query query = session.createQuery(sql); 
query.setString("subname","%"+subnamevalue+"%");
System.out.println("*********"+sql);

注:Subject为数据库表subject映射的类;它有相应的属性subname、subinfo及相应的get和set方法;sub为Subject的一个实例化对象。
另外说明的是:如果你不写对象名称,直接写字段,那么hibernate就按照数据库字段进行查找
如果你写   (对象名.对象属性),那么hibernate就按照ormapping中的映射字段进行查找
分享到:
评论
1 楼 馋朵朵 2011-09-27  
Criteria 

相关推荐

    net55-r8168-8.045a-napi.x86_64.rar

    2018-04-16: Version 8.045a: Changed package metadata to include proper dependencies and restrictions 2018-02-08: Version 8.045: Support for more NIC models and VLAN tags 2015-01-16: Version 8.039.01...

    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 ...

    php-7.3.6.tar.gz

    Fixed bug #77967 (Bypassing open_basedir restrictions via file uris). Standard: Fixed bug #77931 (Warning for array_map mentions wrong type). Fixed bug #78003 (strip_tags output change since ...

    net-e1000e-3.1.0.2-glr2.x86_64.rar

    Dependencies and Restrictions Replaces/Updates: VMware built-in net-e1000e driver Depends On: vmkapi_2_0_0_0, com.vmware.driverAPI-9.2.0.0 Package Properties Requires MaintMode Live Install ...

    spring-boot-reference.pdf

    SpEL Expression Conditions 46.4. Testing your Auto-configuration 46.4.1. Simulating a Web Context 46.4.2. Overriding the Classpath 46.5. Creating Your Own Starter 46.5.1. Naming 46.5.2. autoconfigure...

    net-tulip-1.1.15-1.x86_64.rar

    Dependencies and Restrictions Depends On: vmkapi_2_1_0_0, com.vmware.driverAPI-9.2.1.0 Compatible With: ESXi 5.0, ESXi 5.1, ESXi 5.5, ESXi 6.0, ESXi 6.5 Does NOT work With: ESXi 6.7 and newer ...

    pima-indians-diabetes.csv

    用于神经网络,深度学习方面的数据集。原网址因为The dataset is no longer available due to permission restrictions.

    net51-r8169-6.011.00-2vft.510.0.0.799733.x86_64.rar

    Dependencies and Restrictions Replaces: net-r8169 Compatible With: ESXi 5.1, ESXi 5.5, ESXi 6.0, ESXi 6.5 Does NOT work With: ESXi 6.7 and newer Package Properties Requires MaintMode Live Install...

    ISO_IEC_11172-2 MPEG1标准

    2.4.5 Restrictions on the Multiplexed Stream Semantics 31 2.4.6 Constrained System Parameter Stream 33 1-ANNEX A (informative) 1 1-A.1 Overview 1 1-A.2 Encoder Operations 1 1-A.2.1 Degrees of freedom ...

    ISO_IEC_11172-1 System 标准

    2.4.5 Restrictions on the Multiplexed Stream Semantics 31 2.4.6 Constrained System Parameter Stream 33 1-ANNEX A (informative) 1 1-A.1 Overview 1 1-A.2 Encoder Operations 1 1-A.2.1 Degrees of freedom ...

    hadoop-1.1.1.tar.gz

    which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software. BEFORE using any encryption software, please check your...

    NIST SP800-76-2.pdf

    Such restrictions are designed to ease implementation, assure conformity, facilitate interoperability, and ensure performance, in a manner tailored for PIV applications. The biometric data ...

    dynamic-ip-restrictions

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

    7-Zip压缩软件 7-Zip is open source software. Most of the source code is under the GNU LGPL license. The unRAR code is under a mixed license: GNU LGPL + unRAR restrictions. Check license information here: 7-Zip license.

    7-Zip压缩软件7-Zip is open source software. Most of the source code is under the ... The unRAR code is under a mixed license: GNU LGPL + unRAR restrictions. Check license information here: 7-Zip license.

    NIST SP800-73-4.pdf

    Such restrictions are designed to ease implementation, facilitate interoperability, and ensure performance, in a manner tailored for PIV applications. Keywords authentication; FIPS 201; identity ...

    High-speed-Charting-Control-200807.rar

    One of the main restrictions was that the control had to plot a lot of data and be able to display it quickly (on a Pocket PC). The control is able to do that by plotting only the new points of data,...

    Build.Native.Cross-Platform.Apps.with.Appcelerator

    Do you have JavaScript experience but are ready to move away from the restrictions of web apps? Did you know that with Appcelerator you can use your existing web experience to build 100% native apps ...

    hadoop-trunk.zip

    which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software. BEFORE using any encryption software, please check your...

    NIST SP800-73-2_part3_end-point-client-api-final.pdf

    FIPS 201 defines procedures for the PIV lifecycle... Such restrictions are designed to ease implementation, facilitate interoperability, and ensure performance, in a manner tailored for PIV applications.

Global site tag (gtag.js) - Google Analytics