首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

mybatis泛型Dao参照一

2013-07-04 
mybatis泛型Dao参考一泛型DAO接口的设计博客分类:?结构设计Java代码??import?java.io.Serializable??impo

mybatis泛型Dao参考一
泛型DAO接口的设计博客分类:

    ?结构设计Java代码??mybatis泛型Dao参照一
    1. import?java.io.Serializable;??
    2. import?java.util.List;??
    3. ??
    4. /**?
    5. ?*?GenericDao?DAO层泛型接口,定义基本的DAO功能?
    6. ?*?@author?wl?
    7. ?*?@since?0.1?
    8. ?*?@param?<T>????实体类?
    9. ?*?@param?<PK>????主键类,必须实现Serializable接口?
    10. ?*??
    11. ?*?@see?com.thinkon.commons.dao.GenericIbatisDao?
    12. ?*/??
    13. public?abstract?interface?GenericDao<T,?PK?extends?Serializable>?{??
    14. ????/**?
    15. ?????*?插入一个实体(在数据库INSERT一条记录)?
    16. ?????*?@param?entity?实体对象?
    17. ?????*/??
    18. ????public?abstract?void?insert(T?entity);??
    19. ??????
    20. ????/**?
    21. ?????*?修改一个实体对象(UPDATE一条记录)?
    22. ?????*?@param?entity?实体对象?
    23. ?????*?@return?修改的对象个数,正常情况=1?
    24. ?????*/??
    25. ????public?abstract?int?update(T?entity);??
    26. ??????
    27. ????/**?
    28. ?????*?修改符合条件的记录?
    29. ?????*?<p>此方法特别适合于一次性把多条记录的某些字段值设置为新值(定值)的情况,比如修改符合条件的记录的状态字段</p>?
    30. ?????*?<p>此方法的另一个用途是把一条记录的个别字段的值修改为新值(定值),此时要把条件设置为该记录的主键</p>?
    31. ?????*?@param?param?用于产生SQL的参数值,包括WHERE条件、目标字段和新值等?
    32. ?????*?@return?修改的记录个数,用于判断修改是否成功?
    33. ?????*/??
    34. ????public?abstract?int?update(DynamicSqlParameter?param);??
    35. ??????
    36. ????/**?
    37. ?????*?按主键删除记录?
    38. ?????*?@param?primaryKey?主键对象?
    39. ?????*?@return?删除的对象个数,正常情况=1?
    40. ?????*/??
    41. ????public?abstract?int?delete(PK?primaryKey);??
    42. ??
    43. ????/**?
    44. ?????*?删除符合条件的记录?
    45. ?????*?<p><strong>此方法一定要慎用,如果条件设置不当,可能会删除有用的记录!</strong></p>?
    46. ?????*?@param?param?用于产生SQL的参数值,包括WHERE条件(其他参数内容不起作用)?
    47. ?????*?@return?
    48. ?????*/??
    49. ????public?abstract?int?delete(DynamicSqlParameter?param);??
    50. ??????
    51. ????/**?
    52. ?????*?清空表,比delete具有更高的效率,而且是从数据库中物理删除(delete是逻辑删除,被删除的记录依然占有空间)?
    53. ?????*?<p><strong>此方法一定要慎用!</strong></p>?
    54. ?????*?@return?
    55. ?????*/??
    56. ????public?abstract?int?truncate();??
    57. ??????
    58. ????/**?
    59. ?????*?查询整表总记录数?
    60. ?????*?@return?整表总记录数?
    61. ?????*/??
    62. ????public?abstract?int?count();??
    63. ??????
    64. ????/**?
    65. ?????*?查询符合条件的记录数?
    66. ?????*?@param?param?查询条件参数,包括WHERE条件(其他参数内容不起作用)。此参数设置为null,则相当于count()?
    67. ?????*?@return?
    68. ?????*/??
    69. ????public?abstract?int?count(DynamicSqlParameter?param);??
    70. ??
    71. ????/**?
    72. ?????*?按主键取记录?
    73. ?????*?@param?primaryKey?主键值?
    74. ?????*?@return?记录实体对象,如果没有符合主键条件的记录,则返回null?
    75. ?????*/??
    76. ????public?abstract?T?get(PK?primaryKey);??
    77. ??
    78. ????/**?
    79. ?????*?按主键取记录?
    80. ?????*?@param?primaryKey?主键值?
    81. ?????*?@return?记录实体对象,如果没有符合主键条件的记录,则?throw?DataAccessException?
    82. ?????*/??
    83. ????public?abstract?T?load(PK?primaryKey)?throws?DataAccessException;??
    84. ??
    85. ????/**?
    86. ?????*?取全部记录?
    87. ?????*?@return?全部记录实体对象的List?
    88. ?????*/??
    89. ????public?abstract?List<T>?select();??
    90. ??????
    91. ????/**?
    92. ?????*?按条件查询记录?
    93. ?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件?
    94. ?????*?@return?符合条件记录的实体对象的List?
    95. ?????*/??
    96. ????public?abstract?List<T>?select(DynamicSqlParameter?param);??
    97. ??????
    98. ????/**?
    99. ?????*?按条件查询记录,并处理成分页结果?
    100. ?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件?
    101. ?????*?@return?PaginationResult对象,包括(符合条件的)总记录数、页实体对象List等?
    102. ?????*/??
    103. ????public?abstract?PaginationResult<T>?selectPagination(DynamicSqlParameter?param);??
    104. ??????
    105. ????/**?
    106. ?????*?按条件查询记录,并把有外键关联的字段的关联对象也处理出来?
    107. ?????*?<p>此方法是为了避免1+N而设置的</p>?
    108. ?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件<strong>(不包括外键关联表条件)</strong>?
    109. ?????*?@return?符合条件记录的实体对象的List?
    110. ?????*/??
    111. ????public?abstract?List<T>?selectFk(DynamicSqlParameter?param);??
    112. ??????
    113. ????/**?
    114. ?????*?按条件查询记录,并把有外键关联的字段的关联对象也处理出来,并处理成分页结果?
    115. ?????*?<p>此方法是为了避免1+N而设置的</p>?
    116. ?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件<strong>(不包括外键关联表条件)</strong>?
    117. ?????*?@return?PaginationResult对象,包括(符合条件的)总记录数、页实体对象List等?
    118. ?????*/??
    119. ????public?abstract?PaginationResult<T>?selectFkPagination(DynamicSqlParameter?param);??
    120. ??????
    121. ????/**?
    122. ?????*?批量插入?
    123. ?????*?@param?list?
    124. ?????*/??
    125. ????public?abstract?void?batchInsert(final?List<T>?list);??
    126. ??????
    127. ????/**?
    128. ?????*?批量修改?
    129. ?????*?@param?list?
    130. ?????*/??
    131. ????public?abstract?void?batchUpdate(final?List<T>?list);??
    132. ??????
    133. ????/**?
    134. ?????*?批量删除?
    135. ?????*?@param?list?
    136. ?????*/??
    137. ????public?abstract?void?batchDelete(final?List<PK>?list);??
    138. }??

    ?2、泛型接口的实现类(其他DAO的基类),以iBatis实现:

    Java代码??mybatis泛型Dao参照一
    1. import?java.io.Serializable;??
    2. import?java.sql.SQLException;??
    3. import?java.util.List;??
    4. ??
    5. import?org.springframework.orm.ibatis.SqlMapClientCallback;??
    6. import?org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;??
    7. ??
    8. import?com.ibatis.sqlmap.client.SqlMapExecutor;??
    9. ??
    10. /**?
    11. ?*?iBatis?DAO层泛型基类,实现了基本的DAO功能?利用了Spring的DaoSupport功能?
    12. ?*??
    13. ?*?@author?wl?
    14. ?*?@since?0.1?
    15. ?*?@param?<T>?
    16. ?*????????????实体类?
    17. ?*?@param?<PK>?
    18. ?*????????????主键类,必须实现Serializable接口?
    19. ?*??
    20. ?*?@see?com.thinkon.commons.dao.GenericDao?
    21. ?*?@see?org.springframework.orm.ibatis.support.SqlMapClientDaoSupport?
    22. ?*/??
    23. public?abstract?class?GenericIbatisDao<T,?PK?extends?Serializable>?extends??
    24. ????????SqlMapClientDaoSupport?implements?GenericDao<T,?PK>?{??
    25. ??
    26. ????//?sqlmap.xml定义文件中对应的sqlid??
    27. ????public?static?final?String?SQLID_INSERT?=?"insert";??
    28. ????public?static?final?String?SQLID_UPDATE?=?"update";??
    29. ????public?static?final?String?SQLID_UPDATE_PARAM?=?"updateParam";??
    30. ????public?static?final?String?SQLID_DELETE?=?"delete";??
    31. ????public?static?final?String?SQLID_DELETE_PARAM?=?"deleteParam";??
    32. ????public?static?final?String?SQLID_TRUNCATE?=?"truncate";??
    33. ????public?static?final?String?SQLID_SELECT?=?"select";??
    34. ????public?static?final?String?SQLID_SELECT_PK?=?"selectPk";??
    35. ????public?static?final?String?SQLID_SELECT_PARAM?=?"selectParam";??
    36. ????public?static?final?String?SQLID_SELECT_FK?=?"selectFk";??
    37. ????public?static?final?String?SQLID_COUNT?=?"count";??
    38. ????public?static?final?String?SQLID_COUNT_PARAM?=?"countParam";??
    39. ??
    40. ????private?String?sqlmapNamespace?=?"";??
    41. ??
    42. ????/**?
    43. ?????*?sqlmapNamespace,对应sqlmap.xml中的命名空间?
    44. ?????*??
    45. ?????*?@return?
    46. ?????*/??
    47. ????public?String?getSqlmapNamespace()?{??
    48. ????????return?sqlmapNamespace;??
    49. ????}??
    50. ??
    51. ????/**?
    52. ?????*?sqlmapNamespace的设置方法,可以用于spring注入?
    53. ?????*??
    54. ?????*?@param?sqlmapNamespace?
    55. ?????*/??
    56. ????public?void?setSqlmapNamespace(String?sqlmapNamespace)?{??
    57. ????????this.sqlmapNamespace?=?sqlmapNamespace;??
    58. ????}??
    59. ??
    60. ????/**?
    61. ?????*?数据库方言,缺省为MYSQL?
    62. ?????*/??
    63. ????private?String?dbDialect?=?"MYSQL";??
    64. ??
    65. ????/**?
    66. ?????*?数据库方言dbDialect的get方法?
    67. ?????*??
    68. ?????*?@return?
    69. ?????*/??
    70. ????public?String?getDbDialect()?{??
    71. ????????return?dbDialect;??
    72. ????}??
    73. ??
    74. ????/**?
    75. ?????*?数据库方言dbDialect的set方法?
    76. ?????*??
    77. ?????*?@return?
    78. ?????*/??
    79. ????public?void?setDbDialect(String?dbDialect)?{??
    80. ????????if?(dbDialect?==?null??
    81. ????????????????||?(!dbDialect.equals("MYSQL")?&&?!dbDialect.equals("ORACLE")))??
    82. ????????????throw?new?DaoException("错误的数据库方言设置:本系统只支持MYSQL和ORACLE");??
    83. ????????this.dbDialect?=?dbDialect;??
    84. ????}??
    85. ??
    86. ????public?int?count()?{??
    87. ????????Integer?count?=?(Integer)?getSqlMapClientTemplate().queryForObject(??
    88. ????????????????sqlmapNamespace?+?"."?+?SQLID_COUNT);??
    89. ????????return?count.intValue();??
    90. ????}??
    91. ??
    92. ????public?int?count(DynamicSqlParameter?param)?{??
    93. ????????Integer?count?=?(Integer)?getSqlMapClientTemplate().queryForObject(??
    94. ????????????????sqlmapNamespace?+?"."?+?SQLID_COUNT_PARAM,?param);??
    95. ????????return?count.intValue();??
    96. ????}??
    97. ??
    98. ????public?int?delete(PK?primaryKey)?{??
    99. ????????int?rows?=?getSqlMapClientTemplate().delete(??
    100. ????????????????sqlmapNamespace?+?"."?+?SQLID_DELETE,?primaryKey);??
    101. ????????try?{??
    102. ????????????getSqlMapClientTemplate().getSqlMapClient().startBatch();??
    103. ????????}?catch?(SQLException?e)?{??
    104. ????????????//?TODO?Auto-generated?catch?block??
    105. ????????????e.printStackTrace();??
    106. ????????}??
    107. ????????return?rows;??
    108. ????}??
    109. ??
    110. ????public?int?delete(DynamicSqlParameter?param)?{??
    111. ????????int?rows?=?getSqlMapClientTemplate().delete(??
    112. ????????????????sqlmapNamespace?+?"."?+?SQLID_DELETE_PARAM,?param);??
    113. ????????return?rows;??
    114. ????}??
    115. ??
    116. ????public?int?truncate()?{??
    117. ????????int?rows?=?getSqlMapClientTemplate().delete(??
    118. ????????????????sqlmapNamespace?+?"."?+?SQLID_TRUNCATE);??
    119. ????????return?rows;??
    120. ????}??
    121. ??
    122. ????public?T?get(PK?primaryKey)?{??
    123. ????????return?(T)?getSqlMapClientTemplate().queryForObject(??
    124. ????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PK,?primaryKey);??
    125. ????}??
    126. ??
    127. ????public?void?insert(T?entity)?{??
    128. ????????getSqlMapClientTemplate().insert(sqlmapNamespace?+?"."?+?SQLID_INSERT,??
    129. ????????????????entity);??
    130. ????}??
    131. ??
    132. ????public?T?load(PK?primaryKey)?throws?DaoException?{??
    133. ????????Object?o?=?getSqlMapClientTemplate().queryForObject(??
    134. ????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PK,?primaryKey);??
    135. ????????if?(o?==?null)??
    136. ????????????throw?new?DataAccessException("数据查询异常:无法查询出主键数据");??
    137. ????????return?(T)?o;??
    138. ????}??
    139. ??
    140. ????public?List<T>?select()?{??
    141. ????????return?getSqlMapClientTemplate().queryForList(??
    142. ????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT);??
    143. ????}??
    144. ??
    145. ????public?List<T>?select(DynamicSqlParameter?param)?{??
    146. ????????return?getSqlMapClientTemplate().queryForList(??
    147. ????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PARAM,?param);??
    148. ????}??
    149. ??
    150. ????public?PaginationResult<T>?selectPagination(DynamicSqlParameter?param)?{??
    151. ????????if?(param?!=?null)??
    152. ????????????param.setDbDialect(this.dbDialect);??
    153. ??
    154. ????????PaginationResult<T>?result?=?new?PaginationResult<T>();??
    155. ????????int?count?=?count(param);??
    156. ????????result.setTotalSize(count);??
    157. ????????if?(count?>?0)?{??
    158. ????????????List<T>?data?=?getSqlMapClientTemplate().queryForList(??
    159. ????????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PARAM,?param);??
    160. ????????????result.setData(data);??
    161. ????????}??
    162. ??
    163. ????????return?result;??
    164. ????}??
    165. ??
    166. ????public?List<T>?selectFk(DynamicSqlParameter?param)?{??
    167. ????????return?getSqlMapClientTemplate().queryForList(??
    168. ????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_FK,?param);??
    169. ????}??
    170. ??
    171. ????public?PaginationResult<T>?selectFkPagination(DynamicSqlParameter?param)?{??
    172. ????????if?(param?!=?null)??
    173. ????????????param.setDbDialect(this.dbDialect);??
    174. ????????PaginationResult<T>?result?=?new?PaginationResult<T>();??
    175. ????????int?count?=?count(param);??
    176. ????????result.setTotalSize(count);??
    177. ????????if?(count?>?0)?{??
    178. ????????????List<T>?data?=?getSqlMapClientTemplate().queryForList(??
    179. ????????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_FK,?param);??
    180. ????????????result.setData(data);??
    181. ????????}??
    182. ??
    183. ????????return?result;??
    184. ????}??
    185. ??
    186. ????public?int?update(T?entity)?{??
    187. ????????return?getSqlMapClientTemplate().update(??
    188. ????????????????sqlmapNamespace?+?"."?+?SQLID_UPDATE,?entity);??
    189. ????}??
    190. ??
    191. ????public?int?update(DynamicSqlParameter?param)?{??
    192. ????????if?(param?==?null?||?param.getUpdateValueList()?==?null)??
    193. ????????????throw?new?ParameterException(??
    194. ????????????????????"参数设置错误:使用带参数的update必须设定update的column!");??
    195. ??
    196. ????????return?getSqlMapClientTemplate().update(??
    197. ????????????????sqlmapNamespace?+?"."?+?SQLID_UPDATE_PARAM,?param);??
    198. ????}??
    199. ??
    200. ????public?void?batchInsert(final?List<T>?list){??
    201. ????????SqlMapClientCallback?callback?=?new?SqlMapClientCallback()?{??????????
    202. ????????????public?Object?doInSqlMapClient(SqlMapExecutor?executor)?throws?SQLException?{??????????????
    203. ????????????????executor.startBatch();??????????????
    204. ????????????????for?(T?member?:?list)?{??????????????????
    205. ????????????????????executor.insert(sqlmapNamespace?+?"."?+?SQLID_INSERT,?member);??????????????
    206. ????????????????}??????????????
    207. ????????????????executor.executeBatch();??????????????
    208. ????????????????return?null;??????????
    209. ????????????}??????
    210. ????????};??????
    211. ????????this.getSqlMapClientTemplate().execute(callback);??????
    212. ????}??
    213. ??
    214. ????public?void?batchUpdate(final?List<T>?list){??
    215. ????????SqlMapClientCallback?callback?=?new?SqlMapClientCallback()?{??????????
    216. ????????????public?Object?doInSqlMapClient(SqlMapExecutor?executor)?throws?SQLException?{??????????????
    217. ????????????????executor.startBatch();??????????????
    218. ????????????????for?(T?member?:?list)?{??????????????????
    219. ????????????????????executor.update(sqlmapNamespace?+?"."?+?SQLID_UPDATE,?member);??????????????
    220. ????????????????}??????????????
    221. ????????????????executor.executeBatch();??????????????
    222. ????????????????return?null;??????????
    223. ????????????}??????
    224. ????????};??????
    225. ????????this.getSqlMapClientTemplate().execute(callback);??????
    226. ????}??
    227. ??
    228. ????public?void?batchDelete(final?List<PK>?list){??
    229. ????????SqlMapClientCallback?callback?=?new?SqlMapClientCallback()?{??????????
    230. ????????????public?Object?doInSqlMapClient(SqlMapExecutor?executor)?throws?SQLException?{??????????????
    231. ????????????????executor.startBatch();??????????????
    232. ????????????????for?(PK?member?:?list)?{??????????????????
    233. ????????????????????executor.delete(sqlmapNamespace?+?"."?+?SQLID_DELETE,?member);??????????????
    234. ????????????????}??????????????
    235. ????????????????executor.executeBatch();??????????????
    236. ????????????????return?null;??????????
    237. ????????????}??????
    238. ????????};??????
    239. ????????this.getSqlMapClientTemplate().execute(callback);??????
    240. ????}??
    241. }??

    ?3、某实体的DAO接口:

    Java代码??mybatis泛型Dao参照一
    1. public?interface?UserDao?extends?GenericDao<User,Integer>?{??
    2. //通用方法从GenericDao继承??
    3. //这里可以定义针对此类的特定方法??
    4. }??

    ?4、某实体的DAO实现:

    Java代码??mybatis泛型Dao参照一
    1. public?class?UserDaoImpl?extends?GenericIbatisDao<User,Integer>?implements?UserDao?{??
    2. }??

    ?

热点排行