首页
诗词
字典
板报
句子
名言
友答
励志
学校
网站地图
软件架构设计
软件开发
软件架构设计
CVS SVN
VSTS
PowerDesigner
Rational
软件测试
当前位置:
首页
>
教程频道
>
软件管理
>
软件架构设计
>
mybatis泛型Dao参照一
2013-07-04
mybatis泛型Dao参考一泛型DAO接口的设计博客分类:?结构设计Java代码??import?java.io.Serializable??impo
mybatis泛型Dao参考一
泛型DAO接口的设计
博客分类:
?结构设计Java代码??
import?java.io.Serializable;??
import?java.util.List;??
??
/**?
?*?GenericDao?DAO层泛型接口,定义基本的DAO功能?
?*?@author?wl?
?*?@since?0.1?
?*?@param?<T>????实体类?
?*?@param?<PK>????主键类,必须实现Serializable接口?
?*??
?*?@see?com.thinkon.commons.dao.GenericIbatisDao?
?*/??
public?abstract?interface?GenericDao<T,?PK?extends?Serializable>?{??
????/**?
?????*?插入一个实体(在数据库INSERT一条记录)?
?????*?@param?entity?实体对象?
?????*/??
????public?abstract?void?insert(T?entity);??
??????
????/**?
?????*?修改一个实体对象(UPDATE一条记录)?
?????*?@param?entity?实体对象?
?????*?@return?修改的对象个数,正常情况=1?
?????*/??
????public?abstract?int?update(T?entity);??
??????
????/**?
?????*?修改符合条件的记录?
?????*?<p>此方法特别适合于一次性把多条记录的某些字段值设置为新值(定值)的情况,比如修改符合条件的记录的状态字段</p>?
?????*?<p>此方法的另一个用途是把一条记录的个别字段的值修改为新值(定值),此时要把条件设置为该记录的主键</p>?
?????*?@param?param?用于产生SQL的参数值,包括WHERE条件、目标字段和新值等?
?????*?@return?修改的记录个数,用于判断修改是否成功?
?????*/??
????public?abstract?int?update(DynamicSqlParameter?param);??
??????
????/**?
?????*?按主键删除记录?
?????*?@param?primaryKey?主键对象?
?????*?@return?删除的对象个数,正常情况=1?
?????*/??
????public?abstract?int?delete(PK?primaryKey);??
??
????/**?
?????*?删除符合条件的记录?
?????*?<p><strong>此方法一定要慎用,如果条件设置不当,可能会删除有用的记录!</strong></p>?
?????*?@param?param?用于产生SQL的参数值,包括WHERE条件(其他参数内容不起作用)?
?????*?@return?
?????*/??
????public?abstract?int?delete(DynamicSqlParameter?param);??
??????
????/**?
?????*?清空表,比delete具有更高的效率,而且是从数据库中物理删除(delete是逻辑删除,被删除的记录依然占有空间)?
?????*?<p><strong>此方法一定要慎用!</strong></p>?
?????*?@return?
?????*/??
????public?abstract?int?truncate();??
??????
????/**?
?????*?查询整表总记录数?
?????*?@return?整表总记录数?
?????*/??
????public?abstract?int?count();??
??????
????/**?
?????*?查询符合条件的记录数?
?????*?@param?param?查询条件参数,包括WHERE条件(其他参数内容不起作用)。此参数设置为null,则相当于count()?
?????*?@return?
?????*/??
????public?abstract?int?count(DynamicSqlParameter?param);??
??
????/**?
?????*?按主键取记录?
?????*?@param?primaryKey?主键值?
?????*?@return?记录实体对象,如果没有符合主键条件的记录,则返回null?
?????*/??
????public?abstract?T?get(PK?primaryKey);??
??
????/**?
?????*?按主键取记录?
?????*?@param?primaryKey?主键值?
?????*?@return?记录实体对象,如果没有符合主键条件的记录,则?throw?DataAccessException?
?????*/??
????public?abstract?T?load(PK?primaryKey)?throws?DataAccessException;??
??
????/**?
?????*?取全部记录?
?????*?@return?全部记录实体对象的List?
?????*/??
????public?abstract?List<T>?select();??
??????
????/**?
?????*?按条件查询记录?
?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件?
?????*?@return?符合条件记录的实体对象的List?
?????*/??
????public?abstract?List<T>?select(DynamicSqlParameter?param);??
??????
????/**?
?????*?按条件查询记录,并处理成分页结果?
?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件?
?????*?@return?PaginationResult对象,包括(符合条件的)总记录数、页实体对象List等?
?????*/??
????public?abstract?PaginationResult<T>?selectPagination(DynamicSqlParameter?param);??
??????
????/**?
?????*?按条件查询记录,并把有外键关联的字段的关联对象也处理出来?
?????*?<p>此方法是为了避免1+N而设置的</p>?
?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件<strong>(不包括外键关联表条件)</strong>?
?????*?@return?符合条件记录的实体对象的List?
?????*/??
????public?abstract?List<T>?selectFk(DynamicSqlParameter?param);??
??????
????/**?
?????*?按条件查询记录,并把有外键关联的字段的关联对象也处理出来,并处理成分页结果?
?????*?<p>此方法是为了避免1+N而设置的</p>?
?????*?@param?param?查询条件参数,包括WHERE条件、分页条件、排序条件<strong>(不包括外键关联表条件)</strong>?
?????*?@return?PaginationResult对象,包括(符合条件的)总记录数、页实体对象List等?
?????*/??
????public?abstract?PaginationResult<T>?selectFkPagination(DynamicSqlParameter?param);??
??????
????/**?
?????*?批量插入?
?????*?@param?list?
?????*/??
????public?abstract?void?batchInsert(final?List<T>?list);??
??????
????/**?
?????*?批量修改?
?????*?@param?list?
?????*/??
????public?abstract?void?batchUpdate(final?List<T>?list);??
??????
????/**?
?????*?批量删除?
?????*?@param?list?
?????*/??
????public?abstract?void?batchDelete(final?List<PK>?list);??
}??
?2、泛型接口的实现类(其他DAO的基类),以iBatis实现:
Java代码??
import?java.io.Serializable;??
import?java.sql.SQLException;??
import?java.util.List;??
??
import?org.springframework.orm.ibatis.SqlMapClientCallback;??
import?org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;??
??
import?com.ibatis.sqlmap.client.SqlMapExecutor;??
??
/**?
?*?iBatis?DAO层泛型基类,实现了基本的DAO功能?利用了Spring的DaoSupport功能?
?*??
?*?@author?wl?
?*?@since?0.1?
?*?@param?<T>?
?*????????????实体类?
?*?@param?<PK>?
?*????????????主键类,必须实现Serializable接口?
?*??
?*?@see?com.thinkon.commons.dao.GenericDao?
?*?@see?org.springframework.orm.ibatis.support.SqlMapClientDaoSupport?
?*/??
public?abstract?class?GenericIbatisDao<T,?PK?extends?Serializable>?extends??
????????SqlMapClientDaoSupport?implements?GenericDao<T,?PK>?{??
??
????//?sqlmap.xml定义文件中对应的sqlid??
????public?static?final?String?SQLID_INSERT?=?"insert";??
????public?static?final?String?SQLID_UPDATE?=?"update";??
????public?static?final?String?SQLID_UPDATE_PARAM?=?"updateParam";??
????public?static?final?String?SQLID_DELETE?=?"delete";??
????public?static?final?String?SQLID_DELETE_PARAM?=?"deleteParam";??
????public?static?final?String?SQLID_TRUNCATE?=?"truncate";??
????public?static?final?String?SQLID_SELECT?=?"select";??
????public?static?final?String?SQLID_SELECT_PK?=?"selectPk";??
????public?static?final?String?SQLID_SELECT_PARAM?=?"selectParam";??
????public?static?final?String?SQLID_SELECT_FK?=?"selectFk";??
????public?static?final?String?SQLID_COUNT?=?"count";??
????public?static?final?String?SQLID_COUNT_PARAM?=?"countParam";??
??
????private?String?sqlmapNamespace?=?"";??
??
????/**?
?????*?sqlmapNamespace,对应sqlmap.xml中的命名空间?
?????*??
?????*?@return?
?????*/??
????public?String?getSqlmapNamespace()?{??
????????return?sqlmapNamespace;??
????}??
??
????/**?
?????*?sqlmapNamespace的设置方法,可以用于spring注入?
?????*??
?????*?@param?sqlmapNamespace?
?????*/??
????public?void?setSqlmapNamespace(String?sqlmapNamespace)?{??
????????this.sqlmapNamespace?=?sqlmapNamespace;??
????}??
??
????/**?
?????*?数据库方言,缺省为MYSQL?
?????*/??
????private?String?dbDialect?=?"MYSQL";??
??
????/**?
?????*?数据库方言dbDialect的get方法?
?????*??
?????*?@return?
?????*/??
????public?String?getDbDialect()?{??
????????return?dbDialect;??
????}??
??
????/**?
?????*?数据库方言dbDialect的set方法?
?????*??
?????*?@return?
?????*/??
????public?void?setDbDialect(String?dbDialect)?{??
????????if?(dbDialect?==?null??
????????????????||?(!dbDialect.equals("MYSQL")?&&?!dbDialect.equals("ORACLE")))??
????????????throw?new?DaoException("错误的数据库方言设置:本系统只支持MYSQL和ORACLE");??
????????this.dbDialect?=?dbDialect;??
????}??
??
????public?int?count()?{??
????????Integer?count?=?(Integer)?getSqlMapClientTemplate().queryForObject(??
????????????????sqlmapNamespace?+?"."?+?SQLID_COUNT);??
????????return?count.intValue();??
????}??
??
????public?int?count(DynamicSqlParameter?param)?{??
????????Integer?count?=?(Integer)?getSqlMapClientTemplate().queryForObject(??
????????????????sqlmapNamespace?+?"."?+?SQLID_COUNT_PARAM,?param);??
????????return?count.intValue();??
????}??
??
????public?int?delete(PK?primaryKey)?{??
????????int?rows?=?getSqlMapClientTemplate().delete(??
????????????????sqlmapNamespace?+?"."?+?SQLID_DELETE,?primaryKey);??
????????try?{??
????????????getSqlMapClientTemplate().getSqlMapClient().startBatch();??
????????}?catch?(SQLException?e)?{??
????????????//?TODO?Auto-generated?catch?block??
????????????e.printStackTrace();??
????????}??
????????return?rows;??
????}??
??
????public?int?delete(DynamicSqlParameter?param)?{??
????????int?rows?=?getSqlMapClientTemplate().delete(??
????????????????sqlmapNamespace?+?"."?+?SQLID_DELETE_PARAM,?param);??
????????return?rows;??
????}??
??
????public?int?truncate()?{??
????????int?rows?=?getSqlMapClientTemplate().delete(??
????????????????sqlmapNamespace?+?"."?+?SQLID_TRUNCATE);??
????????return?rows;??
????}??
??
????public?T?get(PK?primaryKey)?{??
????????return?(T)?getSqlMapClientTemplate().queryForObject(??
????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PK,?primaryKey);??
????}??
??
????public?void?insert(T?entity)?{??
????????getSqlMapClientTemplate().insert(sqlmapNamespace?+?"."?+?SQLID_INSERT,??
????????????????entity);??
????}??
??
????public?T?load(PK?primaryKey)?throws?DaoException?{??
????????Object?o?=?getSqlMapClientTemplate().queryForObject(??
????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PK,?primaryKey);??
????????if?(o?==?null)??
????????????throw?new?DataAccessException("数据查询异常:无法查询出主键数据");??
????????return?(T)?o;??
????}??
??
????public?List<T>?select()?{??
????????return?getSqlMapClientTemplate().queryForList(??
????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT);??
????}??
??
????public?List<T>?select(DynamicSqlParameter?param)?{??
????????return?getSqlMapClientTemplate().queryForList(??
????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PARAM,?param);??
????}??
??
????public?PaginationResult<T>?selectPagination(DynamicSqlParameter?param)?{??
????????if?(param?!=?null)??
????????????param.setDbDialect(this.dbDialect);??
??
????????PaginationResult<T>?result?=?new?PaginationResult<T>();??
????????int?count?=?count(param);??
????????result.setTotalSize(count);??
????????if?(count?>?0)?{??
????????????List<T>?data?=?getSqlMapClientTemplate().queryForList(??
????????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_PARAM,?param);??
????????????result.setData(data);??
????????}??
??
????????return?result;??
????}??
??
????public?List<T>?selectFk(DynamicSqlParameter?param)?{??
????????return?getSqlMapClientTemplate().queryForList(??
????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_FK,?param);??
????}??
??
????public?PaginationResult<T>?selectFkPagination(DynamicSqlParameter?param)?{??
????????if?(param?!=?null)??
????????????param.setDbDialect(this.dbDialect);??
????????PaginationResult<T>?result?=?new?PaginationResult<T>();??
????????int?count?=?count(param);??
????????result.setTotalSize(count);??
????????if?(count?>?0)?{??
????????????List<T>?data?=?getSqlMapClientTemplate().queryForList(??
????????????????????sqlmapNamespace?+?"."?+?SQLID_SELECT_FK,?param);??
????????????result.setData(data);??
????????}??
??
????????return?result;??
????}??
??
????public?int?update(T?entity)?{??
????????return?getSqlMapClientTemplate().update(??
????????????????sqlmapNamespace?+?"."?+?SQLID_UPDATE,?entity);??
????}??
??
????public?int?update(DynamicSqlParameter?param)?{??
????????if?(param?==?null?||?param.getUpdateValueList()?==?null)??
????????????throw?new?ParameterException(??
????????????????????"参数设置错误:使用带参数的update必须设定update的column!");??
??
????????return?getSqlMapClientTemplate().update(??
????????????????sqlmapNamespace?+?"."?+?SQLID_UPDATE_PARAM,?param);??
????}??
??
????public?void?batchInsert(final?List<T>?list){??
????????SqlMapClientCallback?callback?=?new?SqlMapClientCallback()?{??????????
????????????public?Object?doInSqlMapClient(SqlMapExecutor?executor)?throws?SQLException?{??????????????
????????????????executor.startBatch();??????????????
????????????????for?(T?member?:?list)?{??????????????????
????????????????????executor.insert(sqlmapNamespace?+?"."?+?SQLID_INSERT,?member);??????????????
????????????????}??????????????
????????????????executor.executeBatch();??????????????
????????????????return?null;??????????
????????????}??????
????????};??????
????????this.getSqlMapClientTemplate().execute(callback);??????
????}??
??
????public?void?batchUpdate(final?List<T>?list){??
????????SqlMapClientCallback?callback?=?new?SqlMapClientCallback()?{??????????
????????????public?Object?doInSqlMapClient(SqlMapExecutor?executor)?throws?SQLException?{??????????????
????????????????executor.startBatch();??????????????
????????????????for?(T?member?:?list)?{??????????????????
????????????????????executor.update(sqlmapNamespace?+?"."?+?SQLID_UPDATE,?member);??????????????
????????????????}??????????????
????????????????executor.executeBatch();??????????????
????????????????return?null;??????????
????????????}??????
????????};??????
????????this.getSqlMapClientTemplate().execute(callback);??????
????}??
??
????public?void?batchDelete(final?List<PK>?list){??
????????SqlMapClientCallback?callback?=?new?SqlMapClientCallback()?{??????????
????????????public?Object?doInSqlMapClient(SqlMapExecutor?executor)?throws?SQLException?{??????????????
????????????????executor.startBatch();??????????????
????????????????for?(PK?member?:?list)?{??????????????????
????????????????????executor.delete(sqlmapNamespace?+?"."?+?SQLID_DELETE,?member);??????????????
????????????????}??????????????
????????????????executor.executeBatch();??????????????
????????????????return?null;??????????
????????????}??????
????????};??????
????????this.getSqlMapClientTemplate().execute(callback);??????
????}??
}??
?3、某实体的DAO接口:
Java代码??
public?interface?UserDao?extends?GenericDao<User,Integer>?{??
//通用方法从GenericDao继承??
//这里可以定义针对此类的特定方法??
}??
?4、某实体的DAO实现:
Java代码??
public?class?UserDaoImpl?extends?GenericIbatisDao<User,Integer>?implements?UserDao?{??
}??
?
查看更多
下一篇
本文网址:
https://www.reader8.com/jiaocheng/20130704/2120967.html
读书人精选
热点排行
【收录】将Spring用来高并发环境的隐忧
细谈Ehcache页面缓存的应用
AOP的JDK and CGLib 动态署理 示例
tomcat施用log4j分割日志的配置方法
struts2表单中文乱码有关问题
spring3.0支持restful范例
spring配备quartz 多任务
Spring的四种配置方式
Java 实现下线翻译功能 调用微软Bing API
getHibernateTemplate() 卡死的有关问题