Hibernate环境配置以及使用方法

来源:互联网 发布:tcl液晶电视网络连接 编辑:程序博客网 时间:2024/05/16 18:14

一.hibernate简介

        Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。

二.hibernate环境搭建

1.导入hibernate核心jar包

         需要导入hibernate3.jar和lib/required文件下所有的jar包再加上一个hibernate-jpa-2.0-api-1.0.1.Final.jar即可。2.添加hibernate核心配置文件hibernate.cfg.xml
01.<!DOCTYPE hibernate-configuration PUBLIC  02.    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  03.    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  04.<hibernate-configuration>  05.    <session-factory >  06.        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  07.        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>  08.        <property name="hibernate.connection.username">root</property>  09.        <property name="hibernate.connection.password">123456</property>  10.       <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  11.        <property name="hibernate.show_sql">true</property>  12.        <property name="hibernate.hbm2ddl.auto">update</property>  13.        <mapping resource="com/zhouxiang/model/User.hbm.xml"/>  14.    </session-factory>  15.</hibernate-configuration>  
    
其中<hibernate-configuration>为配置文件的根,session-factory标签下
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>是数据源的配置分别为驱动、url、用户名、密码                   <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>是配置数据库方言即对哪种数据库操作
        <property name="hibernate.show_sql">true</property>是配置是否打印数据库操作语句
        <property name="hibernate.hbm2ddl.auto">update</property>指定对数据库的默认操作
        <mapping resource="com/zhouxiang/model/User.hbm.xml"/>指定要加载的表与实体间映射关系文件

 3.添加表与实体间映射关系文件 xxx.hbm.xml

01.<?xml version="1.0"?>  02.<!DOCTYPE hibernate-mapping PUBLIC  03.        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  04.        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  05.<hibernate-mapping package="com.zhouxiang.model">  06.    <class name="User" polymorphism="explicit">  07.        <id name="id">  08.            <generator class="uuid" ></generator>  09.        </id>  10.        <property name="name" column="username"></property>  11.        <property name="password" column="password"></property>  12.    </class>    13.</hibernate-mapping> 

三.使用hibernate的7个步骤

   1.第一步:

          创建Configuration读取配置信息
          Configuration cfg = new Configuration().configure();

2.第二步:

          创建sessionFactory
         SessionFactory factory= cfg.buildSessionFactory();

3.第三步:打开session

        Session session=factory.openSession();

4.第四步:开启事务Transaction

        session.getTransaction().begin();

5.第五步:进行持久化操作,即增删查改等操作

         User user=new User();
         user.setName("aaa");
         user.setPassword("123456");
         session.save(user);

6.第六步:提交事务

        session.getTransaction().commit();

 7.关闭资源,也就是关闭session

        session.close();

四.实例代码

hibernate.cfg.xml
01.<!DOCTYPE hibernate-configuration PUBLIC  02.    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  03.    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  04.  05.<hibernate-configuration>  06.    <session-factory >  07.        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  08.        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>  09.        <property name="hibernate.connection.username">root</property>  10.        <property name="hibernate.connection.password">123456</property>  11.        <property name="hibernate.show_sql">true</property>  12.        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  13.        <property name="hibernate.hbm2ddl.auto">update</property>  14.        <mapping resource="com/zhouxiang/model/User.hbm.xml"/>  15.    </session-factory>  16.</hibernate-configuration> 

User.hbm.xml

01.<?xml version="1.0"?>  02.<!DOCTYPE hibernate-mapping PUBLIC  03.        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  04.        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  05.<hibernate-mapping package="com.zhouxiang.model">  06.    <class name="User" polymorphism="explicit">  07.        <id name="id">  08.            <generator class="uuid" ></generator>  09.        </id>  10.        <property name="name" column="username"></property>  11.        <property name="password" column="password"></property>  12.    </class>     13.</hibernate-mapping>  
User类

01./** 02. *  03. */  04.package com.zhouxiang.model;  05.  06./**  07. * @ClassName: User  08. * @Description: TODO 09. * @author zx 10. * @date 2014年5月15日 上午10:40:43  11. *   12. */  13.public class User {  14.    private String id;  15.    private String name;  16.    private String password;  17.    public User()  18.    {}  19.    public String getId() {  20.        return id;  21.    }  22.    public void setId(String id) {  23.        this.id = id;  24.    }  25.    public String getName() {  26.        return name;  27.    }  28.    public void setName(String name) {  29.        this.name = name;  30.    }  31.    public String getPassword() {  32.        return password;  33.    }  34.    public void setPassword(String password) {  35.        this.password = password;  36.    }  37.      38.      39.}  

测试类
01./** 02. *  03. */  04.package com.zhouxiang.test;  05.  06.import org.hibernate.HibernateException;  07.import org.hibernate.Session;  08.import org.hibernate.SessionFactory;  09.import org.hibernate.cfg.Configuration;  10.  11.import com.zhouxiang.model.User;  12.  13./**  14. * @ClassName: Test1  15. * @Description: TODO 16. * @author zx 17. * @date 2014年5月16日 上午10:09:55  18. *   19. */  20.public class Test1 {  21.    public static void main(String args[])  22.    {  23.        SessionFactory factory=null;  24.        Session session=null;  25.        try {  26.            Configuration cfg = new Configuration().configure();  27.                        factory=cfg.buildSessionFactory();  28.                        session=factory.openSession();  29.                        session.getTransaction().begin();  30.                        User user=new User();  31.                        user.setName("bbb");  32.                        user.setPassword("123456");  33.                        session.save(user);  34.                        session.getTransaction().commit();  35.         } catch (HibernateException e) {  36.            // TODO Auto-generated catch block  37.            e.printStackTrace();  38.            session.getTransaction().rollback();  39.        }  40.        finally  41.        {  42.            if(session!=null)  43.            {  44.             if(session.isOpen())  45.             {  46.                 session.close();   47.             }  48.            }  49.        }  50.    }  51.}