【Hibernate】Hibernate系列(二)

    xiaoxiao2025-08-26  1

    Hibernate环境搭建

    我这次创建的是一个叫做student的表,里面存有id、name和age字段,我将搭建一个hibernate环境,利用框架的核心API对student进行数据插入。数据库用的是Mysql。

    1.首先在工程中添加hibernate的jar包:

    将解压路径中hibernate3.jar和lib路径下的required、jpa子目录下所有JAR包添加到应用的类加载路径中。

    然后,我们就可以把建好的库引入到项目中了,具体操作为:右键项目名称-->Properties-->Java Build Path,如图:

    核心文件配置

            到现在,已经把建好的库引入到项目中了,之后我们要做的就是,把Hibernate的核心配置文件(hibernate.cfg.xml)引入到项目中, 该核心配置文件会给Hibernate提供数据库的映射,使得Hibernate与数据库进行连接。如图所示:

            之后,我们需要对Hibernate核心文件做一下配置,加入MySQL的JDBC驱动,配置数据库连接。代码如下:

    [html] view plain copy <span style="font-family:Microsoft YaHei;font-size:18px;"><!DOCTYPE hibernate-configuration PUBLIC      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  <hibernate-configuration>      <session-factory>          <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>          <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property>          <property name="hibernate.connection.username">root</property>          <property name="hibernate.connection.password">123</property>          <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>          <property name="hibernate.show_sql">true</property>        </session-factory>  </hibernate-configuration></span>  hibernate的帮助文档在

    hibernate-distribution-3.6.0.Final\documentation\manual\zh-CN\html_single下的index.html

    2..编写实体类的JavaBean(cn.edu.hpu.model包下)

    [java] view plain copy package cn.edu.zjl.model;      public class Student {      private int id;      private String name;      private int age;      public int getId() {          return id;      }      public void setId(int id) {          this.id = id;      }      public String getName() {          return name;      }      public void setName(String name) {          this.name = name;      }      public int getAge() {          return age;      }      public void setAge(int age) {          this.age = age;      }              }  

    3.配置实体类的hbm映射文件:

    Student.hbm.xml:

    [html] view plain copy <?xml version="1.0"?>  <!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.edu.hpu.model">      <class name="Student" table="stu">          <!-- 主键 这里的主键名字刚好也是'id'-->          <id name="id" column="id"></id>          <property name="name"></property>          <property name="age"></property>      </class>  </hibernate-mapping>  

    4.在src目录下hibernate的配置配置文件,即数据库连接池hibernate.cfg.xml:

    [html] view plain copy <?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.gjt.mm.mysql.Driver</property>          <property name="connection.url">jdbc:mysql://localhost:3306/students</property>          <property name="connection.username">root</property>          <property name="connection.password">1234</property>              <!-- JDBC connection pool (use the built-in) JDBC连接池-->          <!-- <property name="connection.pool_size">1</property> -->              <!-- SQL dialect SQL方言-->          <property name="dialect">org.hibernate.dialect.MySQLDialect</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 是否打印sql语句-->          <property name="show_sql">true</property>              <!-- Drop and re-create the database schema on startup 是否自动生成建表语句语句-->          <property name="hbm2ddl.auto">update</property>              <mapping resource="cn/edu/hpu/model/Student.hbm.xml"/>          </session-factory>      </hibernate-configuration>  

    5.编写测试类:

    [java] view plain copy package cn.edu.zjl.test;      import org.hibernate.Session;  import org.hibernate.SessionFactory;  import org.hibernate.cfg.Configuration;      import cn.edu.hpu.model.Student;      public class StudentTest {      public static void main(String[] args) {          Student s=new Student();          s.setId(1);          s.setName("s1");          s.setAge(1);                    //Configuration是用来读配置文件的          Configuration cfg=new Configuration();          //指定要读哪个文件,不写参数默认读hibernate配置          //文件下的 <mapping resource=.....          SessionFactory sf=cfg.configure().buildSessionFactory();          //buildSessionFactory()可以理解为能得到conn的类                    //暂时把Session理解成数据库的Connection          Session session=sf.openSession();//得到session          session.beginTransaction();          session.save(s);          session.getTransaction().commit();          session.close();          sf.close();                }  }  

    查看MySql数据库,发现已经存在一张新的表stu,并且里面拥有一个id为1,name为s1,age为1的记录。

    这个小例子向大家叙述了如何搭建一个hibernate的环境,以及利用session来对实体数据进行简单的操作。

    最新回复(0)