Hibernate 查询
模糊查询
1
String strSQL="from Classes as a where a.classno like :name";
Query query = session.createQuery(strSQL);
query.setString("name", "%"+OId+"%");
List result=query.list();
2
List result=session.createQuery("from Classes as a where a.classno like '%"+OId+"%'").list();
3
List result=session.createQuery("from Classes as a where a.classno like :name").setParameter("pid",OId).list();
/**
* 根据条件查询
*/
public List byStudent(String sno,String sname,String address,int classid){
Criteria critera = getSession().createCriteria(Student.class);//里面是一个class对象,而不是HQL
if(sno!=null&&!sno.equals("")){//如果equals在前面的话,容易报 nullpoint 异常
critera.add(Restrictions.like("sno", sno,MatchMode.ANYWHERE));
//如果是进行模糊查询的话,就要加一个参数,MatchMode.ANYWHERE ,否则就不能进行模糊查询
}
if(sname!=null&&!sname.equals("")){
critera.add(Restrictions.like("sname", sname,MatchMode.ANYWHERE));
}
if(address!=null&&!address.equals("")){
critera.add(Restrictions.like("address", address,MatchMode.ANYWHERE));
}
if(classid!=0){
critera.add(Restrictions.eq("classes.cid",new Integer(classid)));
}
return critera.list();
}