Hibernate 专题
专题目录
您的位置:java > Hibernate 专题 > Hibernate入门程序
Hibernate入门程序
作者:--    发布时间:2019-11-20

在第一个hibernate入门示例程序中,我们将使用eclipse ide创建一个简单的hibernate应用程序示例。要在eclipse ide中创建第一个hibernate应用程序,需要遵循以下步骤:

重要提示:
按照惯例,易百教程的每个入门教程程序都会详细介绍对应环境的安装,配置和运行步骤,以帮助读者/学习者快速入门。往后的系列教程中没有特别说明,都会以入门教程程序配置为参考。所以,在您阅读每一篇文章遇到配置或运行环境不清楚,请参考入门教程程序配置。

  1. 创建java项目
  2. 为hibernate添加jar文件
  3. 创建持久类
  4. 创建持久类的映射文件
  5. 创建配置文件
  6. 创建检索或存储持久对象的类
  7. 运行应用程序

前提工作:

  1. 安装好 mysql 并启动。参考: http://www.h3.com/mysql/
  2. eclipse neon.2 release (4.6.2)。 参考:http://www.h3.com/eclipse/
  3. 下载 hibernate 相关类库(jar文件)
  4. 创建一个数据库表:tb_employee

1. 创建java项目

打开eclipse,通过 file -> new -> project -> java project 创建java项目。 现在指定项目名称: first-hibernate, 然后 next-> 完成。

2. 为hibernate添加jar文件

添加jar文件右键单击您的项目(first-hibernate) -> build path -> 添加外部存档。

现在选择所有的jar文件,如下图所示 -

创建一个用户自定义库,为这个自定义库起个名字:hibernate-jars,并选择 hibernate 相关jar包。

完成后加入效果如下所示 -

下载所需的 hibernate-jars文件:

在这个例子中,我们将应用程序与mysql数据库连接。 所以你必须添加mysql-connect.jar文件。

3. 创建持久化类

在这里,要创建持久化类,右键单击src -> new -> class - 使用包名指定类(例如:com.h3.mypackage) -> finish

employee.java

package com.h3.mypackage;  

public class employee {  
    private int id;  
    private string firstname,lastname;  

    public int getid() {  
        return id;  
    }  
    public void setid(int id) {  
        this.id = id;  
    }  
    public string getfirstname() {  
        return firstname;  
    }  
    public void setfirstname(string firstname) {  
        this.firstname = firstname;  
    }  
    public string getlastname() {  
        return lastname;  
    }  
    public void setlastname(string lastname) {  
        this.lastname = lastname;  
    }  

}

4. 创建持久化类的映射文件

在这里,我们正在创建与上一主题中创建的相同的映射文件。 要创建映射文件,右键单击src -> new -> file -> 指定文件名(例如employee.hbm.xml) , 它必须在包外部。

employee.hbm.xml

<?xml version='1.0' encoding='utf-8'?>  
<!doctype hibernate-mapping public  
 "-//hibernate/hibernate mapping dtd 3.0//en"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="com.h3.mypackage.employee" table="emp1000">  
    <id name="id">  
     <generator class="assigned"></generator>  
    </id>  

    <property name="firstname"></property>  
    <property name="lastname"></property>  

  </class>  

 </hibernate-mapping>

5. 创建配置文件

配置文件包含数据库的所有信息,如:connection_urldriver_classusernamepassword等。hbm2ddl.auto属性用于自动在数据库中创建表。 我们将在下一个主题中深入学习dialect类。 要创建配置文件,请右键单击src -> new -> file。 现在指定配置文件名,例如: hibernate.cfg.xml

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">

<!-- generated by myeclipse hibernate tools. -->
<hibernate-configuration>

    <session-factory>

        <property name="connection.driver_class">com.mysql.jdbc.driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="dialect">org.hibernate.dialect.mysql5innodbdialect</property>
        <property name="show_sql">true</property>
        <mapping resource="employee.hbm.xml"/>  
    </session-factory>

</hibernate-configuration>

6. 创建检索或存储持久对象的类

在这个类中,我们只是将employee对象存储到数据库中。

package com.h3.mypackage;  

import org.hibernate.session;  
import org.hibernate.sessionfactory;  
import org.hibernate.transaction;  
import org.hibernate.cfg.configuration;  

public class storedata {  
    public static void main(string[] args) {  

        //creating configuration object  
        configuration cfg=new configuration();  
        cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  

        //creating seession factory object  
        sessionfactory factory=cfg.buildsessionfactory();  

        //creating session object  
        session session=factory.opensession();  

        //creating transaction object  
        transaction t=session.begintransaction();  

        employee e1=new employee();
        e1.setid(100);
        e1.setfirstname("max");
        e1.setlastname("su");

        session.persist(e1);//persisting the object  

        t.commit();//transaction is committed  
        session.close();  

        system.out.println("successfully saved");  

    }  
}

7. 运行应用程序

在运行应用程序之前,确定目录结构如下图所示 -

在运行这个示例之前,还差关键的一个步骤,那就是创建对象对应的表:tb_employee,其结构及创建语句语句如下-

create table `tb_employee` (
  `id` int(10) unsigned not null auto_increment,
  `firstname` varchar(32) not null default '',
  `lastname` varchar(32) not null default '',
  primary key (`id`)
) engine=innodb auto_increment=101 default charset=utf8;

接下来,就是运行hibernate应用程序,请右键单击storedata类 -run as - java应用程序。

输入结果如下 -

info: hhh000400: using dialect: org.hibernate.dialect.mysqldialect
hibernate: insert into tb_employee (firstname, lastname, id) values (?, ?, ?)
successfully saved

查看 tb_employee 表中的数据,如下 -


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册