日期:2014-05-18 浏览次数:20862 次
package net.roseindia.dao;
import java.util.Date;
import java.util.List;
import net.roseindia.model.Article;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("articleDao")
public class ArticleDaoImpl implements ArticleDao {
@Autowired
private SessionFactory sessionFactory;
// To Save the article detail
public void saveArticle(Article article) {
article.setAddedDate(new Date());
sessionFactory.getCurrentSession().saveOrUpdate(article);
}
public void deleteArticle(Integer articleId){
System.out.println("Dao-->"+articleId);//能够打印出id号
Article article = (Article) sessionFactory.getCurrentSession().load(
Article.class,articleId);
sessionFactory.getCurrentSession().beginTransaction();
if (null != article) {
sessionFactory.getCurrentSession().delete(article);
}
}
// To get list of all articles
@SuppressWarnings("unchecked")
public List<Article> listArticles() {
return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
}
}
package net.roseindia.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "article")
public class Article {
@Id
@GeneratedValue
@Column(name = "article_id")
private Integer articleId;
@Column(name = "article_name", nullable = false, length=20)
private String articleName;
@Column(name = "article_desc", nullable = false)
private String articleDesc;
@Column(name = "date_added")
private Date addedDate;
public Article() {
}
&n