Chinaunix首页 | 论坛 | 博客
  • 博客访问: 30467325
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-11 13:17:44

1、泛型DAO接口定义
  1. import java.io.Serializable;
  2. import java.util.List;

  3. /**
  4.  * GenericDao DAO层泛型接口,定义基本的DAO功能
  5.  * @author wl
  6.  * @since 0.1
  7.  * @param 实体类
  8.  * @param 主键类,必须实现Serializable接口
  9.  *
  10.  * @see com.thinkon.commons.dao.GenericIbatisDao
  11.  */
  12. public abstract interface GenericDao<T, PK extends Serializable> {
  13.     /**
  14.      * 插入一个实体(在数据库INSERT一条记录)
  15.      * @param entity 实体对象
  16.      */
  17.     public abstract void insert(T entity);
  18.     
  19.     /**
  20.      * 修改一个实体对象(UPDATE一条记录)
  21.      * @param entity 实体对象
  22.      * @return 修改的对象个数,正常情况=1
  23.      */
  24.     public abstract int update(T entity);
  25.     
  26.     /**
  27.      * 修改符合条件的记录
  28.      *

    此方法特别适合于一次性把多条记录的某些字段值设置为新值(定值)的情况,比如修改符合条件的记录的状态字段


  29.      *

    此方法的另一个用途是把一条记录的个别字段的值修改为新值(定值),此时要把条件设置为该记录的主键


  30.      * @param param 用于产生SQL的参数值,包括WHERE条件、目标字段和新值等
  31.      * @return 修改的记录个数,用于判断修改是否成功
  32.      */
  33.     public abstract int update(DynamicSqlParameter param);
  34.     
  35.     /**
  36.      * 按主键删除记录
  37.      * @param primaryKey 主键对象
  38.      * @return 删除的对象个数,正常情况=1
  39.      */
  40.     public abstract int delete(PK primaryKey);

  41.     /**
  42.      * 删除符合条件的记录
  43.      *

    此方法一定要慎用,如果条件设置不当,可能会删除有用的记录!


  44.      * @param param 用于产生SQL的参数值,包括WHERE条件(其他参数内容不起作用)
  45.      * @return
  46.      */
  47.     public abstract int delete(DynamicSqlParameter param);
  48.     
  49.     /**
  50.      * 清空表,比delete具有更高的效率,而且是从数据库中物理删除(delete是逻辑删除,被删除的记录依然占有空间)
  51.      *

    此方法一定要慎用!


  52.      * @return
  53.      */
  54.     public abstract int truncate();
  55.     
  56.     /**
  57.      * 查询整表总记录数
  58.      * @return 整表总记录数
  59.      */
  60.     public abstract int count();
  61.     
  62.     /**
  63.      * 查询符合条件的记录数
  64.      * @param param 查询条件参数,包括WHERE条件(其他参数内容不起作用)。此参数设置为null,则相当于count()
  65.      * @return
  66.      */
  67.     public abstract int count(DynamicSqlParameter param);

  68.     /**
  69.      * 按主键取记录
  70.      * @param primaryKey 主键值
  71.      * @return 记录实体对象,如果没有符合主键条件的记录,则返回null
  72.      */
  73.     public abstract T get(PK primaryKey);

  74.     /**
  75.      * 按主键取记录
  76.      * @param primaryKey 主键值
  77.      * @return 记录实体对象,如果没有符合主键条件的记录,则 throw DataAcces***ception
  78.      */
  79.     public abstract T load(PK primaryKey) throws DataAcces***ception;

  80.     /**
  81.      * 取全部记录
  82.      * @return 全部记录实体对象的List
  83.      */
  84.     public abstract List<T> select();
  85.     
  86.     /**
  87.      * 按条件查询记录
  88.      * @param param 查询条件参数,包括WHERE条件、分页条件、排序条件
  89.      * @return 符合条件记录的实体对象的List
  90.      */
  91.     public abstract List<T> select(DynamicSqlParameter param);
  92.     
  93.     /**
  94.      * 按条件查询记录,并处理成分页结果
  95.      * @param param 查询条件参数,包括WHERE条件、分页条件、排序条件
  96.      * @return PaginationResult对象,包括(符合条件的)总记录数、页实体对象List等
  97.      */
  98.     public abstract PaginationResult<T> selectPagination(DynamicSqlParameter param);
  99.     
  100.     /**
  101.      * 按条件查询记录,并把有外键关联的字段的关联对象也处理出来
  102.      *

    此方法是为了避免1+N而设置的


  103.      * @param param 查询条件参数,包括WHERE条件、分页条件、排序条件(不包括外键关联表条件)
  104.      * @return 符合条件记录的实体对象的List
  105.      */
  106.     public abstract List<T> selectFk(DynamicSqlParameter param);
  107.     
  108.     /**
  109.      * 按条件查询记录,并把有外键关联的字段的关联对象也处理出来,并处理成分页结果
  110.      *

    此方法是为了避免1+N而设置的


  111.      * @param param 查询条件参数,包括WHERE条件、分页条件、排序条件(不包括外键关联表条件)
  112.      * @return PaginationResult对象,包括(符合条件的)总记录数、页实体对象List等
  113.      */
  114.     public abstract PaginationResult<T> selectFkPagination(DynamicSqlParameter param);
  115.     
  116.     /**
  117.      * 批量插入
  118.      * @param list
  119.      */
  120.     public abstract void batchInsert(final List<T> list);
  121.     
  122.     /**
  123.      * 批量修改
  124.      * @param list
  125.      */
  126.     public abstract void batchUpdate(final List<T> list);
  127.     
  128.     /**
  129.      * 批量删除
  130.      * @param list
  131.      */
  132.     public abstract void batchDelete(final List<PK> list);
  133. }
 2、泛型接口的实现类(其他DAO的基类),以iBatis实现:
  1. import java.io.Serializable;
  2. import java.sql.SQLException;
  3. import java.util.List;

  4. import org.springframework.orm.ibatis.SqlMapClientCallback;
  5. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

  6. import com.ibatis.sqlmap.client.SqlMapExecutor;

  7. /**
  8.  * iBatis DAO层泛型基类,实现了基本的DAO功能 利用了Spring的DaoSupport功能
  9.  *
  10.  * @author wl
  11.  * @since 0.1
  12.  * @param
  13.  * 实体类
  14.  * @param
  15.  * 主键类,必须实现Serializable接口
  16.  *
  17.  * @see com.thinkon.commons.dao.GenericDao
  18.  * @see org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
  19.  */
  20. public abstract class GenericIbatisDao<T, PK extends Serializable> extends
  21.         SqlMapClientDaoSupport implements GenericDao<T, PK> {

  22.     // sqlmap.xml定义文件中对应的sqlid
  23.     public static final String SQLID_INSERT = "insert";
  24.     public static final String SQLID_UPDATE = "update";
  25.     public static final String SQLID_UPDATE_PARAM = "updateParam";
  26.     public static final String SQLID_DELETE = "delete";
  27.     public static final String SQLID_DELETE_PARAM = "deleteParam";
  28.     public static final String SQLID_TRUNCATE = "truncate";
  29.     public static final String SQLID_SELECT = "select";
  30.     public static final String SQLID_SELECT_PK = "selectPk";
  31.     public static final String SQLID_SELECT_PARAM = "selectParam";
  32.     public static final String SQLID_SELECT_FK = "selectFk";
  33.     public static final String SQLID_COUNT = "count";
  34.     public static final String SQLID_COUNT_PARAM = "countParam";

  35.     private String sqlmapNamespace = "";

  36.     /**
  37.      * sqlmapNamespace,对应sqlmap.xml中的命名空间
  38.      *
  39.      * @return
  40.      */
  41.     public String getSqlmapNamespace() {
  42.         return sqlmapNamespace;
  43.     }

  44.     /**
  45.      * sqlmapNamespace的设置方法,可以用于spring注入
  46.      *
  47.      * @param sqlmapNamespace
  48.      */
  49.     public void setSqlmapNamespace(String sqlmapNamespace) {
  50.         this.sqlmapNamespace = sqlmapNamespace;
  51.     }

  52.     /**
  53.      * 数据库方言,缺省为MYSQL
  54.      */
  55.     private String dbDialect = "MYSQL";

  56.     /**
  57.      * 数据库方言dbDialect的get方法
  58.      *
  59.      * @return
  60.      */
  61.     public String getDbDialect() {
  62.         return dbDialect;
  63.     }

  64.     /**
  65.      * 数据库方言dbDialect的set方法
  66.      *
  67.      * @return
  68.      */
  69.     public void setDbDialect(String dbDialect) {
  70.         if (dbDialect == null
  71.                 || (!dbDialect.equals("MYSQL") && !dbDialect.equals("ORACLE")))
  72.             throw new DaoException("错误的数据库方言设置:本系统只支持MYSQL和ORACLE");
  73.         this.dbDialect = dbDialect;
  74.     }

  75.     public int count() {
  76.         Integer count = (Integer) getSqlMapClientTemplate().queryForObject(
  77.                 sqlmapNamespace + "." + SQLID_COUNT);
  78.         return count.intValue();
  79.     }

  80.     public int count(DynamicSqlParameter param) {
  81.         Integer count = (Integer) getSqlMapClientTemplate().queryForObject(
  82.                 sqlmapNamespace + "." + SQLID_COUNT_PARAM, param);
  83.         return count.intValue();
  84.     }

  85.     public int delete(PK primaryKey) {
  86.         int rows = getSqlMapClientTemplate().delete(
  87.                 sqlmapNamespace + "." + SQLID_DELETE, primaryKey);
  88.         try {
  89.             getSqlMapClientTemplate().getSqlMapClient().startBatch();
  90.         } catch (SQLException e) {
  91.             // TODO Auto-generated catch block
  92.             e.printStackTrace();
  93.         }
  94.         return rows;
  95.     }

  96.     public int delete(DynamicSqlParameter param) {
  97.         int rows = getSqlMapClientTemplate().delete(
  98.                 sqlmapNamespace + "." + SQLID_DELETE_PARAM, param);
  99.         return rows;
  100.     }

  101.     public int truncate() {
  102.         int rows = getSqlMapClientTemplate().delete(
  103.                 sqlmapNamespace + "." + SQLID_TRUNCATE);
  104.         return rows;
  105.     }

  106.     public T get(PK primaryKey) {
  107.         return (T) getSqlMapClientTemplate().queryForObject(
  108.                 sqlmapNamespace + "." + SQLID_SELECT_PK, primaryKey);
  109.     }

  110.     public void insert(T entity) {
  111.         getSqlMapClientTemplate().insert(sqlmapNamespace + "." + SQLID_INSERT,
  112.                 entity);
  113.     }

  114.     public T load(PK primaryKey) throws DaoException {
  115.         Object o = getSqlMapClientTemplate().queryForObject(
  116.                 sqlmapNamespace + "." + SQLID_SELECT_PK, primaryKey);
  117.         if (o == null)
  118.             throw new DataAcces***ception("数据查询异常:无法查询出主键数据");
  119.         return (T) o;
  120.     }

  121.     public List<T> select() {
  122.         return getSqlMapClientTemplate().queryForList(
  123.                 sqlmapNamespace + "." + SQLID_SELECT);
  124.     }

  125.     public List<T> select(DynamicSqlParameter param) {
  126.         return getSqlMapClientTemplate().queryForList(
  127.                 sqlmapNamespace + "." + SQLID_SELECT_PARAM, param);
  128.     }

  129.     public PaginationResult<T> selectPagination(DynamicSqlParameter param) {
  130.         if (param != null)
  131.             param.setDbDialect(this.dbDialect);

  132.         PaginationResult<T> result = new PaginationResult<T>();
  133.         int count = count(param);
  134.         result.setTotalSize(count);
  135.         if (count > 0) {
  136.             List<T> data = getSqlMapClientTemplate().queryForList(
  137.                     sqlmapNamespace + "." + SQLID_SELECT_PARAM, param);
  138.             result.setData(data);
  139.         }

  140.         return result;
  141.     }

  142.     public List<T> selectFk(DynamicSqlParameter param) {
  143.         return getSqlMapClientTemplate().queryForList(
  144.                 sqlmapNamespace + "." + SQLID_SELECT_FK, param);
  145.     }

  146.     public PaginationResult<T> selectFkPagination(DynamicSqlParameter param) {
  147.         if (param != null)
  148.             param.setDbDialect(this.dbDialect);
  149.         PaginationResult<T> result = new PaginationResult<T>();
  150.         int count = count(param);
  151.         result.setTotalSize(count);
  152.         if (count > 0) {
  153.             List<T> data = getSqlMapClientTemplate().queryForList(
  154.                     sqlmapNamespace + "." + SQLID_SELECT_FK, param);
  155.             result.setData(data);
  156.         }

  157.         return result;
  158.     }

  159.     public int update(T entity) {
  160.         return getSqlMapClientTemplate().update(
  161.                 sqlmapNamespace + "." + SQLID_UPDATE, entity);
  162.     }

  163.     public int update(DynamicSqlParameter param) {
  164.         if (param == null || param.getUpdateValueList() == null)
  165.             throw new ParameterException(
  166.                     "参数设置错误:使用带参数的update必须设定update的column!");

  167.         return getSqlMapClientTemplate().update(
  168.                 sqlmapNamespace + "." + SQLID_UPDATE_PARAM, param);
  169.     }

  170.     public void batchInsert(final List<T> list){
  171.         SqlMapClientCallback callback = new SqlMapClientCallback() {
  172.             public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
  173.                 executor.startBatch();
  174.                 for (T member : list) {
  175.                     executor.insert(sqlmapNamespace + "." + SQLID_INSERT, member);
  176.                 }
  177.                 executor.executeBatch();
  178.                 return null;
  179.             }
  180.         };
  181.         this.getSqlMapClientTemplate().execute(callback);
  182.     }

  183.     public void batchUpdate(final List<T> list){
  184.         SqlMapClientCallback callback = new SqlMapClientCallback() {
  185.             public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
  186.                 executor.startBatch();
  187.                 for (T member : list) {
  188.                     executor.update(sqlmapNamespace + "." + SQLID_UPDATE, member);
  189.                 }
  190.                 executor.executeBatch();
  191.                 return null;
  192.             }
  193.         };
  194.         this.getSqlMapClientTemplate().execute(callback);
  195.     }

  196.     public void batchDelete(final List<PK> list){
  197.         SqlMapClientCallback callback = new SqlMapClientCallback() {
  198.             public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
  199.                 executor.startBatch();
  200.                 for (PK member : list) {
  201.                     executor.delete(sqlmapNamespace + "." + SQLID_DELETE, member);
  202.                 }
  203.                 executor.executeBatch();
  204.                 return null;
  205.             }
  206.         };
  207.         this.getSqlMapClientTemplate().execute(callback);
  208.     }
  209. }
 3、某实体的DAO接口:
  1. public interface UserDao extends GenericDao<User,Integer> {
  2. //通用方法从GenericDao继承
  3. //这里可以定义针对此类的特定方法
  4. }

 4、某实体的DAO实现:
  1. public class UserDaoImpl extends GenericIbatisDao<User,Integer> implements UserDao {
  2. }

阅读(3068) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~