zoukankan      html  css  js  c++  java
  • 关于Hibemate

    1.Hibernate定位

     HIbernate是一款实现了ORM思想的框架

     JDO

     TOpLink

    2.HIbernate初次解释

    Hibernate:冬眠,蛰伏 和持久化有关系

    将内存中data持久化到咱们的硬盘的db中。

                              HIbernate的作为:

     3.一个简单案例:

    1.构建了一个Student实体类

    public class Student {

        private Integer id;

        //name

        private String name;

        //age

        private Integer age;

    }

    2.构建一个大配置

     在src根目录下书写

    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>

            <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>

            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>

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

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

            <!-- 输出所有 SQL 语句到控制台。 -->

            <property name="hibernate.show_sql">true</property>      

            <!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->

            <property name="hibernate.format_sql">true</property>

            <!-- 方言 -->

            <property name="hibernate.dialect">    org.hibernate.dialect.Oracle10gDialect</property>

           <!-- 关联小配置 -->      

       </session-factory>

    </hibernate-configuration> 

    3.构建小配置,和实体类对应的

      Student.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="cn.happy.entity">

         <class name="Student" table="Student">

             <id name="id" type="int" column="id">

             </id>

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

             <property name="age" type="int" column="age"/>

         </class>   

     </hibernate-mapping>

    4.测试代码

      对session进行探究。

      Session.save(stu);

    package cn.happy.test;

    import org.hibernate.SessionFactory;

    import org.hibernate.Transaction;

    import org.hibernate.cfg.Configuration;

    import org.hibernate.classic.Session;

    import org.junit.Test; 

    import cn.happy.entity.Student;

    public class H_01Test {

      @Test

      public void testAdd(){

          //1.1构建一个学生对象

          Student stu=new Student();

          stu.setAge(18);

          stu.setName("2016年8月28日09:21:09训练营");

          stu.setId(3);

          //1.2 找到和数据库的接口      xxx========session--->sessionFactory--->configure.buildSessionFactory()

          //咱们要想打通和db通道

          Configuration cf=new Configuration().configure("hibernate.cfg.xml");

          SessionFactory factory = cf.buildSessionFactory();

          Session session = factory.openSession();

          Transaction tx = session.beginTransaction();       

          //1.3保存  

          session.save(stu);

          tx.commit();

          session.close();

      } 

    }

    -------------------------------------------------------------*************************************************

    4.关于ORM

    Object Relational Mapping 对象/关系 映射。

    ------------------------------------------------*********************************------------------------------------------------------------************************

    5.用HIbernate实现删除,修改和查询

    /**

        * 1.1 删除学生

        */

       @Test

       public void delTest(){

           Session session = HibernateUtil.getSession();

           Student stu=new Student();

           stu.setId(2);

           Transaction tx = session.beginTransaction();

           session.delete(stu);

           tx.commit();

           HibernateUtil.closeSession();

           System.out.println("del ok!");

       }

       /**

        * 1.2 修改学生

        */

       @Test

       public void updateTest(){

           Session session = HibernateUtil.getSession();   

           //不被上下文跟踪对象

           /*Student stu=new Student();

           stu.setId(3);

           stu.setName("美男子");*/ 

           //方式二:如何用呗上下文跟踪的方式

           //检索出一条记录,一个实体对象

           Student stu= (Student)session.load(Student.class,3);

           stu.setName("美女");

           Transaction tx = session.beginTransaction();

           session.update(stu);

           tx.commit();

           HibernateUtil.closeSession();

           System.out.println("update ok!");

       }

    6.反射

    Class 一个类型的类型

    Class clazz= Student.class;

    拿到的是学生类的字节码

  • 相关阅读:
    JAVA环境搭建之MyEclipse10+jdk1.8+tomcat8环境搭建详解
    web.xml 配置 contextConfigLocation
    详解JDBC与Hibernate区别
    copy running-config startup-config 与 copy startup-config running-config
    CSS实现垂直居中的常用方法
    dskms改为ckplayer播放器
    apache2 不支持php文件 解决办法
    Linux下修改Mysql密码的三种方式
    Debian/Ubuntu下安装Apache的Mod_Rewrite模块的步骤
    DIV+ul+LI实现表格效果以及div带滑动条
  • 原文地址:https://www.cnblogs.com/ainiaiwo/p/5815046.html
Copyright © 2011-2022 走看看