为何会出现查询为空,但是查询整个表没问题的情况呢?

这里是没有分清字符串和变量

原来写的是,

String hql = "from ClientInfoModel where clientIp=?";
Query query = session.createQuery(hql);
query.setString(, “cip”);
List<ClientInfoModel> allCientInfo = query.list();

改正后的

String hql = "from ClientInfoModel where clientIp=?";
Query query = session.createQuery(hql);
query.setString(0, cip);
List<ClientInfoModel> allCientInfo = query.list();

正确的参考

、  第一种,用?占位符,如:
//登录(用?占位符)
public List<UserPO> LoginUser(UserPO up)throws Exception{
Session session = HibernateSessionFactory.getSession();
String hql = "from UserPO where name = ? and pwd= ?";
Query query = session.createQuery(hql);
query.setString(, up.getName());
query.setString(, up.getPwd());
List<UserPO> list = query.list();
session.close();
return list;
} 、用“:+命名”占位符,如:
//登录(用":命名"占位符)
public List<UserPO> LoginUser2(UserPO up)throws Exception{
Session session = HibernateSessionFactory.getSession();
String hql = "from UserPO where name = :n and pwd= :p";
Query query = session.createQuery(hql);
query.setString("n", up.getName());
query.setString("p", up.getPwd());
List<UserPO> list = query.list();
session.close();
return list;
} 2.1、使用这种占位符还可以这样设值,如: //登录(用":命名"占位符,用setParameter设值)
public List<UserPO> LoginUser3(UserPO up)throws Exception{
Session session = HibernateSessionFactory.getSession();
String hql = "from UserPO where name = :n and pwd= :p";
Query query = session.createQuery(hql);
query.setParameter("n", up.getName());
query.setParameter("p",up.getPwd());
List<UserPO> list = query.list();
session.close();
return list;
}
使用这种方式不需要写明映射的类型,Hibernate会通过配置自动给我们转,但是由于Hibernate有两种日期格式:Date和TIMESTAMP,所以对于日期类型必须写明映射的类型。写法: 、按照对象进行参数绑定,如: //登录(用":命名"占位符,用setProperties设值,命名参数必须要与被绑定的属性名相同)
public List<UserPO> LoginUser4(UserPO up)throws Exception{
Session session = HibernateSessionFactory.getSession();
String hql = "from UserPO where name = :name and pwd= :pwd";
Query query = session.createQuery(hql);
query.setProperties(up);
List<UserPO> list = query.list();
session.close();
return list;
}
、使用条件查询(Criteria),如: //登录(用条件查询 Criteria)完全脱离sql语句和hql语句
public List<UserPO> LoginUser5(UserPO up)throws Exception{
Session session = HibernateSessionFactory.getSession();
Criteria cri = session.createCriteria(UserPO.class);
cri.add(Restrictions.eq("name", up.getName()));
cri.add(Restrictions.eq("pwd", up.getPwd()));
List<UserPO> list = cri.list();
session.close();
return list;
} 、离线条件查询,如:
//登录(用离线条件查询 DetachedCriteria)
public List<UserPO> LoginUser6(UserPO up)throws Exception{
Session session = HibernateSessionFactory.getSession();
DetachedCriteria dc = DetachedCriteria.forClass(UserPO.class);
dc.add(Restrictions.eq("name", up.getName()));
dc.add(Restrictions.eq("pwd", up.getPwd()));
Criteria cri = dc.getExecutableCriteria(session);
List<UserPO> list = cri.list();
session.close();
return list;
}
使用离线可以将其写在业务层,以参数的形式传入,以减少DAO的代码。
、分页查询:分页查询是数据库应用中的处理方式,query和criteria接口都提供了用于分页查询的方法:
) setFirstResult(int):指定从哪一个对象开始查询,参数是索引位置,从0开始。
) setMaxResult(int):指定一次最多查询的对象数量。

hql 条件查询 返回空的一种情况的更多相关文章

  1. FineReport: 参数为空选出全部值(按条件查询,空条件时直接过滤,不进行查询。。)

    在Java报表软件FineReport中,选择特定的参数(如下图中的姓名.身份证号等)后,会返回我们要查询的数据,然而假如没有输入参数值,我们却仍需要返回数据时该怎样处理呢?应该过滤掉这个条件,不按这 ...

  2. JdbcTemplate查询返回JavaBean的几种方法

    关于JdbcTemplate的官方描述如下: org.springframework.jdbc.core.JdbcTemplate 大约的讲,将JdbcTemplate返回的list结果集生成Java ...

  3. 探讨read的返回值的三种情况

    http://blog.chinaunix.net/uid-23629988-id-3035613.html 今天探讨一个很看似简单的API “read”的返回值问题.read的返回值有哪几个值?每个 ...

  4. js为空的几种情况

    1.null,对象不存在 var ii= document.getElementById("id"); alert(ii); 当前页面不存在id对象 2. undefined  v ...

  5. 自己遇到的ajax调用ashx文件无法获取返回值的一种情况

    无法获取返回值的ashx文件大致如下: public void ProcessRequest (HttpContext context) { context.Response.ContentType ...

  6. android DownloadManager.getInputStream返回null的一种情况

    将下载操作的代码放到一个新的子线程中来执行.

  7. 【Spring Data 系列学习】Spring Data JPA 自定义查询,分页,排序,条件查询

    Spring Boot Jpa 默认提供 CURD 的方法等方法,在日常中往往时无法满足我们业务的要求,本章节通过自定义简单查询案例进行讲解. 快速上手 项目中的pom.xml.application ...

  8. Left join查询为空

    这两个查询,上面查询返回空,下面能正常返回记录 两个表结构: left join 没有匹配上得到的b.dates,b.game_id和b.uid都是null值,在按b.dates=20200317 a ...

  9. SQL 中的多条件查询

    在应用程序开发中,多条件查询是个经常遇到的情况,最简单最麻烦的方法是把所有的可能情况都考虑到,但是无疑是繁琐的,而且很容易漏掉可能的情形,下面是SQL语句实现多条件查询的情况 select * fro ...

随机推荐

  1. Nginx反向代理Odoo并转为https

    生成证书文件 生成自签名证书,并放在指定位置 $ openssl req -x509 -days 3650 -subj '/CN=odoo.youyun.com/' -nodes -newkey rs ...

  2. Taro踩坑记录一: swiper组件pagestate定制,swiperChange中setState导致组件不能滚动。

    import Taro, { Component } from '@tarojs/taro'; import { Swiper, SwiperItem, Image, View } from '@ta ...

  3. 查看pip install *.whl 支持的文件版本

    import pip._internalprint(pip._internal.pep425tags.get_supported())[('cp37', 'cp37m', 'manylinux2010 ...

  4. SPSS超详细操作:分层回归(hierarchical multiple regression)

    SPSS超详细操作:分层回归(hierarchical multiple regression) 1.问题与数据 最大携氧能力(maximal aerobic capacity, VO2max)是评价 ...

  5. JDK源码阅读--String

    public final class String implements java.io.Serializable, Comparable<String>, CharSequence St ...

  6. DataLossError (see above for traceback): file is too short to be an sstable [[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_F

    DataLossError (see above for traceback): file is too short to be an sstable [[Node: save/RestoreV2 = ...

  7. Solr+HBase

  8. 出现 cannot download, $GOPATH not set. For more details see: go help gopath

    执行安装 sudo go get github.com/nsf/gocode 提示: cannot download, $GOPATH not set. For more details see: g ...

  9. session中load()跟get()的区别

    1.相同点:Session.load/get方法均可以根据指定的实体类和id从数据库读取记录,并返回与之对应的实体对象. 2.区别在于: (1)如果未能发现符合条件的记录,get方法返回null,而l ...

  10. Django项目:CMDB(服务器硬件资产自动采集系统)--10--06CMDB测试Linux系统采集硬件数据的命令05

    cd /py/AutoClient/bin python3 auto-client.py /usr/local/python3/bin/pip install requests python3 aut ...