1、导包
2、数据库建模
3、创建持久化类(POJO)
注意:
①、需提供一个无参的构造器,因为hibernate使用反射来实例化持久化类(Constructor.newInstance())
否则报错:
INFO: HHH000327: Error performing load command : org.hibernate.InstantiationException: No default constructor for entity: com.hibernate.domain.Customer
package com.hibernate.domain;
public class Customer {
private int custID;
private String custName;
private String phone;
private String address;
public int getCustID() {
return custID;
}
public void setCustID(int custID) {
this.custID = custID;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Customer(int custID, String custName, String phone, String address) {
super();
this.custID = custID;
this.custName = custName;
this.phone = phone;
this.address = address;
}
public Customer() {
}
}
4、创建映射文件(xxx.hbm.xml)
Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.hibernate.domain.Customer" table="customer">
<!-- name:对应的持久化类的全类名 table:对应的表名 -->
<!-- 下面建立类中的字段 和 表中列的对应关系
id:建立类中字段与表的主键之间的对应关系
name:类中的字段名
column:数据库中的列名
generator:主键生成策略
-->
<id name="custID" column="cust_id">
<generator class="native"></generator>
</id>
<!--
property:建立类中字段与表中普通列之间的对应关系
name:类中的字段名
column:数据库中的列名
-->
<property name="custName" column="cust_name"></property>
<property name="phone" column="cust_phone"></property>
<property name="address" column="cust_addr"></property>
</class>
</hibernate-mapping>
5、创建Hibernate核心配置文件(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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate1</property>
<property name="hibernate.connection.username">root</property>
<!-- 数据库方言(此处选为mysql) -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 控制台是否打出sql -->
<property name="hibernate.show_sql">true</property>
<!-- sql语句是否格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 映射文件加载 -->
<mapping resource="com/hibernate/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、测试类
package com.hibernate.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 com.hibernate.domain.Customer;
public class Test {
@org.junit.Test
public void test() {
//1、创建Configuration对象,对应hibernate的基本配置信息和对象关系映射信息(即hibernate.cfg.xml 及 XXX.hbm.xml信息)
Configuration configuration = new Configuration().configure();
//2、 创建一个 ServiceRegistry 对象
//hibernate 的任何配置和服务都需要在该对象中注册后才能有效.
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
//3、创建sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
//4. 创建一个 Session 对象
Session session = sessionFactory.openSession();
//5、 开启事务
Transaction transaction = session.beginTransaction();
//6、查询编码(主键)为1 的用户
Customer customer = (Customer)session.get(Customer.class,1);
System.out.println(customer);
//7、提交事务
transaction.commit();
//8、关闭资源
session.close();
sessionFactory.close();
}
}