1_hibernate-HelloWorld

来源:互联网 发布:淘宝上开代购店 编辑:程序博客网 时间:2024/05/17 20:22
准备工作:建立java工程,建立libraries,添加需要的jar包,引入数据库连接包

1、建立实体类
2、建立数据库
3、建立hibernate配置文件:hibernate.cfg.xml
4、建立实体类的映射文件:Student.hbm.xml ,实现实体类与数据库中数据的映射关系
5、在hibernate配置文件中,加入实体类与数据库映射文件的地址
6、拿到Configuration,通过它的configure()方法来解析hibernate映射文件(若不为confugure()方法指定hibernate的路径,则默认指向src根目录下的hibernate的映射文件)
7、得到SessionFactory,用来产生session
8、将session存入事务中
9、通过save()方法,将实体类放入session中
10、执行事务
11、关闭session,关闭SessionFactory

                    实现后如下图:
                         




1、建立实体类
package com.bjsxt.hibernate.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;
     }

}

2、建立数据库

1、create database hibernate;
2、use hibernate;
3、create table Student {id int primary key,name varchar(20),age int}


3、建立hibernate配置文件:hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>

        <!-- JDBC connection pool (use the built-in)  数据库连接池-->
        <!--<property name="connection.pool_size">1</property>  -->

        <!-- SQL dialect 方言:hibernate具体生成的哪个数据库的语句-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Enable Hibernate's automatic session context management 3.2以后新加的功能 -->
        <!--<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 hibernate要不要自动生成建表语句 -->
        <!--<property name="hbm2ddl.auto">update</property>-->

        <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

4、建立实体类的映射文件:Student.hbm.xml,实现实体类与数据库中数据的映射关系


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.bjsxt.hibernate.model">
    <class name="Student" ><!--表的映射       实体类名和表明一样,则不用写表明;若不一样,则增加属性table = "tableName" -->
          <id name="id" ></id><!-- 主键字段的映射    若表中对应的字段名与配置文件中的相同,则不用写数据库中的字段名;若不相同,则加属性 column="字段名"  -->
        <property name="name" /><!-- 非主键的普通字段映射 -->
        <property name="age" /><!-- 非主键的普通字段映射 -->
    </class>
</hibernate-mapping>

5、在hibernate配置文件中,加入实体类与数据库映射文件的地址

<mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>

6、在测试类中拿到Configuration,通过它的configure()方法来解析hibernate映射文件(若不为confugure()方法指定hibernate的路径,则默认指向src根目录下的hibernate的映射文件);
得到SessionFactory,用来产生session;
将session存入事务中;
通过save()方法,将实体类放入session中;
执行事务;
关闭session,关闭SessionFactory

public class StudentTest {
     public static void main(String[] args) {
          Student s = new Student();
          s.setId(6);
          s.setName("s5");
          s.setAge(6);

          Configuration cf =new Configuration();
          SessionFactory sf = cf.configure().buildSessionFactory();
          Session session = sf.openSession();
          session.beginTransaction();
          session.save(s);
          session.getTransaction().commit();
          session.close();
          sf.close();

     }
}


原创粉丝点击