?? newsmanager.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 + -