跨向Hibernate(一)

阅读更多
从读Hibernate的手册开始了学习Hibernate的历程。
《与猫同乐》----乐的有些尴尬,因为在99%时,测试用了普通Java类,长时间滞留不前,把我的猫忘在门外了
 
1、配置数据源。
在Tomcat5中,修改conf\tomcat-users.xml文件,加入。启动Tomcat,进入管理页面,添加数据源,最终有如下信息加入项目的配置文件中:
 
 
   
      maxWait
      5000
   

   
      maxActive
      4
   

   
      password
      secret
   

   
      url
      jdbc:mysql://localhost/test
   

   
      driverClassName
      com.mysql.jdbc.Driver
   

   
      maxIdle
      2
   

   
      username
      quickstart
   

 
 
2、添加Hibernate库
把所需的库加入类路径中,或是项目Lib中
 
3、编写hibernate.cfg.xml文件,置于项目编译后的根目录下(在Eclipse或其他IDE中,放在源文件根目录下即可),内容类似下示:


    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    " http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
 
 
   
 
        java:comp/env/jdbc/quickstart
        false
        net.sf.hibernate.dialect.PostgreSQLDialect
 
       
       
 
   
 
 
 
 
4、编写相应的映射类和配置文件,并放在同一目录下,例如:
 
package net.sf.hibernate.examples.quickstart;
 
public class Cat {
 
    private String id;
    private String name;
    private char sex;
    private float weight;
 
    public Cat() {
    }
 
    public String getId() {
        return id;
    }
 
    private void setId(String id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public char getSex() {
        return sex;
    }
 
    public void setSex(char sex) {
        this.sex = sex;
    }
 
    public float getWeight() {
        return weight;
    }
 
    public void setWeight(float weight) {
        this.weight = weight;
    }
 
}
 


    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    " http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
 
 
   
 
       
       
           
           
       
 
       
       
           
       
 
       
 
       
 
   
 


*5、编写自定义SessionFactory类,例如:(此为Hibernate自带的示例)
 
HibernateUtil.java
 
package net.sf.hibernate.examples.quickstart;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
public class HibernateUtil {
 
    private static Log log = LogFactory.getLog(HibernateUtil.class);
 
    private static final SessionFactory sessionFactory;
 
    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            log.error("Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
 
    public static final ThreadLocal session = new ThreadLocal();
 
    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }
 
    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null)
            s.close();
    }
 
}
6、编写测试Jsp或Serverlet,注意,这里是在Tomcat环境下,不能用普通Java类,示例Jsp
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="net.sf.hibernate.Session"%>
<%@ page import="net.sf.hibernate.HibernateException"%>
<%@ page import="net.sf.hibernate.Transaction"%>
<%@ page import="net.sf.hibernate.examples.quickstart.Cat"%>
<%@ page import="net.sf.hibernate.examples.quickstart.HibernateUtil"%>
 
 

<%
          try{
         Session ssession;
            ssession = HibernateUtil.currentSession();
            Transaction tx= ssession.beginTransaction();
            Cat princess = new Cat();
            princess.setName("Princess");
            princess.setSex('F');
            princess.setWeight(7.4f);
 
            ssession.save(princess);
            tx.commit();
 
            HibernateUtil.closeSession();
            }catch(HibernateException e){
            }
           
%>

 
 


 

 
 

你可能感兴趣的:(Hibernate,.net,SQL,Tomcat,JDBC)