1.登录http://www.hibernate.org/downloads.html站点,下载Hibernate Core,目前最新版本是3.3.1.GA,Windows平台下载.zip包(hibernate-distribution-
3.3.1.GA-dist.zip).将下载的.zip包解压.
2.鼠标右击工程项目,选择properties-->Java Build Path-->Libraries,点击Add External Jars,把10个jar包导入进来.10个jar包如下:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate3.jar
hibernate-cglib-repack-2.1_3.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-nop-1.5.2.jar(注:需要单独下载slf4j-1.5.2.zip,解压后就有slf4j-nop-1.5.2.jar)
3.编写hibernate.cfg.xml文件,将其放在项目的src目录下.
<?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>
<!-- Database connection settings(MicroSoft SQL SERVER 2000) -->
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cdh</property>
<property name="connection.username">sa</property>
<property name="connection.password">1234</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<!-- mypack/User.hbm.xml是我接下来要创建的配置文件 -->
<mapping resource="mypack/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.编写POJO.
package mypack;
import java.util.Date;
class User {
private int id;
private String name;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5.编写POJO的配置文件(User.hbm.xml),将其放在POJO同一个包下.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="mypack">
<class name="User" table="USERS">
<comment>Users may bid for or sell auction items.</comment>
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
6.在数据库中创建USERS表.
use cdh
CREATE TABLE users(
id int NOT NULL PRIMARY KEY,
name varchar(20),
birthday datetime
)
7.编写测试代码.
package mypack;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UserAppliction {
/**
* @param args
*/
public static void main(String[] args) {
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = null;
User user = new User();
user.setName("Phil");
user.setBirthday(new Date());
try {
tx = session.beginTransaction();
session.save(user);
tx.commit();
} catch(Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
System.out.println("Test Success!");
}
}
运行结果:
Hibernate: select max(id) from USERS
Hibernate: insert into USERS (name, birthday, id) values (?, ?, ?)
Test Success!
现在可以在查询分析器中写以下查询语句查看刚插入的数据.
use cdh
select * from users
3.3.1.GA-dist.zip).将下载的.zip包解压.
2.鼠标右击工程项目,选择properties-->Java Build Path-->Libraries,点击Add External Jars,把10个jar包导入进来.10个jar包如下:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate3.jar
hibernate-cglib-repack-2.1_3.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-nop-1.5.2.jar(注:需要单独下载slf4j-1.5.2.zip,解压后就有slf4j-nop-1.5.2.jar)
3.编写hibernate.cfg.xml文件,将其放在项目的src目录下.
<?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>
<!-- Database connection settings(MicroSoft SQL SERVER 2000) -->
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cdh</property>
<property name="connection.username">sa</property>
<property name="connection.password">1234</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<!-- mypack/User.hbm.xml是我接下来要创建的配置文件 -->
<mapping resource="mypack/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.编写POJO.
package mypack;
import java.util.Date;
class User {
private int id;
private String name;
private Date birthday;
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5.编写POJO的配置文件(User.hbm.xml),将其放在POJO同一个包下.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="mypack">
<class name="User" table="USERS">
<comment>Users may bid for or sell auction items.</comment>
<id name="id">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
6.在数据库中创建USERS表.
use cdh
CREATE TABLE users(
id int NOT NULL PRIMARY KEY,
name varchar(20),
birthday datetime
)
7.编写测试代码.
package mypack;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UserAppliction {
/**
* @param args
*/
public static void main(String[] args) {
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
Session session = sf.openSession();
Transaction tx = null;
User user = new User();
user.setName("Phil");
user.setBirthday(new Date());
try {
tx = session.beginTransaction();
session.save(user);
tx.commit();
} catch(Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
System.out.println("Test Success!");
}
}
运行结果:
Hibernate: select max(id) from USERS
Hibernate: insert into USERS (name, birthday, id) values (?, ?, ?)
Test Success!
现在可以在查询分析器中写以下查询语句查看刚插入的数据.
use cdh
select * from users