1. 首先在eclipse里建立一个Java Project,命名为HibernateFreshBird。在src的目录下建立一个实体对象Person,包名为com.hellopclee.hibernate.test
为Person定义两个属性,id 和 name,并为它们实现get和set方法。
package com.hellopclee.hibernate.test;
public class Person {
int id;
String name;
public Person() {
super();
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
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;
}
}
2.
到http://hibernate.org/orm/downloads/ 下载Hibernate的文件。我的版本是4.3.2,不同版本在配置上会有些小的变化。下载后解压,把/lib/required目录下的jar包加到HibernateFreshBird项目中来。为了方便使用,可以定义User liberary。右键HibernateFreshBird项目,HibernateFreshBird -> Buid Path ->Configure Buid Path。选择Add Library –> User Library,再点击User Libraries -> New,新建一个自定义的Library,把名字命名为HibernateLib,/lib/required目录下的文件都添加进去。然后回到Add Library的对话框,把HibernateLib勾选上。点击Finish。
3. 顺便把Mysql的驱动也加到项目中来,方法同上。这时候你的项目应该是这个样子的。
4. 在com.hellopclee.hibernate.test下再建立一个映射文件,命名为person.hbm.xml,这个文件的作用是告诉定义数据库中的表和Person类的映射关系。
<?xml version='1.0'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hellopclee.hibernate.test">
<class name="Person" table="person">
<id name="id" type="int" column="Id">
<generator class="assigned" />
</id>
<property name="name">
<column name="Name" />
</property>
</class>
</hibernate-mapping>
5. 再在src下定义一个配置文件,定义HibernateFreshBird项目和数据库的连接。例如我的数据库用的是mysql,连接的是test这个schema, 用户名是root,没有密码,还有告诉HibernateFreshBird通过person.hbm.xml 来建立映射关系。
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/hellopclee/hibernate/test/person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6. 在mysql里建立一个表。
create table person (
id int,
name varchar(10)
)
7. 新建一个HibernateUtil,来初始化和数据库的连接。
package com.hellopclee.hibernate.test;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration cfg = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
return sf;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
8. 现在就可以体验hibernate的威力了。新建一个PersonDetail类,
package com.hellopclee.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.service.ServiceRegistry;
public class PersonDetail {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person person = new Person();
person.setId(123);
person.setName("pclee");
session.save(person);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
运行,你会发现在mysql的Person表里多了一项,123:pclee。如果出错,请根据提示信息作相应的修改。