Hibernate基础系列之 :1初试Hibernate

一、简介

        笔者写该blog遵循的规则是,At First , 先抛出一个Demo; And then, 根据该Demo做一些知识点的分析,大多是一些笔者认为的细节和要点。如果路过的大神发现笔者blog中的内容有误,请不吝赐教,笔者将十分感激。

二、实例展示

  1.  环境准备

  • 下载Hibernate3.3.2
    • 下载Hibernate3.4.0
    •  注意阅读Hibernate Compatibility matrix 
    • 下载 hibernate-annotations-3.4.0.GA 的包 
    • 下载slf4j 1.5.8   hamcrest   apache-log4j-2.2-bin 
    • 下载安装Mysql 
    • 下载 junit4.10   
    • IDE 笔者用的是Netbeants8.0.1 (酷爱这个IDE,偏向开源)
    • 所用版本老旧,但学东东呢,不怕东西老旧,因为已经有很多人给你sweep way 很多坑了。我们主要任务是通过学习这个框架来学习它的一种编程思维(思想这词感觉有点泛滥,太重了微笑),它的核心开发接口;of course,当然还有锻炼查看doc 能力。笔者会给出案例的配置和相关工具和环境,但不会描述详细的操作,笔者只会关注于在学习Hibernate过程中要注意的一些细节和常见应用。

    2、入门要先敲门,Hello Hibetnate程序

    编写一个实体类

    package com.ricezhang.hibernate.model;
    
    public  class Student
    {
         private int id;
         private String name;
         private int age;
    
           //省略get set method 
    
    }

    配置Hibernate的配置文件

    Hibernate 的配置文件请看Hibernate的doc,Hibernate的doc做的非常棒,只要一点基本的知识,你只有看doc 也基本能掌握doc 就在Hibetnate的下载文件中。用一个框架你一般都是先在doc中查找资料,当查无可查时,可以google OR Baidu ,在没办法时可以尝试与业内同行进行交流。
    当然,如果你是用Netbeans的话,他有Hibernate的配置文件,你可以选中你要用的数据库,Netbeans 会帮助你配好的。
    <?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">org.hibernate.dialect.MySQLDialect</property>
            <property name="connection.url">com.mysql.jdbc.Driver</property>
            <property name="connection.username">sa</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.HSQLDialect</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">update</property>
    
            <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>
    
        </session-factory>
    
    </hibernate-configuration>

    XML配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <!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.bjsxt.hibernate.Student" table="student">
            <id name="id" />
            <property name="name" />
            <property name="age" />
        </class>
    </hibernate-mapping>
    

    Test
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.bjsxt.hibernate;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    /**
     *
     * @author Ricezhang <ricezhang@outlook.com>
     */
    public class StudentTest {
    
        public static void main(String[] args) {
            Student s = new Student();
            s.setId(1);
            s.setName("s1");
            s.setAge(11);
    
            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();
        }
    
    }
    
      
    经本人测试:
    BetBeans8.0.1上的 自建的xml文件虽然与在Netbeans 的 Hibernate 自带的文件图标不一样,但同样是可以运行的。

    三、知识分析

         
                                                                                                                   Hibernate基本流程图



          如果要使用Hibetnate 必须掌握上述的流程图,你在编写Hibernate使就会得心应手,当然,这只是一个入门的程序,下面我们会通过这个入门的程序进行分析,以及了解tHibernate的各种的基本配置和包的属性功能。
    线程数:
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    方言:
            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    session上下文的范围:
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>

    Hibernate的缓存:
            <!-- 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>
            <property name="format_sql">true</property>
    Hibenate自动建表:
            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>
    详细Doc 有解析:以doc为准。


    四、知识分享

     
     
    • 官网Doc 
    • 各大论坛中share 的Hibernate的视频教程

    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值