hibernate入门知识01

1.Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装

2.是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端程序使用,也可以在Servlet/JSP的Web应用中使用,最具革命意义的是,Hibernate可以在应用EJB的J2EE架构中取代CMP,完成数据持久化的重任。

3.起源:2001年,澳大利亚墨尔本一位名为Gavin King的27岁的程序员,上街买了一本SQL编程的书,他厌倦了实体bean,认为自己可以开发出一个符合对象关系映射理论,并且真正好用的Java持久化层框架,因此他需要先学习一下SQL。这一年的11月,Hibernate的第一个版本发布了。

3.hibernate是完全的orm框架的一种(使用hql)...

4.创建maven工程,添加需要的依赖,在maven工程的pom.xml中添加两个依赖

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.3.11.Final</version>
</dependency>

5.在src/main/java下创建:hibernate.cfg.xml主配置文件配置如下

  

<session-factory>

  <!--配置数据库相关参数-->
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql:///hibernate?characterEncoding=UTF-8</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">ou134568</property>

  <!-- 方言选择,根据不同的数据库 -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="show_sql">true</property><!-- 打印sql语句 -->

  <!-- 加入映射文件,javabean的映射配置文件 -->
  <!-- <mapping resource="po/User.hbm.xml"></mapping> -->
  <mapping resource="onetomany/Clazz.hbm.xml"></mapping>
  <mapping resource="onetomany/Students.hbm.xml"></mapping>
</session-factory>

6.在测试类中,首先读取主配置文件/生成服务注册器/生成sessionfactory

  我们可以写成一个工具类,这样我们只要调用openSession()就可以了...

  org.hibernate.Session本质上是对java.sql.Connection的封装

  

  public class DBUtils {
     private static Configuration CGF;
     private static SessionFactory FACTORY;
      static{
      //读取主配置hibernate.cfg.xml文件
      CGF = new Configuration().configure();
      //生成服务注册器
      ServiceRegistry sr = new StandardServiceRegistryBuilder()
      .applySettings(CGF.getProperties()).build();
      //由服务注册器来生成sessionfactory
      FACTORY = CGF.buildSessionFactory(sr);
      }


      public static Session openSession(){
      return FACTORY.openSession();
      }
      }

 7.测试方法--根据主配置文件和映射文件生成对应的表,把所有的有映射配置文件的实体类生成对应的表,如果已经存在则不生成

    public void test01(){

    //读取hibernate.cfg.xml配置文件
    Configuration cfg = new Configuration().configure();
    //根据主配置文件--映射文件生成对应的表
    SchemaExport export = new SchemaExport(cfg);
    //生成表
    export.create(true,true);

     }

8.测试方法,将一个对象持久化(存入数据库)

    public void test02(){

    Session session = DBUtils.openSession();

    session.beginTransaction();
    Clazz clazz = new Clazz();
    clazz.setClazzName("一年级");

    Students student01=new Students();
    student01.setStudentName("王五");
    student01.setClazz(clazz);

    Students student02=new Students();
    student01.setStudentName("张三");
    student01.setClazz(clazz);
    

    //保存,要先保存clazz.否则会异常

    session.save(clazz);
    session.save(student01);
    session.save(student02);
    

    //提交事务并关闭session
    session.getTransaction().commit();
    session.close();

    }

原文地址:https://www.cnblogs.com/ou134568/p/6859673.html