歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hibernates HelloWorld

Hibernates HelloWorld

日期:2017/3/1 10:41:18   编辑:Linux編程

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了。否則再認真測試一下,看一下配置。

Copyright © Linux教程網 All Rights Reserved