Hibernate的HQL查询

static List query(String name){

    Session session = null;

   

     try{

          s = HibernateUtil.getSession();

          String hql = "from User as user where user.name =?";

          Query query = s.createQuery(hql); //查询语句

          query.setString(0,name);//参数传递,下标从零开始、、

//        query.setString("name",name);//String hql = "from User as user where user.name =:name";

//        query.setFirstResult(1) //分页

//        query.setMaxResults(10) //   分页     

//         User u = (User)query.uniqueResult(); //当确定查询结果只有一个对象,多个时会报错

  

          List list =  qyery.list();//执行查询 

     }finally{

             if(s!=null)

             s.close();

     }

     return list

}

相关推荐