------------------HQL----------------------------------------------------------
{
Session session = getCurrentSession();
Query query = session.createQuery("from tableA as a where a.name=: name and a.age =:age");
//如果想执行sql语句,这里需要写成session.creatSQLQuery("sql语句");
query.setString("name","Tom");
query.setInteger("age",20);
List list = query.list();
当执行list()方法时,Hibernate将执行以下sql语句:
select * from tableA where name="Tom" and age =20;
}
protected Session getCurrentSession(){
SessionFactory sessionFactory;
return sessionFactory.getCurrentSession();
}
============================================================
也可以用方法链的方式访问Query接口,这样会更加简洁
List result = session.createQuery(".......").setString("name","Tom").setInteger("age",20).list();
---------------QBC-----Criteria方法查询-----------------------------------
Criteria criteria = getCurrentSession().createCriteria(persistentClass);
Criterion criterion1=Restrictions.like("name","T%");
Criterion criterion2=Restrictions.eq("age",new Integer(21));
criteria.add(criterion1);
criteria.add(criterion2);
List result = criteria.list();
hibernate-hql查询
最新推荐文章于 2018-09-15 20:51:35 发布