版权声明:本文为博主原创文章,非商用转载请注明出处: https://blog.csdn.net/qq_17058993/article/details/84068930
参考连接:https://www.yiibai.com/hibernate/first-hibernate-application.html
一、创建Java项目
打开Eclipse,通过 File -> New -> project -> java project 创建java项目。 现在指定项目名称: first-hibernate
, 然后 next-> 完成。
二、下载Hibernate的jar包并导入
1、下载地址:
http://hibernate.org/orm/releases/
2、下载完成以后找到jar,如下:
3、build path
进行相应的编辑修改,找到下载的jar。
导入工程中即可。
三、创建持久化类
右键单击src
-> New
-> Class
- 使用包名指定类(例如:com.yiibai.mypackage
) -> finish
Employee.java
package com.yiibai.mypackage;
/**
* 持久化类
*
* @author Dell
*
*/
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;
}
}
四、创建持久化类的映射文件
在这里,我们正在创建与上一主题中创建的相同的映射文件。 要创建映射文件,右键单击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.yiibai.mypackage.Employee" table="EMPLOYEE">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="firstName"></property>
<property name="lastName"></property>
</class>
</hibernate-mapping>
其中 table 为表名,必须和数据库中的表对应起来。
五、创建配置文件
1、右键单击src
-> new
-> file
。 现在指定配置文件名,例如: hibernate.cfg.xml
2、配置文件包括数据库的连接信息,驱动包
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">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="connection.username">petition</property>
<property name="connection.password">petition</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这里我用的是Oracle,方言对应起来。
六、创建检索或存储持久对象的类
在这个类中,我们只是将employee
对象存储到数据库中。
package com.yiibai.mypackage;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
/**
* 创建检索或存储持久对象的类
* @author Dell
*
*/
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(12);
e1.setFirstName("Max");
e1.setLastName("Su");
session.persist(e1); //persisting the object
t.commit(); //transaction is committed
session.close();
System.out.println("successfully saved");
}
}
七、创建Oracle数据表
字段名必须大写,否则报 列名无效
八、运行测试
1、目录结构
2、console
3、Oracle