一、创建Hibernate 配置文件 hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!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>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">gcki_test</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.4.9:1521:orcl</property>
<property name="hibernate.connection.username">gcki_test</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="connection.isolation">2</property>
<mapping resource="com/yang/model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
二、创建持久化类:将数据库表映射成的一个对象
package com.yang.model;
public class User {
private Integer id;
private String name;
private String telPhone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelPhone() {
return telPhone;
}
public void setTelPhone(String telPhone) {
this.telPhone = telPhone;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", telPhone=" + telPhone + "]";
}
}
三、创建对象-关系映射文件:声明数据库表和持久化类之间的关系
<?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>
<class name="com.yang.model.User" table="RE_HIBERNATE_USER">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="telPhone" type="java.lang.String">
<column name="TELPHONE" />
</property>
</class>
</hibernate-mapping>
四、通过 Hibernate API 编写访问数据库的代码
package Test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.yang.model.User;
public class StanderTest {
private SessionFactory sessionFactory;
private Configuration configuration;
private Session session;
private Transaction transaction;
@Before
public void init(){
configuration=new Configuration().configure();
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destory(){
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testSave(){
User user=new User();
user.setName("miemie");
user.setTelPhone("258369");
session.save(user);
}
}