不喜愛用註解,用的都是xml文件配置形式java
一。下載hibernatemysql
http://sourceforge.net/projects/hibernate/sql
二。解壓-->lib--->required(必須的架包,其他的可選)所有加入MyEclipse的lib目錄下session
三。編寫實體類(Book.java)app
package org.fary.entity;ide
import java.io.Serializable;測試
/**ui
* @author jerrythis
* @Date 2013-4-27編碼
* @Email 1457049206@qq.com
*
* 實體類
*/
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String price;
private String author;
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 getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
五。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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/user?useUnicode=true&amp;characterEncoding=UTF-8
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.internal.NoCachingRegionFactory</property>
<!-- 字符編碼 -->
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="org/fary/entity/Book.hbm.xml" />
</session-factory>
</hibernate-configuration>
六。SessionFactory類(HibernateSessionFactory)
package org.fary.utils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
* @author jerry
* @Date 2013-4-27
* @Email 1457049206@qq.com
*
* sessionFactory 工廠
*/
public class HibernateSessionFactory {
private static Configuration configuration = null;
private static SessionFactory sessionFactory = null;
private static ServiceRegistry serviceRegistry = null;
/*
* ServiceRegistry是Hibernate4.X新增接口,
* 應用於Hibernate的配置或者服務等將統一貫這個ServiceRegistry註冊後才能生效。
* 因此須要構建一個ServiceRegistry對象
* ,將配置信息向它註冊,而後Configuration對象根據從這個ServiceRegistry對象中獲取配置信息生成SessionFactory
* 。 Hibernate4的配置入口再也不是Configuration對象,而是ServiceRegistry對象,
* Configuration對象將經過ServiceRegistry對象獲取配置信息。 hibernate4
* 源碼位置:org.hibernate.service.ServiceRegistryBuilder 具體可參看hibernate源碼。以及API
*/
static {
try {
// 首先獲取配置信息
configuration = new Configuration().configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
// 建立Session Factory
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e) {
// System.out.println("SessionFactory建立失敗");
e.printStackTrace();
}
}
public static Session getSession() {
Session session = null;
// Session 是否開啓
if (null == session || false == session.isOpen()) {
session = sessionFactory.openSession();
}
return session;
}
}