`

Hibernate入门

阅读更多

1.新建项目

 

2.New User Library
 a.Window--->Preferences--->Java--->Build Path--->User Library--->点击New,输入hibernate3.3;
 b.Add JARs
  hibernate-distribution-3.3.2.GA\hibernate3.jar;
  hibernate-distribution-3.3.2.GA\lib\required下所有jar;
  slf4j-1.5.8\slf4j-nop-1.5.8.jar。
 c.选择工程--->new--->Build Path--->Add Library--->User Library,next--->选中hibernate3.3,Finish。
 
3.导入数据库驱动包
 mysql-connector-java-5.0.8-bin.jar
 
4.建立数据库及表
 

show databases;
 create database hibernate;
 create table student(
  id int primary key,
  name varchar(20),
  age int,
  birthday date
 );

  
5.在src建立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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></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/fengyun/shopping/entity/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 
6.包com.fengyun.shopping.entity下建立Student类

package com.fengyun.shopping.entity;

public class Student {
 private Integer id;
 
 private String name;
 private Integer age;
 
 public Student() {
  super();
 }

 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 Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

}

 
7.为Student类建立映射文件:Student.hbm.xml

<?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.fengyun.shopping.entity.Student">
   <id name="id"/>
   <property name="name"/>
    <property name="age"/>
  </class>
</hibernate-mapping>

 

8.将映射文件添加到hibernate.cfg.xml中
<mapping resource="com/fengyun/shopping/entity/Student.hbm.xml"/>


9.测试

package com.fengyun.shopping.entity;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class StudentTest {
 public static void main(String[] args) {
  Student s = new Student();
  s.setId(1);
  s.setName("wanghao");
  s.setAge(12);
  
  Configuration cfg = new Configuration();
  SessionFactory sf = cfg.configure().buildSessionFactory();
  Session session = sf.openSession();
  session.beginTransaction();
  session.save(s);
  session.getTransaction().commit();
  session.close();
  sf.close();
 }
}

 
10.建议
启动Hibernate,此过程包括创建一个全局的SessoinFactory,并把它储存在应用程序代码容易访问的地方。SessionFactory可以创建并打开新的Session。一个Session代表一个单线程的单元操作,SessionFactory则是个线程安全的全局对象,只需要被实例化一次。

我们将创建一个HibernateUtil辅助类(helper class)来负责启动Hibernate和更方便地操作SessionFactory

package util;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics