Galam_Chan 2017-04-09 02:11 采纳率: 0%
浏览 1156

HQL 模糊查询奇怪问题

代码如下
    public List<Students> selectByCondition(String sid,String sname,int pageNum, int pageSize) {
    // TODO Auto-generated method stub
    Transaction tx = null;
    String hql = "";
    List<Students> students=null;
    try {
        Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
        tx = session.beginTransaction();
        hql = "from Students s where 1=1";
        if (sname != null && !"".equals(sname))
            hql += " and  s.sname like '%" + sname + "%'";
        if (sid != null && !"".equals(sid))
            hql += " and   s.sid = '" + sid + "'";
        System.out.println("hql语句:"+hql);
        int fromIndex=Page.countOffset(pageSize,pageNum);
        Query query = session.createQuery(hql);
        query.setMaxResults(fromIndex);
        query.setFirstResult(pageSize);
        students = query.list();
        System.out.println("开始"+fromIndex+"--结束"+pageSize);
        System.out.println("学生查询结果:"+students);
        tx.commit();
    } catch (Exception ex) {
        tx.rollback();
        ex.printStackTrace();
    }
    return students;
}
查询记录数
public int selectCountByCondition(String sid,String sname) {
    // TODO Auto-generated method stub
    Transaction tx = null;
    String hql = "";
    int count = 0;
    try {
        Session session = MyHibernateSessionFactory.getSessionFactory().getCurrentSession();
        tx = session.beginTransaction();
        hql = "select count(*) from Students where 1=1";
        if (sname != null && !"".equals(sname))
            hql += " and  sname like '%" + sname + "%'";
        if (sid!= null && !"".equals(sid))
            hql += "and   sid = '" + sid + "'";
        Query query = session.createQuery(hql);
        count = ((Number) query.uniqueResult()).intValue();
        System.out.println(count);
        tx.commit();
    } catch (Exception ex) {
        tx.rollback();
        ex.printStackTrace();
    }
    return count;
}
很奇怪,就是我用如下测试方法却报空,PS:数据库里有对应的数据
    @Test
public void test(){
String sid="13140";
    String sid="";
    String sname="周芷若";
    List<Students> list=new ArrayList<Students>();
    StudentsDAO st=new StudentsDAOImpl();
    int count=0;
    count=st.selectCountByCondition(sid, sname);
    list=st.selectByCondition(sid, sname, 1, count);
    if(list!=null&&list.size()>0){
        for(int i=0;i<=list.size();i++){
            System.out.println("------");
        System.out.println("学号:"+list.get(i).getSid()+"姓名:"+list.get(i).getSname());
    }
    }
}
查得了count=st.selectCountByCondition(sid, sname);
却执行list=st.selectByCondition(sid, sname, 1, count);这句话为[],搞了好久也不知道问什么会这样,之前用过同样方式查询啊,也是将query.list()以类形式放进list。
  • 写回答

1条回答 默认 最新

  • KiterCher 2017-04-10 03:10
    关注

    额,我给你指出几个问题
    你的hql打印出来应该是这样的吧?
    hql = "from Students s where 1=1 and s.sname like '%周芷若%' and s.sid = ' ' ";
    1、拿出来你的list.size()为0;证明你的hql没有问题,但是where条件除了问题,你的数据库里面sid的值应该是null吧?如果是null,那么你的hql就是错误的,你的hql 应该这样写
    hql = "from Students s where 1=1 and s.sname like '%周芷若%' and ISNULL(s.sid) " ; 这样才是对的,不能以 sid = "" 来匹配;
    2、还有就是你的关于hql的类型问题,既然你的hql语句是可以追加的,那就应该使用StringBuffer.append()啊,使用String不适合;

    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码