在ssh框架中, 怎么在DAO 中写方法, 自己定义的方法, 难道非要用hql语句吗、? ?  谢谢

解决方案 »

  1.   

    继承HibernateDaoSupport 就行了
      

  2.   

    2楼,我们在用ssh框架的时候,是不是它本身给我们生成了很多的方法, 几乎够我们用了, 可是, 我想自己重写个方法,我也刚刚把ssh学完, 不太会用, 请问,我自己在数据访问层中,我自己怎么写方法 , 谢谢
      

  3.   

    可以继承HibernateDaoSupport的方法也可以自己定义方法了
      

  4.   

    我一般都是自己写方法的,Dao层无非就是用来操作数据库的啊,当然是用hibernate的hql语句实现的话 就方便多了,尤其是hibernate的查询非常强大的,可以实现类似预编译语句,多种条件限制等等顺便贴出我最近做的一个项目
    的一个DAO实现吧(历史记录部分)
    不是很好啊,STRUTS+HIbernate实现
    package dao;
    import actionform.*;
    import java.util.List;import javax.management.Query;import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;import actionform.HistoryForm;
    import actionform.NewsInfoForm;
    public class NewsDao { private static SessionFactory sessionfactory=null;
    private static Session session=null;
    private Transaction tx=null;
        static{
        
    try{

    Configuration config=new Configuration().configure();
         sessionfactory=config.buildSessionFactory();
    }
    catch(Exception e)
    {}
        }
        
        public int deleteNews(Integer id)
        {
         session=sessionfactory.openSession();
        
         try{
        
         Transaction tx=session.beginTransaction();
         NewsInfoForm form1=(NewsInfoForm) session.load(NewsInfoForm.class, id);
         session.delete(form1);
         tx.commit();
            return 1;
        
         }catch(Exception e){return 0;}
          
        
        }
        public boolean addNews(NewsInfoForm form)
        {
         session=sessionfactory.openSession();
         tx=session.beginTransaction();
         try{
         session.save(form);
        
         tx.commit();
         return true;
         }
         catch(Exception e){return false;}
        
        }
        public boolean  update(NewsInfoForm  form)
        {   
         session=sessionfactory.openSession();
         tx=session.beginTransaction();
            try{
             session.load(NewsInfoForm.class, form.getId());
             session.update(form);
             tx.commit();
             return true;
            }catch(Exception e){ return false;}
        }

        public List selectnews()
        {
         session=sessionfactory.openSession();
         tx=session.beginTransaction();
         try{
         String hql="from NewsInfoForm";
         org.hibernate.Query query=session.createQuery(hql);
         List list=query.list();
         return list;
         }
         catch(Exception e){}
         return null;
        
        }
        public List selectmany(String str,String content)
        {
         session=sessionfactory.openSession();
         tx=session.beginTransaction();
         try{
         String hql="from NewsInfoForm where name like '%"+str+"%'"+"and content like '%"+content+"%'";
         org.hibernate.Query query=session.createQuery(hql);
         List list=query.list();
         return list;
         }
         catch(Exception e){}
         return null;
        }
        public NewsInfoForm selectSingle(Integer id)
        {
         session=sessionfactory.openSession();
         try{
         tx=session.beginTransaction();
         NewsInfoForm form=(NewsInfoForm)session.load(NewsInfoForm.class, id);
         tx.commit();
         return form;
         }catch(Exception e){return null;}
        }
        
        
        
    }
      

  5.   

    Hibernate也支持原生的SQL语句的啊,有一个方法的,你去查一下API 我也记不得了
      

  6.   

    hibernate只封装了一些常用的增删改查的方法,但是主要的方法编写还是要看你程序中的变化,毕竟hibernate中的方法都是一些已经定义死了的方法嘛,灵活性往往没有自己编写的那么强
      

  7.   

    不一定非要hql语句,你也可以用qbc的方式。
      

  8.   

    也可以使用sql语句!只不过是你使用hql的时候用的是Query对象,使用sql的时候用的是SQLQuery对象
      

  9.   

    这段代码中,每个session打开执行提交后,都没有关闭session,这样子设计是不是有点问题!
      

  10.   

     session.createSQLQuery(sql);
    hibernate中使用sql语句