package com.dao;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.bean.News;
import com.util.HibernateSessionFactory;
public class NewsDao {
//设置当前时间,修改日志 所取得的时刻
Date now = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ds = df.format(now);
public List<News> getAllNews(){
List<News> list = null;
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
Query query =session.createQuery("from News order by dates desc");
list = query.list();
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
return list;
}
/* public News getById(News news){
News n = new News();
List<News> list = null;
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
Query query =session.createQuery("from News where id='"+news.getId()+"'");
list = query.list();
for (int i = 0; i <list.size(); i++) {
n = list.get(i);
}
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
return n;
}*/
public News getById(News news){
News n = new News();
List<News> list = null;
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
try {
session.beginTransaction();
n = (News)session.get(News.class, news.getId());
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
return n;}
public int getAddCount(News news){
int flag = 0;
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
Query q = session.createQuery("update News n set n.count=n.count+1 where n.id='"+news.getId()+"'");
flag = q.executeUpdate();
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
return flag;
}
public void getAddNews(News news){
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
news.setId(new BigDecimal(0));
session.save(news);
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
}
public void getUpdateNews(News news){
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
news.setLastupdate(ds);
session.update(news);
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
}
public void getDeleteNews(News news){
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
session.delete(news);
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
}
/**
* 实现分页技术
*/
// 获取查询内容 分页
public List<News> findwithpage(int pagesize,int currentrow){
List<News> list = null;
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
Query query =session.createQuery("from News order by dates desc").setFirstResult(currentrow).setMaxResults(pagesize);
list = query.list();
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
return list;
}
//新闻的记录总数
public int getRows(){
long totalrows = 0;
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
Session session = HibernateSessionFactory.getSession();
Transaction ts = null;
try {
ts = session.beginTransaction();
Object ob =(session.createQuery("select count(*) from News").iterate().next());
totalrows = Integer.parseInt(ob.toString());
//Query q = session.createQuery("select count(*) from News"); //获得查询条件行数
//list = q.list();
//flag = (Long)list.get(0);
ts.commit();
} catch (Exception e) {
e.printStackTrace();
}
finally{
session.close();
}
return (int) totalrows;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Struts2+Hibernate新闻发布系统.rar (54个子文件)
export.sql 6KB
News
.mystrutsdata 240B
.myhibernatedata 534B
.settings
org.eclipse.jdt.core.prefs 330B
src
com
dao
UserDao.java 911B
NewsDao.java 5KB
action
NewsAction.java 3KB
UsersAction.java 1KB
bean
News.hbm.xml 1KB
Users.java 929B
Users.hbm.xml 807B
News.java 2KB
util
HibernateSessionFactory.java 3KB
struts.xml 1KB
.struts.mex 76B
hibernate.cfg.xml 1003B
.project 2KB
WebRoot
css
styles.css 517B
images
index_bottom.gif 1KB
Spacer.gif 4KB
CSS.CSS 617B
index_bg.gif 284B
index_top2.gif 12KB
index_top3.gif 17KB
01.gif 28KB
index_pic.gif 5KB
index_top1.gif 4KB
index_top4.gif 8KB
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 671B
classes
com
dao
UserDao.class 2KB
NewsDao.class 5KB
action
UsersAction.class 2KB
NewsAction.class 3KB
bean
News.class 2KB
News.hbm.xml 1KB
Users.hbm.xml 807B
Users.class 1KB
util
HibernateSessionFactory.class 2KB
struts.xml 1KB
.struts.mex 76B
hibernate.cfg.xml 1003B
lib
classes12.jar 1.52MB
content_news.jsp 3KB
users
login.jsp 3KB
index.jsp 4KB
addnews.jsp 3KB
error.jsp 3KB
updatebegin_news.jsp 4KB
index.jsp 3KB
error.jsp 862B
index_news.jsp 4KB
.mymetadata 285B
.classpath 747B
.myeclipse
共 54 条
- 1
IT_Jeff
- 粉丝: 16
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页