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

📄 newsimp.java

📁 一个新闻发布系统
💻 JAVA
字号:
package com.accp.t29.module.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.accp.t29.hibernate.cfg.HibernateSessionFactory;
import com.accp.t29.hibernate.pojo.NewsTb;
import com.accp.t29.module.service.NewsService;

public final class NewsImp implements NewsService {

	public boolean addNews(NewsTb news) {
		// TODO 自动生成方法存根
		boolean flag = false;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(news);
			tx.commit();
			flag = tx.wasCommitted();
			
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace(System.out);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return flag;
	}

	public List<NewsTb> findNewsBetweenTime(String begin, String end) {
		// TODO 自动生成方法存根
		return null;
	}

	public List<NewsTb> findNewsByAll(int offset, int length) {
		// TODO 自动生成方法存根
		List<NewsTb> newsList = null;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			newsList = session.createQuery("from NewsTb")
					.setFirstResult(offset).setMaxResults(length).list();
			tx.commit();
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace(System.out);
		} finally {
			if (session != null)
				session.close();
		}
		return newsList;

	}

	public List<NewsTb> findNewsByAuthor(String author, int offset, int length) {
		// TODO 自动生成方法存根
		List<NewsTb> newsList = null;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			newsList = session.createQuery(
					"from NewsTb where authorname like '%:authorname%'")
					.setString("authorname", author).list();
			tx.commit();
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return newsList;
	}

	public List<NewsTb> findNewsByCount(int count) {
		// TODO 自动生成方法存根
		return null;
	}

	public List<NewsTb> findNewsByTitle(String newsTitle) {
		// TODO 自动生成方法存根
		return null;
	}

	public int getTotalRsTotalCount() {
		// TODO 自动生成方法存根
		int totalSize = 0;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			totalSize = Integer.parseInt(session.createSQLQuery(
					"select count(id) from news_tb").uniqueResult().toString());
			tx.commit();
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} finally {
			if (session != null)
				session.close();
		}
		return totalSize;
	}

	public boolean removeNews(int[] newsid) {
		// TODO 自动生成方法存根
		boolean flag = false;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Connection conn = session.connection();
			PreparedStatement pst = conn
					.prepareStatement("delete from news_tb where id=?");
			for (int i = 0; i < newsid.length; i++) {
				pst.setInt(1, newsid[i]);
				pst.addBatch();
			}
			if (pst.executeBatch().length == newsid.length) {
				flag = true;
			}
			tx.commit();
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace(System.out);
		} catch (SQLException e) {
			// TODO 自动生成 catch 块
			if (session != null)
				session.close();
			e.printStackTrace(System.out);
		}

		return flag;
	}

	public boolean updateNews(NewsTb news) {
		// TODO 自动生成方法存根
		boolean flag = false;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			NewsTb newsTb = (NewsTb) session.get(NewsTb.class, news.getId());
			newsTb.setAttachmentTb(news.getAttachmentTb());
			newsTb.setCount(news.getCount());
			newsTb.setIscheck(news.getIscheck());
			newsTb.setIspircture(news.getIspircture());
			newsTb.setIsrelease(news.getIsrelease());
			newsTb.setNewscontent(news.getNewscontent());
			newsTb.setNewssubhead(news.getNewssubhead());
			newsTb.setNewstitle(news.getNewstitle());
			newsTb.setReleasetime(news.getReleasetime());
			newsTb.setRemark(news.getRemark());
			newsTb.setResourceTb(news.getResourceTb());
			newsTb.setAuthorname(news.getAuthorname());
			session.update(newsTb);
			tx.commit();
			flag = true;
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace(System.out);
		} finally {
			if (session != null)
				session.close();
		}
		return flag;
	}

	public NewsTb findNewsByID(int id) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		NewsTb newsBean = null;
		try {
			tx = session.beginTransaction();
			newsBean = (NewsTb) session.createQuery("from NewsTb where id=:id")
					.setInteger("id", id).uniqueResult();
			tx.commit();
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return newsBean;
	}

	public List<NewsTb> findNewsBySql(String condititon, String conditionValue,
			int offset, int length) {
		List<NewsTb> newsList = null;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			if (condititon.equals("count")) {
				newsList = session.createCriteria(NewsTb.class).add(
						Restrictions
								.eq("count", Long.parseLong(conditionValue)))
						.setFirstResult(offset).setMaxResults(length).list();
			} else {
				newsList = session.createCriteria(NewsTb.class)
						.add(
								Expression.like(condititon, "%"
										+ conditionValue + "%"))
						.setFirstResult(offset).setMaxResults(length).list();
			}
			tx.commit();
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return newsList;
	}

	public int getTotalCountBySql(String condititon, String conditionValue) {
		int totalSize = 0;
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			if (condititon.equals("count")) {
				totalSize = Integer.parseInt(session.createCriteria(
						NewsTb.class).setProjection(Projections.count("id"))
						.add(
								Restrictions.eq("count", Long
										.parseLong(conditionValue)))
						.uniqueResult().toString());
			} else
				totalSize = Integer.parseInt(session.createCriteria(
						NewsTb.class).setProjection(Projections.count("id"))
						.add(
								Expression.like(condititon, "%"
										+ conditionValue + "%")).uniqueResult()
						.toString());
			tx.commit();
		} catch (HibernateException e) {
			// TODO 自动生成 catch 块
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalSize;

	}

}

⌨️ 快捷键说明

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