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

📄 forummessagesdao.java

📁 struts+hibernate BBS mysql数据库 功能基本齐全
💻 JAVA
字号:
package com.elan.forum.dao;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import com.elan.db.ElHbnDB;
import com.elan.forum.model.Forummessages;
import com.elan.forum.util.Constents;

/**
 * A data access object (DAO) providing persistence and search support for
 * Forummessages entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.elan.forum.model.Forummessages
 * @author MyEclipse Persistence Tools
 */

public class ForumMessagesDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(ForumMessagesDAO.class);
	// property constants
	public static final String FROM_NAME = "fromName";
	public static final String FROM_ID = "fromId";
	public static final String TO_NAME = "toName";
	public static final String TO_ID = "toId";
	public static final String TITLE = "title";
	public static final String TEXT = "text";
	public static final String IS_READ = "isRead";
	public static final String IS_LJ = "isLj";

	private static ForumMessagesDAO fmDAO;

	static {
		fmDAO = new ForumMessagesDAO();
	}

	private ForumMessagesDAO() {
	}

	public static ForumMessagesDAO getForumMessagesDAO() {
		return fmDAO;
	}

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

	public void update(Forummessages fm) {
		Session session = ElHbnDB.getSession();
		session.update(fm);
		System.out.println("更新过程");
	}

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

	public Forummessages findById(java.lang.Integer id) {
		log.debug("getting ForumMessages instance with id: " + id);
		try {
			Forummessages instance = (Forummessages) ElHbnDB.getSession().get(
					"com.elan.forum.model.Forummessages", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(ForumMessagesDAO instance) {
		log.debug("finding ForumMessagesDAO instance by example");
		try {
			List results = getSession().createCriteria(
					"com.elan.forum.model.Forummessages").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<Forummessages> findByPropertyList(Integer id, Integer fromId) {
		return this.findByProperty(id, fromId, null, null, null, null, null,
				null, null, null, null);
	}

	public Forummessages findByProperty(Integer id, Integer toId) {
		List<Forummessages> listFm = this.findByProperty(id, null, null, toId,
				null, null, null, null, null, null, null);
		if (null != listFm && 0 < listFm.size()) {
			Forummessages fm = listFm.get(0);
			fm.setIsRead(Byte.valueOf("1"));
			update(fm);
			return fm;
		}
		return null;
	}

	public List<Forummessages> findByProperty(String propertyName, Object value) {
		log.debug("finding Forummessages instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Forummessages 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<Forummessages> findByFromName(Object fromName) {
		return findByProperty(FROM_NAME, fromName);
	}

	public List<Forummessages> findByFromId(Object fromId) {
		return findByProperty(FROM_ID, fromId);
	}

	public List<Forummessages> findByToName(Object toName) {
		return findByProperty(TO_NAME, toName);
	}

	public List<Forummessages> findByToId(Object toId) {
		return findByProperty(TO_ID, toId);
	}

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

	public List<Forummessages> findByText(Object text) {
		return findByProperty(TEXT, text);
	}

	public List<Forummessages> findByIsRead(Object isRead) {
		return findByProperty(IS_READ, isRead);
	}

	public List<Forummessages> findByIsLj(Object isLj) {
		return findByProperty(IS_LJ, isLj);
	}

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

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

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

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

	public  Integer getCount(Integer id, String ifStr) {
		log.debug("get count FORUMMESSAGES");
		try {
			Session session = ElHbnDB.getSession();
			String hql = "select count(*) from Forummessages";
			Query query;
			if (null != id) {
				hql += " where toId = ?";
				if(null != ifStr) {
					hql += ifStr;
				}
				System.out.println("getCount:" + hql);
				query = session.createQuery(hql);
				query.setParameter(0, id);
				return Integer
						.valueOf(((Long) query.uniqueResult()).intValue());
			} else {
				if(null != ifStr) {
					hql += ifStr;
				}
				System.out.println("getCount:" + hql);
				query = session.createQuery(hql);
				return (Integer) query.uniqueResult();
			}
		} catch (RuntimeException re) {
			log.error("getCount failed", re);
			throw re;
		}
	}
	public Integer getCount(Integer id) {
		return this.getCount(id, " and isLj = 0 order by id desc");
	}
	
	public Integer getLjCount(Integer id) {
		return this.getCount(id, " and isLj = 1 order by id desc");
	}

	public Integer getCount() {
		return getCount(null);
	}

	public List<Forummessages> findByIf(Integer id, Integer page,
			Integer pageSize, String ifStr) {
		List<Forummessages> listFm;
		String hql = "From Forummessages as fm where 1 = 1";
		Session session = ElHbnDB.getSession();
		Query query;
		boolean bIdNull = true;
		if (null != id) {
			hql += " and fm.toId = ?";
			bIdNull = !bIdNull;
		}
		if(null != ifStr) {
			hql += ifStr;
		}
		query = session.createQuery(hql);
		if (!bIdNull) {
			query.setParameter(0, id);
		}
		System.out.println("::::::::::" + hql);
		System.out.println("page::::::::" + page);
		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		listFm = query.list();
		if (listFm.size() > 0) {
			return listFm;
		}
		return null;
	}
	public List<Forummessages> findByPage(Integer id, Integer page,
			Integer pageSize) {
		return this.findByIf(id, page, pageSize, " and isLJ = 0 order by id desc");
	}

	public int setLjMsg(Forummessages fm) {
		Session session = ElHbnDB.getSession();
		if(null != fm) {
			session.update(fm);
			return Constents.CURRENT_OPERATION_SUCCESS;
		}
		return Constents.CURRENT_OPERATION_ERROR;
	}
	
	

	public List<Forummessages> findByProperty(Integer Id, Integer fromId,
			String fromName, Integer toId, String toName,
			Timestamp startCreateTime, Timestamp endCreateTime, Byte isRead,
			Byte isLJ, Integer page, Integer pageSize) {
		Session session = ElHbnDB.getSession();
		List<Forummessages> listFm = null;
		Criteria criteria = session.createCriteria(Forummessages.class);
		if (null != Id) {
			Criterion idCtn = Expression.eq("id", Id);
			criteria.add(idCtn);
		}
		if (null != fromId) {
			Criterion fromIdCtn = Expression.eq("fromId", fromId);
			criteria.add(fromIdCtn);
		}

		if (null != fromName && !"".equals(fromName.trim())) {
			Criterion fromNameCtn = Expression.eq("fromName", fromName);
			criteria.add(fromNameCtn);
		}

		if (null != toId) {
			Criterion toIdCtn = Expression.eq("toId", toId);
			criteria.add(toIdCtn);
		}

		if (null != toName && !"".equals(toName.trim())) {
			Criterion toNameCtn = Expression.eq("toName", toName);
			criteria.add(toNameCtn);
		}

		if (null != isRead) {
			Criterion isReadCtn = Expression.eq("isRead", isRead);
			criteria.add(isReadCtn);
		}

		if (null != isLJ) {
			Criterion isLJCtn = Expression.eq("isLJ", isLJ);
			criteria.add(isLJCtn);
		}

		if (null != startCreateTime && null != endCreateTime) {
			Criterion startCreateTimeCtn = Expression.between("createTime",
					startCreateTime, endCreateTime);
			criteria.add(startCreateTimeCtn);
		}

		if (null != page && null != pageSize) {
			if (1 < page && 1 < pageSize) {
				criteria.setFirstResult((page - 1) * pageSize);
			} else {
				page = 1;
				pageSize = 10;
				criteria.setMaxResults((page - 1) * pageSize);
			}
		}
		listFm = criteria.list();
		if (0 < listFm.size()) {
			return listFm;
		}
		return null;
	}

	public List<Forummessages> findLjByPage(Integer id, Integer page,
			Integer pageSize) {
		return this.findByIf(id, page, pageSize, " and isLJ = 1");
	}

	public int postMsg(Integer fromId, String fromName, Integer toId,
			String toName, String title, String text) {
		Session session = ElHbnDB.getSession();
		Forummessages fm = new Forummessages();
		fm.setFromId(fromId);
		fm.setFromName(fromName);
		fm.setToId(toId);
		fm.setToName(toName);
		fm.setTitle(title);
		fm.setText(text);
		fm.setCreateTime(new Timestamp(System.currentTimeMillis()));
		fm.setIsLj(Byte.valueOf("0"));
		fm.setIsRead(Byte.valueOf("0"));
		
		session.save(fm);
		return Constents.CURRENT_OPERATION_SUCCESS;
	}
}

⌨️ 快捷键说明

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