我的第一个用Hibernate框架写的小例子

                                    Hibernate框架

  今天我来主要讲一下Hibernate框架的配置步骤,在讲之前,我们先了解一下使用框架的优势

1.不再考虑公共问题,框架已经帮我们做好了

2.可以专心于业务逻辑,保证核心业务逻辑的开发质量

3.结构统一,便于学习和维护

4.框架中集成了前人的经验,可以帮助新手写出稳定、性能优良而且结构优美的高质量程序。

  我们都知道SSH集成框架指的是基于Struts或Struts2+Spring+Hibernate的技术框架,也就是我们常说的三大框架,使用这个集成框架将使我们应用程序更加健壮、稳固、轻巧和优雅。

  Hibernate是数据持久化(数据持久化就是将内存中的数据模型转换为存储模型,以及将存储模型转换为内存... 然后根据框架的配置文件,自动或手动决定什么时候把这种保存提交到数据库)工具,是一个开放源代码的对象关系映射框架。

优点:

1.Hibernate功能强大,较之JDBC方式操作数据库,代码量大大减少,提高持久化代码的开发速度,降低维护成本。

2.Hibernate支持许多面向对象的特性

3.可移植性好

4.Hibernate框架开源免费

缺点:

1.不适合以数据为中心的大量使用存储过程的应用。

2.大规模的批量插入、修改和删除不适合用HibernateHibernate环境搭建

1.下载和部署jar包

2.创建配置文件hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings 数据库连接配置-->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">wth</property>
        <property name="connection.password">1509184562</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect 方言-->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>


        <!-- Echo all executed SQL to stdout 在控制台打印后台sql语句-->
        <property name="show_sql">true</property>
        <!-- 格式化语句 -->
       <property name="format_sql">true</property>
        <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
          <!-- 关联小配置 -->
        <mapping resource="cn/entity/student.hbm.xml" />

    </session-factory>

</hibernate-configuration>

3.创建持久化类和映射文件

小配置:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.entity">

        <class name="Student" table="STUDENT">
                <id name="sId" column="SID">
                        <generator class="native"/>
                </id>
                <!-- assigned 程序员赋值  native后台DB赋值-->
                <property name="sName" type="string" column="SNAME"/>
                <property name="sAge"/>             
                       
        </class>
</hibernate-mapping>

持久化类

package cn.test;

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

import cn.entity.Student;

public class Test {
  public static void main(String[] args) {
    Student stu=new Student();

    stu.setsName("呵");
    stu.setsAge(12);
    Configuration cfg=new Configuration().configure();
    SessionFactory sc = cfg.buildSessionFactory();
    
    Session se = sc.openSession();
    Transaction tx=se.beginTransaction();
    
    se.save(stu);
    tx.commit();
    System.out.println("success!!!");
  }
}

 

posted @ 2016-08-02 18:30  柒陌枫  阅读(6831)  评论(0编辑  收藏  举报