⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 articlehibernatedao.java

📁 是一个网站的博客系统
💻 JAVA
字号:
package com.opensource.blog.dao.hibernate;

import org.springframework.orm.hibernate3.support.*;
import com.opensource.blog.dao.ArticleDAO;
import com.opensource.blog.model.Article;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateCallback;
import java.sql.SQLException;


public class ArticleHibernateDAO
    extends HibernateDaoSupport implements ArticleDAO {

  private static final String LOAD_BY_ID_BLOGID = "from Article where id = ? and blogid = ?";

  private static final String LOADS_BY_BLOGID =
      "from Article where blogid = ? order by posttime desc";

  private static final String GET_NUM_BY_BLOGID = "select count(*) from Article where blogid = ?";

  private static final String GET_NUM_BY_BLOGID_ISHIDE =
      "select count(*) from Article where blogid = ? and ishide = ?";

  private static final String LOADS_BY_BLOGID_ISHIDE =
      "from Article where blogid = ? and ishide = ? order by posttime desc";

  private static final String LOADS_BY_SORTID = "from Article where sortid = ? and blogid = ?";

  private static final String GET_NUM_BY_SORTID =
      "select count(*) from Article where sortid = ? and blogid = ?";

  private static final String LOADS_BY_POSTDATE =
      "from Article where blogid = ? and postdate = ? order by id desc";

  private static final String GET_NUM_BY_POSTDATE =
      "select count(*) from Article where blogid = ? and postdate = ?";

  private static final String LOADS_GROUPBY_POSTTIME =
      "select postdate from Article where blogid = ? group by postdate";

  private static final String GET_NUM_ALL = "select count(*) from Article";

  private static final String LOADS_ALL = "from Article order by id desc";

  public ArticleHibernateDAO() {
  }

  /**
   *
   * @param article Article
   * @return Article
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public Article saveArticle(Article article) {
    this.getHibernateTemplate().saveOrUpdate(article);
    return article;
  }

  /**
   *
   * @param id long
   * @param blogID long
   * @return Article
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public Article findArticleByID_BlogID(long id, long blogID) {
    Object[] o = {new Long(id), new Long(blogID)};
    List l = this.getHibernateTemplate().find(LOAD_BY_ID_BLOGID, o);
    if (l == null || l.isEmpty()) {
      return null;
    }
    else {
      return (Article) l.get(0);
    }
  }

  /**
   *
   * @param blogID long
   * @return int
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public int getArticleNumByBlogID(long blogID) {
    List l = this.getHibernateTemplate().find(GET_NUM_BY_BLOGID, new Long(blogID));
    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Integer) l.get(0)).intValue();
    }
  }

  public int getArticleNumByBlogID_IsHide(long blogID, int ishide) {
    Object[] o = {new Long(blogID), new Long(ishide)};
    List l = this.getHibernateTemplate().find(GET_NUM_BY_BLOGID_ISHIDE, o);
    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Integer) l.get(0)).intValue();
    }

  }

  /**
   *
   * @param blogID long
   * @param firstResult int
   * @param maxResults int
   * @return List
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public List findArticlesByBlogID(final long blogID, final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
          Query query = s.createQuery(LOADS_BY_BLOGID);
          query.setLong(0, blogID);
          query.setFirstResult(firstResult);
          query.setMaxResults(maxResults);
          return query.list();
      }
    });
  }

  public List findArticlesByBlogID_IsHide(final long blogID, final int ishide,
                                          final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
          Query query = s.createQuery(LOADS_BY_BLOGID_ISHIDE);
          query.setLong(0, blogID);
          query.setLong(1, ishide);
          query.setFirstResult(firstResult);
          query.setMaxResults(maxResults);
          return query.list();
      }
    });
  }

  /**
   *
   * @param sortID long
   * @return int
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public int getArticleNumBySort(long blogID, long sortID) {
    Object[] o = {new Long(sortID), new Long(blogID)};
    List l = this.getHibernateTemplate().find(GET_NUM_BY_SORTID, o);
    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Integer) l.get(0)).intValue();
    }
  }

  /**
   *
   * @param sortID long
   * @param firstResult int
   * @param maxResults int
   * @return List
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public List findArticlesBySort(final long blogID, final long sortID, final int firstResult,
                                 final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
          Query query = s.createQuery(LOADS_BY_SORTID);
          query.setLong(0, sortID);
          query.setLong(1, blogID);
          query.setFirstResult(firstResult);
          query.setMaxResults(maxResults);
          return query.list();
      }
    });
  }

  public List findArticlesAllBySort(long blogID, long sortID) {
    Object[] o = {new Long(blogID), new Long(sortID)};
    return this.getHibernateTemplate().find(LOADS_BY_SORTID, o);
  }

  /**
   *
   * @param postDate String
   * @return int
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public int getArticleNumByPostDate(long blogID, String postDate) {
    Object[] o = {new Long(blogID), postDate};
    List l = this.getHibernateTemplate().find(GET_NUM_BY_POSTDATE, o);
    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Integer) l.get(0)).intValue();
    }
  }

  /**
   *
   * @param postDate String
   * @param firstResult int
   * @param maxResults int
   * @return List
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public List findArticlesByPostDate(final long blogID, final String postDate,
                                     final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
          Query query = s.createQuery(LOADS_BY_POSTDATE);
          query.setLong(0, blogID);
          query.setString(1, postDate);
          query.setFirstResult(firstResult);
          query.setMaxResults(maxResults);
          return query.list();
      }
    });
  }

  public List findArticlesGroupByPostDate(long blogID) {
    return this.getHibernateTemplate().find(LOADS_GROUPBY_POSTTIME, new Long(blogID));
  }

  /**
   *
   * @return int
   */
  public int getArticleAllNum() {
    List l = this.getHibernateTemplate().find(GET_NUM_ALL);
    if (l == null || l.isEmpty()) {
      return 0;
    }
    else {
      return ( (Integer) l.get(0)).intValue();
    }
  }

  /**
   *
   * @param firstResult int
   * @param maxResults int
   * @return List
   */
  public List findArticlesAll(final int firstResult, final int maxResults) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session s) throws HibernateException, SQLException {
          Query query = s.createQuery(LOADS_ALL);
          query.setFirstResult(firstResult);
          query.setMaxResults(maxResults);
          return query.list();
      }
    });
  }

  /**
   *
   * @param article Article
   * @todo Implement this com.opensource.blog.dao.ArticleDAO method
   */
  public void removeArticle(Article article) {
    this.getHibernateTemplate().delete(article);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -