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

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

原来写的是,

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. HTML - 图片标签相关

    <html> <head></head> <body> <!-- src : 图片的路径 (本地资源路径, 网络资源路径) title : 图片的 ...

  2. Ubuntu下安装和配置Apache2,小编觉得挺不错的,现在就分享给大家

    本篇文章主要介绍了详解Ubuntu下安装和配置Apache2,小编觉得挺不错的,现在就分享给大家,也给大家做个参考.有兴趣的朋友可以了解一下.(http://xz.8682222.com) 在Ubun ...

  3. csp-s模拟测试61砖块, 数字,甜圈题解

    题面:https://www.cnblogs.com/Juve/articles/11626350.html 砖块: 直接模拟即可,map统计被覆盖的次数 #include<iostream&g ...

  4. Zuul的过滤器

    过滤器类型与请求生命周期:         Zuul中定义了4种标准过滤器类型,这些过滤器类型对应于请求的典型生命周期         PRE: 这种过滤器在请求被路由之前调用.可利用这种过滤器实现身 ...

  5. iOS开发UITableView的动画cell

    1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...

  6. SpringMVC处理请求的大致流程是怎么样的

    SpringMVC请求处理流程   Spring MVC请求处理架构图:   1.用户首先发送请求到前端控制器Dispatcher Servlet,  2.在doDispath这个方法中会为请求找到对 ...

  7. vue elment.style样式修改(第三方组件自生成元素)

    参考:https://blog.csdn.net/dcxia89/article/details/80402490         https://blog.csdn.net/jianglibo102 ...

  8. shell下时间日期的加减乘除运算

    首先我们先来说说什么是shell下的时间戳: 自1970年1月1日(00:00:00 UTC/GMT)以来的秒数.它也被称为Unix时间戳(Unix Timestam.Unix epoch.POSIX ...

  9. mac版pycharm的字体和行间距设置

  10. 分布式事务中间件 Fescar—RM 模块源码解读

    前言 在SOA.微服务架构流行的年代,许多复杂业务上需要支持多资源占用场景,而在分布式系统中因为某个资源不足而导致其它资源占用回滚的系统设计一直是个难点.我所在的团队也遇到了这个问题,为解决这个问题上 ...