处理新闻分类的DAO

来源:互联网 发布:codelite mac 编辑:程序博客网 时间:2024/05/16 01:38
package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import org.hibernate.cfg.AnnotationConfiguration;

import entity.*;


public class NewsTypeDao {

   
    
    public ListcelAllNewsType(){
      AnnotationConfiguration ac = newAnnotationConfiguration().configure("hibernate.cfg.xml");
      SessionFactory sf =ac.buildSessionFactory();
      Session session = sf.openSession();
      Query q = session.createQuery("fromNewsType");
      List list = q.list();
      session.close();
      return list;
    }
    
    

    public ListselectAll(){
      AnnotationConfiguration ac = newAnnotationConfiguration().configure("hibernate.cfg.xml");
      SessionFactory sf =ac.buildSessionFactory();
      Session session = sf.openSession();
      String hql="from NewsType";
      Query q=session.createQuery(hql);
      Listli=q.list();
      session.close();
      return li;
      
      
    }
   
    publicNewsType getNewsTypeById(int tid){
      AnnotationConfiguration ac = newAnnotationConfiguration().configure("hibernate.cfg.xml");
      SessionFactory sf =ac.buildSessionFactory();
      Session session = sf.openSession();
      NewsType ntype =(NewsType)session.get(NewsType.class,tid);
      
      
      session.close();
      
      return ntype;
      
    }
    
   
    
    public voiddelNewsTypeById(int tid){
      NewsType ntype = this.getNewsTypeById(tid);
      AnnotationConfiguration ac = newAnnotationConfiguration().configure("hibernate.cfg.xml");
      SessionFactory sf =ac.buildSessionFactory();
      Session session = sf.openSession();
      Transactiontran=session.beginTransaction();
      
      session.delete(ntype);
      tran.commit();
      session.close();
      
    }
    
   
    
    
    public intaddNewsType(NewsType ntype){
      int i=0;
      AnnotationConfiguration ac = newAnnotationConfiguration().configure("hibernate.cfg.xml");
      SessionFactory sf =ac.buildSessionFactory();
      Session session = sf.openSession();
      Transactiontran=session.beginTransaction();
      if(ntype!=null){
      session.save(ntype);
      tran.commit();
         i=1;
      }
      session.close();
      return i;
      
    }
    
   
    
    public voidmodifyNewsType(NewsType ntype){
    
      AnnotationConfiguration ac = newAnnotationConfiguration().configure("hibernate.cfg.xml");
      SessionFactory sf =ac.buildSessionFactory();
      Session session = sf.openSession();
      Transactiontran=session.beginTransaction();
      session.update(ntype);
      tran.commit();
      
      session.close();
      
    }
    
    
    
    
    
    
    
}

0 0