Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2534293
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-10-18 10:30:54

从网上抓了一个SearchDao
From:
  1. /* ====================================================================
  2.  * $Id$
  3.  * ====================================================================
  4.  * 文件名 SearchDao.java
  5.  * 机能名
  6.  * 履历 2005-2-1 dlxu 创建新文件
  7.  * Copyright 2004 东南大学 All Rights Reserved
  8.  * ====================================================================
  9.  */
  10. package cn.edu.seu.album.dao;
  11.    
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.    
  15. import net.sf.hibernate.Query;
  16. import net.sf.hibernate.Session;
  17.    
  18. import org.apache.log4j.Category;
  19. import org.apache.log4j.Logger;
  20.    
  21. import cn.edu.seu.album.common.StringUtil;
  22. import cn.edu.seu.album.model.SearchCondition;
  23.    
  24. /**
  25.  *

    [概 要]


  26.  *

    [详 细]


  27.  *

    [备 考] 无。


  28.  *
  29.  * @author dlxu
  30.  * @version 1.0 2005-2-1
  31.  * @since 1.0
  32.  */
  33. public final class SearchDao {
  34.    
  35.     /**
  36.      * 日志定义。
  37.      */
  38.     private static final Category log = Logger.getInstance(SearchDao.class);
  39.    
  40.     /**
  41.      * 单例模式。
  42.      */
  43.     private static SearchDao dao = new SearchDao();
  44.    
  45.     /**
  46.      *

    [概 要] 构造方法


  47.      *

    [详 细] 构造方法


  48.      *

    [备 考] 无。


  49.      *
  50.      */
  51.     private SearchDao() {
  52.    
  53.     }
  54.    
  55.     /**
  56.      *

    [概 要]


  57.      *

    [详 细]


  58.      *

    [备 考] 无。


  59.      * @return
  60.      */
  61.     public static SearchDao getInstance() {
  62.         return dao;
  63.     }
  64.    
  65.     /**
  66.      *

    [概 要]


  67.      *

    [详 细]


  68.      *

    [备 考] 无。


  69.      * @param condition
  70.      * @param session
  71.      * @return
  72.      */
  73.     public List getPictureList(SearchCondition cond, Session session)
  74.             throws Exception {
  75.         log.debug("getPictureList开始");
  76.    
  77.         String sql = getSearchSql(cond);
  78.    
  79.         Query query = session.createQuery(sql);
  80.    
  81.         if (!StringUtil.isEmpty(cond.getFromYear())) {
  82.             query.setTimestamp(0, cond.getFromDate());
  83.             query.setTimestamp(1, cond.getToDate());
  84.         }
  85.         if (!StringUtil.isEmpty(cond.getOwner())) {
  86.             if (!StringUtil.isEmpty(cond.getFromYear())) {
  87.             query.setString(2, cond.getOwner());
  88.             } else {
  89.                 query.setString(0, cond.getOwner());
  90.             }
  91.         }
  92.    
  93.         List rsltList = query.list();
  94.    
  95.         if (null == rsltList) {
  96.             rsltList = new ArrayList();
  97.         }
  98.    
  99.         log.debug("getPictureList结束");
  100.         return rsltList;
  101.     }
  102.    
  103.     /**
  104.      *

    [概 要]


  105.      *

    [详 细]


  106.      *

    [备 考] 无。


  107.      * @param cond
  108.      * @return
  109.      */
  110.     private String getSearchSql(SearchCondition cond) {
  111.         log.debug("getSearchSql开始");
  112.    
  113.         StringBuffer sql = new StringBuffer();
  114.    
  115.         sql.append(" from ");
  116.         sql.append(" cn.edu.seu.album.pojo.Photo photo ");
  117.         sql.append(" where ");
  118.         sql.append(" 1 = 1 ");
  119.         if (!StringUtil.isEmpty(cond.getFromYear())) {
  120.             sql.append(" and photo.lastUpdateDate > ? ");
  121.             sql.append(" and photo.lastUpdateDate ? ");
  122.         }
  123.         if (!StringUtil.isEmpty(cond.getOwner())) {
  124.             sql.append(" and photo.person.name = ? ");
  125.         }
  126.         sql.append(" order by ");
  127.         if (StringUtil.isEmpty(cond.getSortField())) {
  128.             sql.append(" photo.lastUpdateDate desc ");
  129.         } else {
  130.             sql.append(cond.getSortField());
  131.             sql.append(" ");
  132.             sql.append(cond.getSortOrder());
  133.         }
  134.    
  135.         log.debug("getSearchSql结束");
  136.         return sql.toString();
  137.     }
  138. }

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