javaweb对商品名的模糊查询
- 摘要:packagedream.ourshopping.persistence;importjava.util.List;importorg.apache.log4j.Logger;importorg.hibernate.LockMode;importorg.hibernate.Query;importorg.hibernate.Transaction;importorg.hibernate.criterion.Example;importdream.ourshopping.domain
- 标签:javaweb Web Java 模糊查询
class="dp-j">
- package?dream.ourshopping.persistence;???
- ???
- import?java.util.List;???
- ???
- import?org.apache.log4j.Logger;???
- import?org.hibernate.LockMode;???
- import?org.hibernate.Query;???
- import?org.hibernate.Transaction;???
- import?org.hibernate.criterion.Example;???
- ???
- import?dream.ourshopping.domain.Product;???
- ???
- ?
- ?
- ?
- ?
- ?
- ???
- public?class?ProductDAO?extends?BaseHibernateDAO?{???
- ???
- ????private?static?Logger?log?=?Logger.getLogger(ProductDAO.class.getName());???
- ???
- ??????
- ????public?static?final?String?SORTID?=?"sortid";???
- ???
- ????public?static?final?String?NAME?=?"name";???
- ???
- ????public?static?final?String?PRICE?=?"price";???
- ???
- ????public?static?final?String?SALEPRICE?=?"saleprice";???
- ???
- ????public?static?final?String?DESCRIPT?=?"descript";???
- ???
- ????public?static?final?String?CONTENTS?=?"contents";???
- ???
- ????public?static?final?String?SALECOUNT?=?"salecount";???
- ???
- ????public?static?final?String?IMAGE?=?"image";???
- ???
- ????public?void?save(Product?transientInstance)?{???
- ????????log.debug("添加商品");???
- ????????try?{???
- ????????????getSession().save(transientInstance);???
- ????????????log.debug("添加商品");???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("添加商品出错",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?void?delete(int?id)?{???
- ????????log.debug("根据ID删除商品?"+id);???
- ????????try?{???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"delete?Product?p?where?p.id="?+?id);???
- ????????????q.executeUpdate();???
- ????????????log.debug("根据ID删除商品成功!");???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("根据ID删除商品出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?Product?findById(java.lang.Integer?id)?{???
- ????????log.debug("根据ID查询商品"?+?id);???
- ????????try?{???
- ????????????Product?instance?=?(Product)?getSession().get(???
- ????????????????????"dream.ourshopping.domain.Product",?id);???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("根据ID查询商品出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findBySortId(java.lang.Integer?id,?int?begin,?int?size)?{???
- ????????log.debug("根据类别ID查询商品?"?+?id);???
- ????????try?{???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"from?Product?p?where?p.sort.id="?+?id);???
- ????????????q.setFirstResult(begin);???
- ????????????q.setMaxResults(size);???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("根据类别ID查询商品出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findBySortIdCount(java.lang.Integer?id)?{???
- ????????log.debug("根据类别ID查询商品总数?"?+?id);???
- ????????try?{???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"select?count(*)?from?Product?p?where?p.sort.id="?+?id);???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("根据类别ID查询商品总数出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findLikeByName(java.lang.String?name,?int?beginnum,?int?size)?{???
- ????????log.debug("根据商品名称模糊查询商品?"?+?name);???
- ????????try?{???
- ????????????name?=?"'%"?+?name?+?"%'";???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"from?Product?p?where?p.name?like?"?+?name);???
- ????????????q.setFirstResult(beginnum);???
- ????????????q.setMaxResults(size);???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("根据商品名称模糊查询商品出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findLikeByNameCount(java.lang.String?name)?{???
- ????????log.debug("根据商品名称模糊查询商品总数?"?+?name);???
- ????????try?{???
- ????????????name?=?"'%"?+?name?+?"%'";???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"select?count(*)?from?Product?p?where?p.name?like?"?+?name);???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("根据商品名称模糊查询商品总数出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findHot()?{???
- ????????log.debug("查询销售排行前四名商品?");???
- ????????try?{???
- ???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"from?Product?sort?order?by?sort.salecount?desc");???
- ????????????q.setFirstResult(0);???
- ????????????q.setMaxResults(4);???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("查询销售排行前四名商品出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findMaxId()?{???
- ????????log.debug("查询商品最大ID?");???
- ????????try?{???
- ???
- ????????????Query?q?=?getSession()???
- ????????????????????.createQuery("select?max(p.id)from?Product?p");???
- ???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("查询商品最大ID出错!",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ???????
- ????public?List?findByLikeName(String?name,int?beginnum)?{???
- ????????log.debug("商品模糊查询?"+name);???
- ????????try?{???
- ????????????name="'%"+name+"%'";???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"from?Product?p?where?p.name?like?"+name?);???
- ????????????q.setFirstResult(beginnum);???
- ????????????q.setMaxResults(10);???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("商品模糊查询",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???????
- ????public?List?findCountByLikeName(String?name)?{???
- ????????log.debug("商品模糊查询?"+name);???
- ????????try?{???
- ????????????name="'%"+name+"%'";???
- ????????????Query?q?=?getSession().createQuery(???
- ????????????????????"select?count(*)?from?Product?p?where?p.name?like?"+name?);???
- ????????????List?instance?=?q.list();???
- ????????????return?instance;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("商品模糊查询",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???????
- ???
- ????public?List?findByExample(Product?instance)?{???
- ????????log.debug("finding?Product?instance?by?example");???
- ????????try?{???
- ????????????List?results?=?getSession().createCriteria(???
- ????????????????????"dream.ourshopping.domain.Product").add(???
- ????????????????????Example.create(instance)).list();???
- ????????????log.debug("find?by?example?successful,?result?size:?"???
- ????????????????????+?results.size());???
- ????????????return?results;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("find?by?example?failed",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findByProperty(String?propertyName,?Object?value)?{???
- ????????log.debug("finding?Product?instance?with?property:?"?+?propertyName???
- ????????????????+?",?value:?"?+?value);???
- ????????try?{???
- ????????????String?queryString?=?"from?Product?as?model?where?model."???
- ????????????????????+?propertyName?+?"=??";???
- ????????????Query?queryObject?=?getSession().createQuery(queryString);???
- ????????????queryObject.setParameter(0,?value);???
- ????????????return?queryObject.list();???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("find?by?property?name?failed",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?List?findBySortid(Object?sortid)?{???
- ????????return?findByProperty(SORTID,?sortid);???
- ????}???
- ???
- ????public?List?findByName(Object?name)?{???
- ????????return?findByProperty(NAME,?name);???
- ????}???
- ???
- ????public?Product?merge(Product?detachedInstance)?{???
- ????????log.debug("merging?Product?instance");???
- ????????try?{???
- ????????????Transaction?ts?=?getSession().beginTransaction();???
- ????????????Product?result?=?(Product)?getSession().merge(detachedInstance);???
- ????????????ts.commit();???
- ????????????log.debug("merge?successful");???
- ????????????return?result;???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("merge?failed",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?void?attachDirty(Product?instance)?{???
- ????????log.debug("attaching?dirty?Product?instance");???
- ????????try?{???
- ????????????getSession().saveOrUpdate(instance);???
- ????????????log.debug("attach?successful");???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("attach?failed",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- ???
- ????public?void?attachClean(Product?instance)?{???
- ????????log.debug("attaching?clean?Product?instance");???
- ????????try?{???
- ????????????getSession().lock(instance,?LockMode.NONE);???
- ????????????log.debug("attach?successful");???
- ????????}?catch?(RuntimeException?re)?{???
- ????????????log.error("attach?failed",?re);???
- ????????????throw?re;???
- ????????}???
- ????}???
- }??