Hibernate's HelloWorld


1.创建自己的Web应用程序的骨架:

webApp/

            classes/

                        hibernate.cfg.xml

       web.xml

                       Customer.hbm.xml

                        java files

            lib/存放hibernate和连接数据库所需要的包:

2.文件的内容为:

hibernate.cfg.xml:

<?xml version='1.0' encoding="UTF-8"?>  
<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/customers</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">yang</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping resource="Customer.hbm.xml" />
    </session-factory>
</hibernate-configuration>

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>My Web Application</display-name>
</web-app>

Customer.hbm.xml:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="yang.www.Customer" table="customer">
        <id name="id" column="ID" type="long" length="10">
            <generator class="increment" />
        </id>
        <property name="name" column="NAME" type="string" length="20"
            not-null="true" />
        <property name="year" column="YEAR" type="int" not-null="true" />
    </class>
</hibernate-mapping> 

JavaBean:

package yang.www;

import java.io.Serializable;

public class Customer implements Serializable
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private long id;
    private String name;
    private int year;
    
    public long getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public int getYear() {
        return year;
    }
    public void setId(long id) {
        this.id = id;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setYear(int year) {
        this.year = year;
    }
    
    
}

测试类:

package yang.www;

import org.hibernate.*;  
import org.hibernate.cfg.Configuration;  
public class HibernateMain
{  
    public static void main(String[] args) {  
        Configuration config = new Configuration();  
        config.configure();                                                   // 配置文件名不是hibernate.cfg.xml时将文件名传入  
        SessionFactory sessionFactory = config.buildSessionFactory();    // 相当于JDBC的注册驱动  
        Session session = sessionFactory.openSession();                    // 相当于JDBC的getConnection  
        Transaction tx = session.beginTransaction();                    // Hibernate操作必须启动事务
        
        Customer user = new Customer();                                    // 创建持久化对象  
        user.setName("YangZhiYong");  
        user.setYear(22);  
        session.save(user);                                                // 保存对象  
        tx.commit();                                                    // 持久化到数据库中  
    }  
}

然后运行这个测试类。如果没有意外的话,会显示如下的内容:

如果显示的是这样子的内容,表示你成功地运行Hibernate了。否则再认真测试一下,看一下配置。

相关内容