static void query(String name){
Session s = null;
try{
s = HiberanteUtil.getSession();
String hql = "from User as user where user.name = ?";
Query query = s.createQuery(hql);
query.setString(0,name);
List<User> list = query.list();
for(User user:list){
System.out.println(user.getName());
}
}finally{
if(s!=null){
s.close();
}
}
}
String hql = "from User as user where user.name = :name";
query.setString("name",name);
- Query常用方法
- setFirstResult()//分页,起始页
- setMaxResult()//分页,行数
- setXXX()//设置查询参数
- list()//可以查询多条记录
- uniqueResult()//当只有一条记录时使用