天天看點

最基本的hibernate架構

不喜好用注解,用的都是xml檔案配置形式

一。下載下傳hibernate

http://sourceforge.net/projects/hibernate/

二。解壓-->lib--->required(必須的架包,其餘的可選)全部加入MyEclipse的lib目錄下

三。編寫實體類(Book.java)

package org.fary.entity;

import java.io.Serializable;

/**

 * @author jerry

 * @Date 2013-4-27

 * @Email [email protected]

 * 

 *        實體類

 */

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;

}

四。Book.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC 

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.fary.entity">

<class name="Book" table="book" catalog="user">

<id name="id" column="b_id">

<generator class="identity" />

</id>

<property name="name" column="name" />

<property name="author" column="author" />

<property name="price" column="price" />

</class>

</hibernate-mapping>

五。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;amp;characterEncoding=UTF-8

<property name="connection.username">root</property>

<property name="connection.password">123456</property>

<property name="connection.driver_class">

com.mysql.jdbc.Driver

<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;

 *        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;

七。測試

package org.fary.test;

import java.util.ArrayList;

import java.util.List;

import org.fary.entity.Book;

import org.fary.entity.Student;

import org.fary.utils.HibernateSessionFactory;

 *        hibernate的測試類

public class Test {

private static void book() {

Session session = HibernateSessionFactory.getSession();

session.beginTransaction();

Book book = new Book();

book.setName("hibernate4");

book.setAuthor("fary");

book.setPrice("45.5");

session.save(book);

session.getTransaction().commit();

session.close();

public static void main(String[] args) {

book();

繼續閱讀