zoukankan      html  css  js  c++  java
  • HibernateTemplate类的使用 (转)

    HibernateTemplate类的使用 (转)

     

     

    HibernateTemplate 提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作, Spring 2.0 更增加对命名 SQL 查询的支持,也增加对分页的支持。大部分情况下,使用 Hibernate 的常规用法,就可完成大多数 DAO 对象的 CRUD 操作。下面是 HibernateTemplate 的常用方法简介:

    q      void delete(Object entity) :删除指定持久化实例

    q      deleteAll(Collection entities) :删除集合内全部持久化类实例

    q      find(String queryString) :根据 HQL 查询字符串来返回实例集合

    q      findByNamedQuery(String queryName) :根据命名查询返回实例集合

    q      get(Class entityClass, Serializable id) :根据主键加载特定持久化类的实例

    q      save(Object entity) :保存新的实例

    q      saveOrUpdate(Object entity) :根据实例状态,选择保存或者更新

    q      update(Object entity) :更新实例的状态,要求 entity 是持久状态

    q      setMaxResults(int maxResults) :设置分页的大小

    1.  /**   
    2. * 向数据库添加一条对应于一个业务对象实例的记录   
    3.  
    4. * @param entity   
    5. * 业务对象实例   
    6.       */    
    7. public Entity create(Entity entity) throws DaoException {    
    8. try {    
    9. getHibernateTemplate().save(entity);    
    10. return entity;    
    11. catch (DataAccessException e) {    
    12.             throw new DaoException("保存 " + entity.getClass().getName()    
    13. " 实例到数据库失败", e);    
    14. }    
    15. }    
    16.   
    17. /**   
    18. * 向数据库更新一条对应于一个业务对象实例的记录   
    19.  
    20. * @param entity   
    21.       * 业务对象实例   
    22.       */    
    23. public void update(Entity entity) throws DaoException {    
    24. try {    
    25. getHibernateTemplate().update(entity);    
    26. catch (DataAccessException e) {    
    27.             throw new DaoException("更新 " + entity.getClass().getName()    
    28. " 实例到数据库失败", e);    
    29. }    
    30. }    
    31.   
    32. /**   
    33. * 从数据库删除一条对应于一个业务对象的记录   
    34.  
    35. * @param entity   
    36. * 业务对象实例   
    37.       */    
    38. public void delete(Entity entity) throws DaoException {    
    39. try {    
    40. getHibernateTemplate().delete(entity);    
    41. catch (DataAccessException e) {    
    42.             throw new DaoException("从数据库删除 " + entity.getClass().getName()    
    43. " 实例失败", e);    
    44. }    
    45. }    
    46.   
    47. /**   
    48. * 从数据库删除所有对应于一个业务对象的记录   
    49.  
    50. * @param clazz   
    51. * 指定类型的业务对象   
    52.       */    
    53. public void deleteAll(Class clazz) throws DaoException {    
    54. try {    
    55. List result = getHibernateTemplate().loadAll(clazz);    
    56. getHibernateTemplate().deleteAll(result);    
    57. catch (DataAccessException e) {    
    58. log.error("从数据库删除 " + clazz.getName() + " 的所有记录失败", e);    
    59. throw new DaoException("从数据库删除 " + clazz.getName() + " 的所有记录失败", e);    
    60. }    
    61. }    
    62.   
    63. public void deleteAll(Collection entities) throws DaoException {    
    64. try {    
    65. getHibernateTemplate().deleteAll(entities);    
    66. catch(DataAccessException e) {    
    67. throw new DaoException(e);    
    68. }    
    69. }    
    70.   
    71. /**   
    72. * 根据关键字从数据库加载指定类型的业务对象。   
    73.  
    74. * @param clazz   
    75. * 业务对象的Class   
    76. * @param keyName   
    77. * 指定关键字对应的字段名称   
    78. * @param keyValue   
    79. * 指定关键字的值   
    80. * @return <ul>   
    81. * <li>当关键字唯一并存在该记录时,返回该记录对应的业务对象</li>   
    82. * <li>当关键字不唯一,返回查询结果的第一条记录所对应的业务对象</li>   
    83. * <li>当不存在该记录时,返回null</li>   
    84.       */    
    85. public Object loadByKey(Class clazz, String keyName, Object keyValue)    
    86. throws DaoException {    
    87. try {    
    88. List result = getHibernateTemplate().find(    
    89. "from " + clazz.getName() + " where " + keyName + " = ?",    
    90. keyValue);    
    91. if (result != null && result.size() > 0) {    
    92. return result.get(0);    
    93. else {    
    94. return null;    
    95. }    
    96. catch (DataAccessException e) {    
    97.             throw new DaoException("加载 " + keyName + " 为 " + keyValue + " 的 "    
    98. + clazz.getName() + " 实例失败", e);    
    99. }    
    100. }    
    101.   
    102. /**   
    103. * 从数据库加载指定类型的业务对象的所有记录。   
    104.  
    105. * @param clazz   
    106. * 业务对象的Class   
    107. * @return 返回数据库中对应该业务对象的所有记录的集合   
    108.       */    
    109. public List loadAll(Class clazz) throws DaoException {    
    110. try {    
    111. return getHibernateTemplate().loadAll(clazz);    
    112. catch (DataAccessException e) {    
    113.             throw new DaoException("加载所有 " + clazz.getName() + " 实例时失败", e);    
    114. }    
    115. }    
    116.   
    117. /**   
    118. * 根据查询语句查询数据库并返回查询结果所包含的业务对象集合。   
    119.  
    120. * @param queryString   
    121. * 指定查询语句   
    122. * @return 返回查询结果包含的业务对象集合   
    123.       */    
    124. public List find(String queryString) throws DaoException {    
    125. try {    
    126. return getHibernateTemplate().find(queryString);    
    127. catch (DataAccessException e) {    
    128.             throw new DaoException("执行查询 " + queryString + " 失败", e);    
    129. }    
    130. }    
    131.   
    132. /**   
    133. * 根据带一个参数的查询语句查询数据库并返回查询结果所包含的业务对象集合。   
    134.  
    135. * @param queryString   
    136. * 指定查询语句   
    137. * @param param   
    138. * 指定所带参数   
    139. * @return 返回查询结果包含的业务对象集合   
    140.       */    
    141. public List find(String queryString, Object param) throws DaoException {    
    142. try {    
    143. return getHibernateTemplate().find(queryString, param);    
    144. catch (DataAccessException e) {    
    145.             throw new DaoException("执行参数为 " + param + " 的查询 " + queryString    
    146. " 失败", e);    
    147. }    
    148. }    
    149.   
    150. /**   
    151. * 根据带多个参数的查询语句查询数据库并返回查询结果所包含的业务对象集合。   
    152.  
    153. * @param queryString   
    154. * 指定查询语句   
    155. * @param params   
    156. * 指定参数数组   
    157. * @return 返回查询结果包含的业务对象集合   
    158.       */    
    159. public List find(String queryString, Object[] params) throws DaoException {    
    160. try {    
    161. return getHibernateTemplate().find(queryString, params);    
    162. catch (DataAccessException e) {    
    163. StringBuffer paramString = new StringBuffer("");    
    164. for (int i = 0; i < params.length; i++) {    
    165. paramString.append(params[i]);    
    166. paramString.append(" ");    
    167. }    
    168.             throw new DaoException("执行参数为 " + paramString + "的查询 "    
    169. + queryString + " 失败", e);    
    170. }    
    171. }    
    172.   
    173. /**   
    174. * 根据已定义的查询语句查询数据库并返回查询结果所包含的业务对象集合。   
    175.  
    176. * @param queryName   
    177. * 已定义查询语句的名称   
    178. * @return 返回查询结果包含的业务对象集合   
    179.       */    
    180. public List findByNamedQuery(String queryName) throws DaoException {    
    181. try {    
    182. return getHibernateTemplate().findByNamedQuery(queryName);    
    183. catch (DataAccessException e) {    
    184.             throw new DaoException("执行命名为 " + queryName + " 的查询失败");    
    185. }    
    186. }    
    187.   
    188. /**   
    189. * 根据已定义的带一个参数的查询语句查询数据库并返回查询结果所包含的业务对象集合。   
    190.  
    191. * @param queryName   
    192. * 已定义查询语句的名称   
    193. * @param param   
    194. * 指定的参数   
    195. * @return 返回查询结果包含的业务对象集合   
    196.       */    
    197. public List findByNamedQuery(String queryName, Object param)    
    198. throws DaoException {    
    199. try {    
    200. return getHibernateTemplate().findByNamedQuery(queryName, param);    
    201. catch (DataAccessException e) {    
    202.             throw new DaoException("执行参数为 " + param + " 命名为 " + queryName    
    203. " 的查询失败");    
    204. }    
    205. }    
    206.   
    207. /**   
    208. * 根据已定义的带多个参数的查询语句查询数据库并返回查询结果所包含的业务对象集合。   
    209.  
    210. * @param queryName   
    211. * 已定义查询语句的名称   
    212. * @param params   
    213. * 指定的参数数组   
    214. * @return 返回查询结果包含的业务对象集合   
    215.       */    
    216. public List findByNameQuery(String queryName, Object[] params)    
    217. throws DaoException {    
    218. try {    
    219. return getHibernateTemplate().findByNamedQuery(queryName, params);    
    220. catch (DataAccessException e) {    
    221. StringBuffer paramString = new StringBuffer("");    
    222. for (int i = 0; i < params.length; i++) {    
    223. paramString.append(params[i]);    
    224. paramString.append(" ");    
    225. }    
    226.             throw new DaoException("执行参数为 " + paramString + "命名为 " + queryName    
    227. " 的查询失败");    
    228. }    
    229. }  
     
  • 相关阅读:
    jquery实现选项卡(两句即可实现)
    常用特效积累
    jquery学习笔记
    idong常用js总结
    织梦添加幻灯片的方法
    LeetCode "Copy List with Random Pointer"
    LeetCode "Remove Nth Node From End of List"
    LeetCode "Sqrt(x)"
    LeetCode "Construct Binary Tree from Inorder and Postorder Traversal"
    LeetCode "Construct Binary Tree from Preorder and Inorder Traversal"
  • 原文地址:https://www.cnblogs.com/yaoxing92/p/3022301.html
Copyright © 2011-2022 走看看