📄 articledaohibernateimpl.java
字号:
package com.test.bbs.dao.impl;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.test.bbs.dao.ArticleDao;
import com.test.bbs.domain.Reply;
import com.test.bbs.domain.Subject;
import com.test.bbs.service.impl.Page;
public class ArticleDaoHibernateImpl implements ArticleDao {
Log logger = LogFactory.getLog(this.getClass());
//添加回复
public void addReply(Reply reply) {
Session session = HibernateTools.getSessionFactory()
.getCurrentSession();
session.save(reply);
}
//添加主题帖
public void addSubject(Subject subject) {
Session session = HibernateTools.getSessionFactory()
.getCurrentSession();
session.save(subject);
}
//删除回复
public void delReply(Reply reply) {
HibernateTools.getSessionFactory().getCurrentSession().delete(reply);
}
//删除主题
public void delSubject(Subject subject) {
HibernateTools.getSessionFactory().getCurrentSession().delete(subject);
}
public Subject getSubject(Integer id) {
Session session = HibernateTools.getSessionFactory()
.getCurrentSession();
Subject subject = (Subject) session.get(Subject.class, id);
return subject;
}
public void updateSubject(Subject subject) {
HibernateTools.getSessionFactory().getCurrentSession().update(subject);
}
public List getHotSubjects() {
Session session = HibernateTools.getSessionFactory()
.getCurrentSession();
Criteria c = session.createCriteria(Subject.class);
c.addOrder(Order.desc("replyCount"));
c.setMaxResults(10);
return c.list();
}
public Reply getReply(Integer id) {
Session session = HibernateTools.getSessionFactory()
.getCurrentSession();
Reply reply = (Reply) session.get(Reply.class, id);
return reply;
}
public Page getPage(Subject subject, int pageNo, int pageSize) {
logger.info("11111111111111");
Session session = HibernateTools.getSessionFactory()
.getCurrentSession();
Criteria c = session.createCriteria(Reply.class);
c.add(Restrictions.eq("subject", subject));
c.setProjection(Projections.rowCount());
int totalCount = (Integer) c.uniqueResult();
c = session.createCriteria(Reply.class);
c.add(Restrictions.eq("subject", subject));
c.setFirstResult((pageNo - 1) * pageSize);
c.setMaxResults(pageSize);
List replys = c.list();
Page page = new Page(replys, totalCount, pageSize, pageNo);
logger.info("222222222222222222");
logger.info("page:" + page);
return page;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -