hibernate如何简单的去复杂的高级查询

搞个简单的例子:假如有个商品表,有根据producid,productName,productType,productAdress,等多个条件差,只需这样做就可以了

在通过request请求获得这几个条件的参数值,或者用form获得也可以了,获得后

Product p=new Product();

p.setProductid(...);

p.setProductName(...);

p.setProductType(...);

p.setProductAdress(...);

----这样是按照条件查询

Session session =HibernateSessionFactory.getSession();
<wbr><wbr><br><wbr><wbr>List&lt;Products&gt; list=<br><wbr><wbr><wbr>session.createCriteria(Products.class)<br><wbr><wbr><wbr>.add(Example.create(<wbr> p<wbr><wbr> ))<br><wbr><wbr><wbr><wbr>.list();<wbr><wbr><br><wbr><wbr><br> HibernateSessionFactory.closeSession();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

----如果是按照条件模糊查询可以这样

Session session =HibernateSessionFactory.getSession();
<wbr><wbr><br><wbr>Criteria c=<br><wbr><wbr><wbr>session.createCriteria(Products.class)</wbr></wbr></wbr></wbr></wbr></wbr>

.c.add(Restrictions.like("productid", "%"+p.getProductid()+"%"))

.c.add(Restrictions.like("productName", "%"+p.getProductName()+"%"))

.c.add(Restrictions.like("productType","%"+p.getProductType()+"%"))

.c.add(Restrictions.like("productAdress","%"+p.getProductAdress+"%"))

.list();<wbr><wbr><br> HibernateSessionFactory.closeSession();</wbr></wbr>

你可能感兴趣的:(Hibernate)