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

📄 newsmanager.java

📁 fish新闻系统fish新闻系统fish新闻系统fish新闻系统
💻 JAVA
字号:
package com.fise.bl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;

import com.fise.HibernateSessionFactory;
import com.fise.News;
import com.fise.bean.Pager;

public class NewsManager {
	public News[] getAllNews() {
	    /* will hold the books we are going to return later */
	    List newss = new ArrayList();
	    /* a Hibernate session */
	    Session session = null;
	    /* we always need a transaction */
	    Transaction tx = null;
	    try {
	      /* get session of the current thread */
	    	session = HibernateSessionFactory.currentSession();

	      tx = session.beginTransaction();
	      Query query = session
	          .createQuery("select n from News as n order by n.date,n.title");
	      for (Iterator iter = query.iterate(); iter.hasNext();) {
	        newss.add((News) iter.next());
	      }
	      tx.commit();
	    } catch (HibernateException e) {
	      e.printStackTrace();
	      if (tx != null) try {
	        tx.rollback();
	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }
	    } finally {
	      try {
	        if (session != null) session.close();
	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }

	    }
	    return (News[]) newss.toArray(new News[0]);
	  }
	
	public News getNewsByPrimaryKey(Integer primaryKey) {
	    /* holds our return value */
	    News news = null;
	    /* a Hibernate session */
	    Session session = null;
	    /* we always need a transaction */
	    Transaction tx = null;

	    try {
	      /* get session of the current thread */
	      session = HibernateSessionFactory.currentSession();

	      tx = session.beginTransaction();
	      news = (News) session.get(News.class, primaryKey);
	      tx.commit();
	    } catch (HibernateException e) {
	      e.printStackTrace();
	      if (tx != null) try {
	        tx.rollback();

	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }

	    } finally {
	      try {
	        if (session != null) session.close();
	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }
	    }
	    return news;
	  }
	
	public void saveNews(News newsValue) {

	    /* a Hibernate session */
	    Session session = null;
	    /* we always need a transaction */
	    Transaction tx = null;
	    try {
	      /* get session of the current thread */
	      session = HibernateSessionFactory.currentSession();

	      tx = session.beginTransaction();
	      News news;
	      if (newsValue.getId() != null && newsValue.getId().intValue() != 0) {
	        news = (News) session.get(News.class, newsValue.getId());
	        if (news != null) {
	          news.setDate(newsValue.getDate());
	          news.setTitle(newsValue.getTitle());
	          news.setInfo(newsValue.getInfo());
	          session.update(news);
	          System.out.println(news.getDate());
	          System.out.println(news.getTitle());
	          System.out.println(news.getInfo());
	        }
	      }
	      else
	      {
	        news = new News();
	        news.setDate(newsValue.getDate());
	        news.setTitle(newsValue.getTitle());
	        news.setInfo(newsValue.getInfo());
	        session.save(news);
	      }
	      tx.commit();
	    } catch (HibernateException e) {
	      e.printStackTrace();
	      if (tx != null) try {
	        tx.rollback();
	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }

	    } finally {
	      try {
	        if (session != null) session.close();
	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }
	    }
	  }
	public void removeNewsByPrimaryKey(Integer primaryKey) {
	    /* a Hibernate session */
	    Session session = null;
	    /* we always need a transaction */
	    Transaction tx = null;

	    try {
	      /* get session of the current thread */
	      session = HibernateSessionFactory.currentSession();

	      tx = session.beginTransaction();
	      News news = (News) session.get(News.class, primaryKey);
	      if (news != null) session.delete(news);
	      tx.commit();
	    } catch (HibernateException e) {
	      e.printStackTrace();
	      if (tx != null) try {
	        tx.rollback();
	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }
	    } finally {
	      try {
	        if (session != null) session.close();
	      } catch (HibernateException e1) {
	        e1.printStackTrace();
	      }
	    }
	  }
	
	public News[] getNewsByDateTitle(String date,String title,Pager pager)throws Exception {
		 List list = null;
	     Session session = null;
	     Transaction transaction = null;
	     Query query = null;
	     try {
	        session = HibernateSessionFactory.currentSession();
	        transaction = session.beginTransaction();
			query = session.createQuery("select n from News as n where n.date like :date and n.title like :title order by n.id desc");
			query.setString("date", "%" + date + "%");
			query.setString("title", "%" + title + "%");
			query.setFirstResult(pager.getStartRows()); // 设置起始行
	        query.setMaxResults(pager.getPageSize());   // 设置取数据行数
	        list = query.list();
	        transaction.commit();
	        session.close();
	     } catch (Exception excp) {
	         session.close();
	     }
	     return (News[]) list.toArray(new News[0]);
	}

	/**
	 * 
	 * 取得总数据行
	 * 
	 */
	public int selectDataRows() {
		int rows = 0;
		List list = null;
		Session session = null;
		Transaction transaction = null;
		Query query = null;
		try {
			session = HibernateSessionFactory.currentSession();
			transaction = session.beginTransaction();
			query = session.createQuery("select news from News as news");
			list = query.list();
			rows = list.size();
			transaction.commit();
			session.close();
		} catch (Exception excp) {
			session.close();
		}
		return rows;
	}
	
	/**
    * 分页查询数据
    * 
    * @param pager 
    * @return 0 1 0: Success 1: Fail
    */
   public News[] selectData(Pager pager) {
    List list = null;
    Session session = null;
    Transaction transaction = null;
    Query query = null;
    try {
        session = HibernateSessionFactory.currentSession();
        transaction = session.beginTransaction();
        query = session.createQuery("select news from News as news order by news.id desc");
        query.setFirstResult(pager.getStartRows()); // 设置起始行
        query.setMaxResults(pager.getPageSize());   // 设置取数据行数
        list = query.list();
        transaction.commit();
        session.close();
    } catch (Exception excp) {
        session.close();
    }
    return (News[]) list.toArray(new News[0]);
   }

}

⌨️ 快捷键说明

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