当前位置:编程学习 > JAVA >>

Hibernate中的web开发模式

新建sessionFactoryUtil
Java代码 
package SessionFactoryUtil; 
 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
 
public class MySessionFactory { 
    private static SessionFactory sessionFactory; 
    static{ 
        Configuration config=new Configuration(); 
        config.configure(); 
        sessionFactory=config.buildSessionFactory(); 
    } 
    public static SessionFactory getSessionFactory() 
    { 
        return sessionFactory; 
    } 

 在一个Action中调用
 
Java代码 
HttpServletRequest request = ServletActionContext.getRequest(); 
        HttpServletResponse response=ServletActionContext.getResponse(); 
        HttpSession session=request.getSession(); 
        Session sess = MySessionFactory.getSessionFactory().getCurrentSession(); 
        Transaction tx = sess.beginTransaction(); 
        Query q1=sess.createQuery("from Category"); 
        List<Category> list=q1.list(); 
        tx.commit(); 
         
        request.setAttribute("listCategory", list); 
        return "addArticle"; 

作者“helinhang”
 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,