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

📄 elantopicdaoimpl.java

📁 struts+hibernate BBS mysql数据库 功能基本齐全
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	/**
	 * 获取topicID下回复的条数
	 * 
	 * @param
	 * @return Long
	 */
	public Long getForumTopicReplyCountById(Integer topicId) {
		Session session = ElHbnDB.getSession();
		String hql = "select Count(*) From Forumtopicreply where topicId = "
				+ topicId + "and locked = 0";
		Long l = ((Long) session.createQuery(hql).uniqueResult());
		return l;
	}


	/**
	 * 给出topicId,返回所有的topicreply
	 * @param topicId
	 * @return list<Forumtopicreply>
	 */
	public List<Forumtopicreply> getForumTopicReplyById(Integer topicId) {
		Session session = ElHbnDB.getSession();
		String hql = "from forumtopicreply where topicId = ? and locked = 0";
		Query query = session.createQuery(hql);
		query.setParameter(0, topicId);
		return query.list();
	}
	
	/**
	 * 返回给定叁数信息返回一定大小的topicreply list
	 * 根据本人在以前测试一个用hibernate 的setM...setF....来分页,
	 * 记忆中比MYSQL limit 分页的数据块
	 * 所以采用hibernate分页
	 * @param page 第几页开始
	 * @param pageSize 页码的大小
	 * @param topicId 哪个topic下面
	 * @return List<Forumtopicreply> 
	 */
	public List<Forumtopicreply> getForumTopicReplyById(Integer page, Integer pageSize, Integer topicId) {
		Session session = ElHbnDB.getSession();
		String hql = "From Forumtopicreply where topicId = ? and locked = 0";
		Query query = session.createQuery(hql);
		query.setParameter(0, topicId);
		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		return query.list();
	}
	
	/**
	 * 成员 topicCount 并不包括topic主题,只包括reply的topic
	 * map: 1)topicCount
	 * 		2)page
	 * 		3)pageCount
	 * 		4)replyList
	 * 		5)fuList   回复的作者
	 * @param page 第几页开始
	 * @param pageSize 页码的大小
	 * @param topicId 哪个topic下面
	 * @return Map
	 * (non-Javadoc)
	 * @see com.elan.forum.dao.TopicDAO#getForumTopicReply(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public Map getForumTopicReply(Integer page, Integer pageSize, Integer topicId) {
		Map map = new HashMap();
		// 设置这个topic下面的的回复的topicReplyCount
		Long topicCount = this.getForumTopicReplyCountById(topicId);
		Long pageCount = new Long(0);
		
		map.put("topicCount", topicCount);
		
		if(0 == topicCount % pageSize) {
			pageCount = topicCount / pageSize;
		} else {
			pageCount = topicCount / pageSize + 1;
		}
		if(page > pageCount) {
			page = pageCount.intValue();
		}
		//..
		map.put("page", page);
		// 设置pageCount,一共有多少页
		map.put("pageCount", pageCount);
		// 设置topic下面的topicReply
		List<Forumtopicreply> list = this.getForumTopicReplyById(page, pageSize, topicId);
		map.put("replyList", list);
		return map;
	}

	public boolean saveTopic(ForumTopic forumTopic) {
		Session session = ElHbnDB.getSession();
		session.save(forumTopic);
		return true;
	}

	public ForumTopic getTopicId(Integer topicId) {
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery("from ForumTopic where id = ? and locked = 0");
		query.setParameter(0, topicId);
		List<ForumTopic> list = query.list();
		if(list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/*
	 * 获取新的帖子
	 * (non-Javadoc)
	 * @see com.elan.forum.dao.TopicDAO#getNewPostTopic(java.lang.Integer)
	 */
	public List<ForumTopic> getNewPostTopic(Integer pieceId, Integer page, Integer pageSize) {
		Session session = ElHbnDB.getSession();
		String hql = "From ForumTopic where 1 = 1 and";
		if(pieceId != null) {
			hql += " pieceId = ? and";
		}
		hql += " locked = 0 order by id desc";
		Query query = session.createQuery(hql);
		if(pieceId != null) {
			query.setParameter(0, pieceId);
		}
		if(page != null && page.intValue() > 1) {
			query.setFirstResult((page - 1) * pageSize);
		}
		if(pageSize != null && pageSize > -1) {
			query.setMaxResults(pageSize);
		}
		return query.list();
	}

	/**
	 * 获取最热门的帖子
	 */
	public List<ForumTopic> getHotTopic(Integer pieceId, Integer page,
			Integer pageSize) {
		boolean isPieceId = false;
		String hql = "From ForumTopic";
		if(pieceId != null && pieceId.intValue() > 0) {
			hql += " where pieceId = ? and hot = 1";
			isPieceId = true;
		} else {
			hql += " where hot = 1";
		}
		hql += " and locked = 0 order by id desc";
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery(hql);
		if(isPieceId) {
			query.setParameter(0, pieceId);
		}
		if(pageSize != null && pageSize.intValue() > -1) {
			query.setMaxResults(pageSize);
		}
		return query.list();
	}

	public Forumpiece getForumPieceById(Integer pieceId) {
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery("From Forumpiece where id = ?");
		query.setParameter(0, pieceId);
		List<Forumpiece> list = query.list();
		if(list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public List<Forummodule> getModule() {
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery("From Forummodule");
		List<Forummodule> list = query.list();
		return list;
	}

	public List<Forumpiece> getPiece() {
		Session session  = ElHbnDB.getSession();
		Query query = session.createQuery("From Forumpiece");
		List<Forumpiece> list = query.list();
		return list;
	}

	public Map<String, Object> search(Integer moduleId, Integer pieceId,
			Integer topicId, Integer hotReal, String topicName, String author,
			Timestamp startTime, Timestamp endTime, Integer precision, Integer page, Integer pageSize) {
		//采用hql实现,也可以采用QBC语句实现
		Integer topicCount = 0;
		Map<String, Object> map = null;
		Session session = ElHbnDB.getSession();
		String hql = this.createHql(moduleId, pieceId, topicId, hotReal, topicName, author, startTime, endTime, precision, page, pageSize, topicCount);
		System.out.println(hql);
		topicCount = Integer.valueOf(((Long) session.createQuery("select Count(*) " + hql).uniqueResult()).intValue());
		Integer pageCount = 0;
		if(topicCount % pageSize == 0) {
			pageCount = topicCount / pageSize;
		} else {
			pageCount = topicCount / pageSize + 1;
		}
		if(page < 0) {
			page = 1;
		}else if(page > pageCount) {
			page = pageCount;
		}
		Query query = session.createQuery(hql);
		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		List<ForumTopic> listTopic = query.list();
		if(listTopic.size() > 0) {
			map = new HashMap<String, Object>();
			map.put("page", page);//当前页
			map.put("pageSize", pageSize);//页码大小
			map.put("topicCount", topicCount);//总帖子数
			map.put("pageCount", pageCount);//总页数
			map.put("listTopic", listTopic);//分过页的帖子列表
			return map;
		}
		return null;
	}
	
	
	private String createHql(Integer moduleId, Integer pieceId,
			Integer topicId, Integer hotReal, String topicName, String author,
			Timestamp startTime, Timestamp endTime, Integer precision, Integer page, Integer pageSize, Integer topicCount) {
		String hql = "From ForumTopic where 1 = 1  and locked = 0 ";
		if(topicId != null && topicId.intValue() > 0) {
			hql += " and id = " + topicId;
		} 
		if(pieceId != null && pieceId.intValue() > 0) {
			hql += " and pieceId = "+ pieceId;
		}
		if(moduleId != null && moduleId.intValue() > 0) {
			hql += " and moduleId = " + moduleId;
		}
		if(hotReal != null && (hotReal.intValue() == 2 || hotReal.intValue() == 1)) {
			if(1 == hotReal.intValue()) {
				hql += " and hot = 1";
			} else if(2 == hotReal.intValue()) { //精华
				hql += " and essence = 1";
			}
		}
		if(author != null && !"".equals(author.trim())) {
			hql += " and author = '" + author + "'";
		}
		if(startTime != null && endTime != null) {
			hql += " and createTime between '" + startTime + "' and '" + endTime+ "'";
		}
		if(topicName != null && !"".equals(topicName.trim())) {
			if(precision != null && 1 == precision.intValue()) { //精确
				hql += " and title = '" + topicName + "'";
			} else if(precision != null && 2 == precision.intValue()) {
				hql += " and title like '%" + topicName + "%'";
			}
		}
		hql += " order by id desc";
		return hql;
	}

	public List<ForumTopic> getTopTopic(Integer pieceId) {
		Session session  =ElHbnDB.getSession();
		String hql = "From ForumTopic where pieceId = ? and top = 1 and locked = 0";
		Query query = session.createQuery(hql);
		query.setParameter(0, pieceId);
		List<ForumTopic> list = query.list();
		if(list.size() > 0) {
			return list;
		}
		return null;
	}

	/* 
	 * 获取精华帖子
	 * (non-Javadoc)
	 * @see com.elan.forum.dao.TopicDAO#getEssenceTopic(java.lang.Integer)
	 */
	public List<ForumTopic> getEssenceTopic(Integer pieceId) {
		Session session = ElHbnDB.getSession();
		String hql = null;
		boolean isEmptyPieceId = false;
		if(pieceId != null) {
			hql = "From ForumTopic where pieceId = ? and essence = 1 and locked = 0";
		} else {
			hql = "From ForumTopic where essence = 1 and locked = 0";
			isEmptyPieceId = true;
		}
		Query query = session.createQuery(hql);
		if(!isEmptyPieceId) {
			query.setParameter(0, pieceId);
		}
		List<ForumTopic> list = query.list();
		if(list.size() > 0) {
			return list;
		} 
		return null;
	}

	public Long getTopicCountById(Integer pieceId) {
		Session session = ElHbnDB.getSession();
		String hql = "SELECT COUNT(*) From ForumTopic where pieceId = ? and locked = 0";
		Query query = session.createQuery(hql);
		query.setParameter(0, pieceId);
		return (Long) query.uniqueResult();
	}

	public Long getTopicCountByHql(String hql) {
		Session session = ElHbnDB.getSession();
		if(hql == null) {
			return new Long(0);
		}
		Query query = session.createQuery(hql);
		return (Long) query.uniqueResult();
	}

	public List<ForumTopic> getTopicByHql(String hqlStr) {
		Session session = ElHbnDB.getSession();
		String hql ="From ForumTopic where 1 = 1";
		List list ;
		if(null != hqlStr) {
			hql += hqlStr;
		}
		Query query = session.createQuery(hql);
		list = query.list();
		return list;
	}
	public List<ForumTopic> getUserTopic(Integer id, Integer page, Integer pageSize) {
		Session session = ElHbnDB.getSession();
		List<ForumTopic> list;
		String hql = "From ForumTopic where  authorId = ? and locked = 0 order by id desc";
		Query query = session.createQuery(hql);
		query.setParameter(0, id);
		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		list = query.list();
		return list;
	}

	public Integer getCount(Integer authorId) {
		Session session = ElHbnDB.getSession();
		String hql = "Select Count(*) From ForumTopic where authorId = ? and locked = 0  order by id";
		Query query = session.createQuery(hql);
		query.setParameter(0, authorId);
		Long l = (Long) query.uniqueResult();
		Integer count = Integer.valueOf(l.intValue());
		return count;
	}

	public Integer getCount(String hql) {
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery(hql);
		Long l = (Long) query.uniqueResult();
		Integer count = Integer.valueOf(l.intValue());
		return count;
	}
	
	public  Integer getCount(String hql, int paras, String [] values) {
		Session session = ElHbnDB.getSession();
		Query query = session.createQuery(hql);
		if(paras > 0) {
			for(int i = 0 ; i < paras; i++) {
				query.setParameter(i, values[i]);
			}
		}
		Long l = (Long) query.uniqueResult();
		Integer count = Integer.valueOf(l.intValue());
		return count;
	}

	public Integer getHotTopicCount(Integer id) {
		String hql = "select Count(*) from ForumTopic where authorId = ? and Hot = 1 and locked = 0";
		String [] values = new String[]{"" +id};
		return this.getCount(hql, values.length, values);
	}
	public List<ForumTopic> getTopic(String hql, int paras, String [] values, Integer page, Integer pageSize) {
		Session session = ElHbnDB.getSession();
		List<ForumTopic> list;
		Query query = session.createQuery(hql);
		if(paras > 0) {
			for(int i = 0 ; i < paras; i++) {
				query.setParameter(i, values[i]);
			}
		}
		if(null != page && null != pageSize) {
			query.setFirstResult((page - 1) * pageSize);
			query.setMaxResults(pageSize);
		}
		list = (List<ForumTopic>)query.list();
		return list;
	}
	public List<ForumTopic> getUserHotTopic(Integer id, Integer page,
			Integer pageSize) {
		String hql = "From ForumTopic where authorId = ? and Hot = 1 and locked = 0 order by id desc";
		String [] values = new String[]{"" + id};
		return this.getTopic(hql, values.length, values, page, pageSize);
	}

	public Integer getEssenceTopicCount(Integer id) {
		String hql = "select Count(*) from ForumTopic where authorId = ? and essence = 1 and locked = 0";
		String [] values = new String[]{"" +id};
		return this.getCount(hql, values.length, values);
	}

	public List<ForumTopic> getUserEssenceTopic(Integer id, Integer page,
			Integer pageSize) {
		String hql = "From ForumTopic where authorId = ? and essence = 1 and locked = 0 order by id desc";
		String [] values = new String[]{"" + id};
		return this.getTopic(hql, values.length, values, page, pageSize);
	}

	/* 
	 * 这里通过connection连接,要过Hibernate
	 * (non-Javadoc)
	 * @see com.elan.forum.dao.TopicDAO#getUserReplyTopic(java.lang.Integer)
	 */
	public Integer getUserReplyTopic(Integer id) {
		String sql = "select count(*) as count from (select * from forumtopicreply where authorId = ? and locked = 0  group by topicId) as frt";
		Session session = ElHbnDB.getSession();
		Integer count = 0;
		Connection conn = session.connection();
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		try {
			pStmt = conn.prepareStatement(sql);
			pStmt.setInt(1, id);
			rs = pStmt.executeQuery();
			conn.commit();
			if(null != rs && rs.next()) {
				count = rs.getInt("count");
			}
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	/* 
	 * 获取用户回复过的帖子
	 * @see com.elan.forum.dao.TopicDAO#getUserReplyed(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<ForumTopic> getUserReplyed(Integer id, Integer page,
			Integer pageSize) {
		String hql = "select topicId From Forumtopicreply where authorId = ? and locked = 0 group by topicId order by id desc";
		String hqlForumTopic = "From ForumTopic as ft where id in(";
		Session session = ElHbnDB.getSession();
		List<Object> list;
		Integer [] ArrTopicId;
		Query query = session.createQuery(hql);
		query.setParameter(0, id);
		query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		list = query.list();
		for(int i = 0; i < list.size(); i++) {
			hqlForumTopic += (Integer)list.get(i) + ",";
		}
		hqlForumTopic +="-1) and locked = 0 order by id desc";
		List<ForumTopic> listFt;
		query =  session.createQuery(hqlForumTopic);
		//query.setFirstResult((page - 1) * pageSize);
		query.setMaxResults(pageSize);
		listFt = query.list();
		System.out.println(hqlForumTopic);
		System.out.println("第:" + (page - 1) * pageSize);
		System.out.println("一起返回:" + pageSize + "条");
		for(int i = 0; i < listFt.size(); i++) {
			System.out.println(((ForumTopic)listFt.get(i)).getTitle());
		}
		return listFt;
	}
	
}

⌨️ 快捷键说明

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