BlogHibernateDAO.java
资源名称:Myblog.rar [点击查看]
上传用户:wlfwy2004
上传日期:2016-12-12
资源大小:33978k
文件大小:2k
源码类别:
Jsp/Servlet
开发平台:
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();
- }
- });
- }
- }