- 概述
hibernate是对象关系映射解决方案。设计目标是将软件开发人员从大量相同的数据持久层相关编程工作中解放出来,让程序员更多的关注业务逻辑的设计。
- helloworld
1 Person.java.需要持久化的pojo类
/**
* Cappuccino's Class File
*/
package com.aaron.orm;
/**
* @author Cappuccino 2012-6-11下午05:57:23
*/
public class Person {
private long id;
private String name;
private String address;
/**
* @return the id
*/
public long getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(long id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public String getAddress() {
return address;
}
/**
* @param address
* the address to set
*/
public void setAddress(String address) {
this.address = address;
}
}
2 在pojo类所在的包下配置xml文件:person.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.aaron.orm.Person" table="person">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="name" column="name" />
<property name="address" column="address" />
</class>
</hibernate-mapping>
注意:XML配置文件头很容易出错,建议直接copy.下面的hibernate.cfg.xml也一样。
3 对Hibernate进行配置,导入Hibernate依赖包。文件:hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/aaron/orm/person.hbm.xml" />
</session-factory>
</hibernate-configuration>
4 测试类test.java的编写
/**
* Cappuccino's Class File
*/
package com.aaron.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.aaron.orm.Person;
/**
* @author Cappuccino 2012-6-11下午06:24:10
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person person = new Person();
person.setName("Cappuccino");
person.setAddress("Cappuccino's house");
Configuration cf = new Configuration();
SessionFactory sf = cf.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(person);
session.getTransaction().commit();
session.close();
sf.close();
}
}
5 文件结构如下:
参考: