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

📄 newsdao.java

📁 struts hibernate spring实现新闻发布系统含说明文档
💻 JAVA
字号:
package com.ata.wx.java01b.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class News.
 * 
 * @see com.ata.wx.java01b.dao.News
 * @author MyEclipse Persistence Tools
 */

public class NewsDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(NewsDAO.class);
	// property constants
	public static final String TITLE = "title";
	public static final String CONTENT = "content";

	public void save(News transientInstance) {
		log.debug("saving News instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(News persistentInstance) {
		log.debug("deleting News instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public News findById(java.lang.String id) {
		log.debug("getting News instance with id: " + id);
		try {
			News instance = (News) getSession().get(
					"com.ata.wx.java01b.dao.News", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(News instance) {
		log.debug("finding News instance by example");
		try {
			List results = getSession().createCriteria(
					"com.ata.wx.java01b.dao.News").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding News instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from News as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findAll() {
		log.debug("finding all News instances");
		try {
			String queryString = "from News";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public News merge(News detachedInstance) {
		log.debug("merging News instance");
		try {
			News result = (News) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(News instance) {
		log.debug("attaching dirty News instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(News instance) {
		log.debug("attaching clean News instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	// 查找最新5条记录
	public List topFind() {
		log.debug("finding top 5 instances");
		try {
			String queryString = "from News order by time desc limit 0,8";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(0);
			queryObject.setMaxResults(8);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List<News> findNewsAndComment(String newsid) {
		log.debug("finding news add Comment");
		try {
			System.out.println("aaaaaaaaaaaaa");
			String queryString = "from News as news inner join fetch news.comments as comm left join fetch comm.user as user Where news.newsid=:id order by comm.time asc";
			System.out.println(queryString);
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setString("id", newsid);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List<News> findAllNews(){
		log.debug("finding all News with user add type");
		try {
			String queryString = "from News as news inner join fetch news.user inner join fetch news.newstype";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public static void main(String[] args) {
		// NewsDAO dao = new NewsDAO();
		// List<News> newslist = dao.topFind();
		// for(News n:newslist){
		// System.out.println(n.getTime());
		// }
		
		
//		NewsDAO dao = new NewsDAO();
//		List<News> newslist = dao
//				.findNewsAndComment("4028828e18ba76ee0118be76f0d10001");
//		News news = newslist.get(0);
//		Set<Comment> comm = news.getComments();
//		for(Comment c:comm){
//			System.out.println(c.getContent());
//			System.out.println(c.getUser().getUsername());
//			System.out.println(c.getTime());
//		}
		
		NewsDAO dao = new NewsDAO();
		List<News> newslist = dao.findAllNews();
		System.out.println(newslist.size()+"--------");
	}
}

⌨️ 快捷键说明

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