javaweb对商品名的模糊查询_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > javaweb对商品名的模糊查询

javaweb对商品名的模糊查询

 2013/8/2 19:08:52  LiaoJuncai  程序员俱乐部  我要评论(0)
  • 摘要: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">
  1. package?dream.ourshopping.persistence;???
  2. ???
  3. import?java.util.List;???
  4. ???
  5. import?org.apache.log4j.Logger;???
  6. import?org.hibernate.LockMode;???
  7. import?org.hibernate.Query;???
  8. import?org.hibernate.Transaction;???
  9. import?org.hibernate.criterion.Example;???
  10. ???
  11. import?dream.ourshopping.domain.Product;???
  12. ???
  13. /**??
  14. ?*?Data?access?object?(DAO)?for?domain?model?class?Product.??
  15. ?*???
  16. ?*?@see?com.study.hibernate.Product??
  17. ?*?@author?MyEclipse?-?Hibernate?Tools??
  18. ?*/???
  19. public?class?ProductDAO?extends?BaseHibernateDAO?{???
  20. ???
  21. ????private?static?Logger?log?=?Logger.getLogger(ProductDAO.class.getName());???
  22. ???
  23. ????//?property?constants???
  24. ????public?static?final?String?SORTID?=?"sortid";???
  25. ???
  26. ????public?static?final?String?NAME?=?"name";???
  27. ???
  28. ????public?static?final?String?PRICE?=?"price";???
  29. ???
  30. ????public?static?final?String?SALEPRICE?=?"saleprice";???
  31. ???
  32. ????public?static?final?String?DESCRIPT?=?"descript";???
  33. ???
  34. ????public?static?final?String?CONTENTS?=?"contents";???
  35. ???
  36. ????public?static?final?String?SALECOUNT?=?"salecount";???
  37. ???
  38. ????public?static?final?String?IMAGE?=?"image";???
  39. ???
  40. ????public?void?save(Product?transientInstance)?{???
  41. ????????log.debug("添加商品");???
  42. ????????try?{???
  43. ????????????getSession().save(transientInstance);???
  44. ????????????log.debug("添加商品");???
  45. ????????}?catch?(RuntimeException?re)?{???
  46. ????????????log.error("添加商品出错",?re);???
  47. ????????????throw?re;???
  48. ????????}???
  49. ????}???
  50. ???
  51. ????public?void?delete(int?id)?{???
  52. ????????log.debug("根据ID删除商品?"+id);???
  53. ????????try?{???
  54. ????????????Query?q?=?getSession().createQuery(???
  55. ????????????????????"delete?Product?p?where?p.id="?+?id);???
  56. ????????????q.executeUpdate();???
  57. ????????????log.debug("根据ID删除商品成功!");???
  58. ????????}?catch?(RuntimeException?re)?{???
  59. ????????????log.error("根据ID删除商品出错!",?re);???
  60. ????????????throw?re;???
  61. ????????}???
  62. ????}???
  63. ???
  64. ????public?Product?findById(java.lang.Integer?id)?{???
  65. ????????log.debug("根据ID查询商品"?+?id);???
  66. ????????try?{???
  67. ????????????Product?instance?=?(Product)?getSession().get(???
  68. ????????????????????"dream.ourshopping.domain.Product",?id);???
  69. ????????????return?instance;???
  70. ????????}?catch?(RuntimeException?re)?{???
  71. ????????????log.error("根据ID查询商品出错!",?re);???
  72. ????????????throw?re;???
  73. ????????}???
  74. ????}???
  75. ???
  76. ????public?List?findBySortId(java.lang.Integer?id,?int?begin,?int?size)?{???
  77. ????????log.debug("根据类别ID查询商品?"?+?id);???
  78. ????????try?{???
  79. ????????????Query?q?=?getSession().createQuery(???
  80. ????????????????????"from?Product?p?where?p.sort.id="?+?id);???
  81. ????????????q.setFirstResult(begin);???
  82. ????????????q.setMaxResults(size);???
  83. ????????????List?instance?=?q.list();???
  84. ????????????return?instance;???
  85. ????????}?catch?(RuntimeException?re)?{???
  86. ????????????log.error("根据类别ID查询商品出错!",?re);???
  87. ????????????throw?re;???
  88. ????????}???
  89. ????}???
  90. ???
  91. ????public?List?findBySortIdCount(java.lang.Integer?id)?{???
  92. ????????log.debug("根据类别ID查询商品总数?"?+?id);???
  93. ????????try?{???
  94. ????????????Query?q?=?getSession().createQuery(???
  95. ????????????????????"select?count(*)?from?Product?p?where?p.sort.id="?+?id);???
  96. ????????????List?instance?=?q.list();???
  97. ????????????return?instance;???
  98. ????????}?catch?(RuntimeException?re)?{???
  99. ????????????log.error("根据类别ID查询商品总数出错!",?re);???
  100. ????????????throw?re;???
  101. ????????}???
  102. ????}???
  103. ???
  104. ????public?List?findLikeByName(java.lang.String?name,?int?beginnum,?int?size)?{???
  105. ????????log.debug("根据商品名称模糊查询商品?"?+?name);???
  106. ????????try?{???
  107. ????????????name?=?"'%"?+?name?+?"%'";???
  108. ????????????Query?q?=?getSession().createQuery(???
  109. ????????????????????"from?Product?p?where?p.name?like?"?+?name);???
  110. ????????????q.setFirstResult(beginnum);???
  111. ????????????q.setMaxResults(size);???
  112. ????????????List?instance?=?q.list();???
  113. ????????????return?instance;???
  114. ????????}?catch?(RuntimeException?re)?{???
  115. ????????????log.error("根据商品名称模糊查询商品出错!",?re);???
  116. ????????????throw?re;???
  117. ????????}???
  118. ????}???
  119. ???
  120. ????public?List?findLikeByNameCount(java.lang.String?name)?{???
  121. ????????log.debug("根据商品名称模糊查询商品总数?"?+?name);???
  122. ????????try?{???
  123. ????????????name?=?"'%"?+?name?+?"%'";???
  124. ????????????Query?q?=?getSession().createQuery(???
  125. ????????????????????"select?count(*)?from?Product?p?where?p.name?like?"?+?name);???
  126. ????????????List?instance?=?q.list();???
  127. ????????????return?instance;???
  128. ????????}?catch?(RuntimeException?re)?{???
  129. ????????????log.error("根据商品名称模糊查询商品总数出错!",?re);???
  130. ????????????throw?re;???
  131. ????????}???
  132. ????}???
  133. ???
  134. ????public?List?findHot()?{???
  135. ????????log.debug("查询销售排行前四名商品?");???
  136. ????????try?{???
  137. ???
  138. ????????????Query?q?=?getSession().createQuery(???
  139. ????????????????????"from?Product?sort?order?by?sort.salecount?desc");???
  140. ????????????q.setFirstResult(0);???
  141. ????????????q.setMaxResults(4);???
  142. ????????????List?instance?=?q.list();???
  143. ????????????return?instance;???
  144. ????????}?catch?(RuntimeException?re)?{???
  145. ????????????log.error("查询销售排行前四名商品出错!",?re);???
  146. ????????????throw?re;???
  147. ????????}???
  148. ????}???
  149. ???
  150. ????public?List?findMaxId()?{???
  151. ????????log.debug("查询商品最大ID?");???
  152. ????????try?{???
  153. ???
  154. ????????????Query?q?=?getSession()???
  155. ????????????????????.createQuery("select?max(p.id)from?Product?p");???
  156. ???
  157. ????????????List?instance?=?q.list();???
  158. ????????????return?instance;???
  159. ????????}?catch?(RuntimeException?re)?{???
  160. ????????????log.error("查询商品最大ID出错!",?re);???
  161. ????????????throw?re;???
  162. ????????}???
  163. ????}???
  164. ???
  165. ???????
  166. ????public?List?findByLikeName(String?name,int?beginnum)?{???
  167. ????????log.debug("商品模糊查询?"+name);???
  168. ????????try?{???
  169. ????????????name="'%"+name+"%'";???
  170. ????????????Query?q?=?getSession().createQuery(???
  171. ????????????????????"from?Product?p?where?p.name?like?"+name?);???
  172. ????????????q.setFirstResult(beginnum);???
  173. ????????????q.setMaxResults(10);???
  174. ????????????List?instance?=?q.list();???
  175. ????????????return?instance;???
  176. ????????}?catch?(RuntimeException?re)?{???
  177. ????????????log.error("商品模糊查询",?re);???
  178. ????????????throw?re;???
  179. ????????}???
  180. ????}???
  181. ???????
  182. ????public?List?findCountByLikeName(String?name)?{???
  183. ????????log.debug("商品模糊查询?"+name);???
  184. ????????try?{???
  185. ????????????name="'%"+name+"%'";???
  186. ????????????Query?q?=?getSession().createQuery(???
  187. ????????????????????"select?count(*)?from?Product?p?where?p.name?like?"+name?);???
  188. ????????????List?instance?=?q.list();???
  189. ????????????return?instance;???
  190. ????????}?catch?(RuntimeException?re)?{???
  191. ????????????log.error("商品模糊查询",?re);???
  192. ????????????throw?re;???
  193. ????????}???
  194. ????}???
  195. ???????
  196. ???
  197. ????public?List?findByExample(Product?instance)?{???
  198. ????????log.debug("finding?Product?instance?by?example");???
  199. ????????try?{???
  200. ????????????List?results?=?getSession().createCriteria(???
  201. ????????????????????"dream.ourshopping.domain.Product").add(???
  202. ????????????????????Example.create(instance)).list();???
  203. ????????????log.debug("find?by?example?successful,?result?size:?"???
  204. ????????????????????+?results.size());???
  205. ????????????return?results;???
  206. ????????}?catch?(RuntimeException?re)?{???
  207. ????????????log.error("find?by?example?failed",?re);???
  208. ????????????throw?re;???
  209. ????????}???
  210. ????}???
  211. ???
  212. ????public?List?findByProperty(String?propertyName,?Object?value)?{???
  213. ????????log.debug("finding?Product?instance?with?property:?"?+?propertyName???
  214. ????????????????+?",?value:?"?+?value);???
  215. ????????try?{???
  216. ????????????String?queryString?=?"from?Product?as?model?where?model."???
  217. ????????????????????+?propertyName?+?"=??";???
  218. ????????????Query?queryObject?=?getSession().createQuery(queryString);???
  219. ????????????queryObject.setParameter(0,?value);???
  220. ????????????return?queryObject.list();???
  221. ????????}?catch?(RuntimeException?re)?{???
  222. ????????????log.error("find?by?property?name?failed",?re);???
  223. ????????????throw?re;???
  224. ????????}???
  225. ????}???
  226. ???
  227. ????public?List?findBySortid(Object?sortid)?{???
  228. ????????return?findByProperty(SORTID,?sortid);???
  229. ????}???
  230. ???
  231. ????public?List?findByName(Object?name)?{???
  232. ????????return?findByProperty(NAME,?name);???
  233. ????}???
  234. ???
  235. ????public?Product?merge(Product?detachedInstance)?{???
  236. ????????log.debug("merging?Product?instance");???
  237. ????????try?{???
  238. ????????????Transaction?ts?=?getSession().beginTransaction();???
  239. ????????????Product?result?=?(Product)?getSession().merge(detachedInstance);???
  240. ????????????ts.commit();???
  241. ????????????log.debug("merge?successful");???
  242. ????????????return?result;???
  243. ????????}?catch?(RuntimeException?re)?{???
  244. ????????????log.error("merge?failed",?re);???
  245. ????????????throw?re;???
  246. ????????}???
  247. ????}???
  248. ???
  249. ????public?void?attachDirty(Product?instance)?{???
  250. ????????log.debug("attaching?dirty?Product?instance");???
  251. ????????try?{???
  252. ????????????getSession().saveOrUpdate(instance);???
  253. ????????????log.debug("attach?successful");???
  254. ????????}?catch?(RuntimeException?re)?{???
  255. ????????????log.error("attach?failed",?re);???
  256. ????????????throw?re;???
  257. ????????}???
  258. ????}???
  259. ???
  260. ????public?void?attachClean(Product?instance)?{???
  261. ????????log.debug("attaching?clean?Product?instance");???
  262. ????????try?{???
  263. ????????????getSession().lock(instance,?LockMode.NONE);???
  264. ????????????log.debug("attach?successful");???
  265. ????????}?catch?(RuntimeException?re)?{???
  266. ????????????log.error("attach?failed",?re);???
  267. ????????????throw?re;???
  268. ????????}???
  269. ????}???
  270. }??
发表评论
用户名: 匿名