Hibernate框架简单应用步骤

时间:2022-05-31
本文章向大家介绍Hibernate框架简单应用步骤,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

所属分类:Hibrenate

Hibernate框架应用步骤


引入hibernate所需jar包


1.创建实体类,(最好实现序列化接口)

``` public class Student{ private Integer id; private String name; private int age; private double score; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age, double score) { super(); this.name = name; this.age = age; this.score = score; } public Integer getId() { return id; } public void setId(Integer 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; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", score=" + score + "]"; }

} ```


2.实体类到数据库的映射关系

``` <?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> </hibernate-mapping>

```

3.测试hibernate配置

``` public class MyTest { @Test public void testSave() { // 1.加载主配置文件 Configuration configure = new Configuration().configure(); // 2.创建Session工厂 SessionFactory sessionFactory = configure.buildSessionFactory(); // 3.获取Session Session session = sessionFactory.getCurrentSession(); try { // 4.开启事务 session.beginTransaction(); // session.getTransaction().begin();

        // 5.操作
        session.save(new Student("张三", 23, 93.5));
        // 6.事务提交
        session.getTransaction().commit();
    } catch (Exception e) {
        e.printStackTrace();
        // 7.事务回滚
        session.getTransaction().rollback();
    }

}

}

```

4.在src包下创建hibernate.cfg.xml文件,是hibernate的配置文件

``` <?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>com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/zfw?useUnicode=true&&characterEncoding=utf-8 root

    <!-- 方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- c3p0数据源 -->
    <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
    <!-- 当前Session上下文 -->
    <property name="hibernate.current_session_context_class">thread</property>

    <!-- 自动建表 -->
    <property name="hibernate.hbm2ddl.auto">update</property>

    <!-- 显示sql语句 -->
    <property name="hibernate.show_sql">true</property>

    <!-- 格式化sql语句 -->
    <property name="hibernate.format_sql">true</property>


    <!-- 注册映射文件,此配置要加在最后,因为上面执行完后,才映射到数据库中 -->
    <mapping resource="com/zfw/beans/Student.hbm.xml" />
</session-factory>

</hibernate-configuration>

```