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

📄 bloghibernatedao.java

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

import org.springframework.orm.hibernate3.support.*;
import com.opensource.blog.dao.BlogDAO;
import com.opensource.blog.model.Blog;

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


public class BlogHibernateDAO
    extends HibernateDaoSupport implements BlogDAO {

  private static final String LOAD_BY_USERNAME = "from Blog where username = ?";

  private static final String GET_ALL_NUM = "select count(*) from Blog";

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

  public BlogHibernateDAO() {
    super();
  }

  /**
   *
   * @param blog Blog
   * @return Blog
   * @todo Implement this com.opensource.blog.dao.BlogDAO method
   */
  public Blog saveBlog(Blog blog) {
    this.getHibernateTemplate().saveOrUpdate(blog);
    return blog;
  }

  /**
   *
   * @param id long
   * @return Blog
   */
  public Blog findBlogByID(long id) {
    return (Blog)this.getHibernateTemplate().get(Blog.class, new Long(id));
  }

  /**
   *
   * @return Blog
   * @param userName String
   * @todo Implement this com.opensource.blog.dao.BlogDAO method
   */
  public Blog findBlogByUserName(String userName) {
    List l = this.getHibernateTemplate().find(LOAD_BY_USERNAME, userName);
    if (l == null || l.isEmpty()) {
      return null;
    }
    else {
      return (Blog) l.get(0);
    }
  }

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

  /**
   *
   * @param firstResult int
   * @param maxResults int
   * @return List
   */
  public List findBlogAll(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();
      }
    });

  }

}

⌨️ 快捷键说明

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