?? elantopicdaoimpl.java
字號:
/**
* 獲取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 + -