1、建表建序列(所用数据库为Oracle,数据库名为XE,用户名为system,密码为manager)
create table employee
(
id number primary key,
name varchar2(64) not null,
email varchar2(64) not null,
hiredate date not null
);
create sequence emp_seq
start with 1
increment by 1
minvalue 1
nomaxvalue
nocycle
nocache
/
2、程序结构
3、各个文件
Employee.java
package com.tfj.domain;
import java.io.Serializable;
//这是一个domain对象(实际上就是javabean/有些人pojo)
//他和Employee对应
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String email;
private java.util.Date hiredate;
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public java.util.Date getHiredate() {
return hiredate;
}
public void setHiredate(java.util.Date hiredate) {
this.hiredate = hiredate;
}
}
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">
<!-- package : 表示该类在哪个包下 -->
<hibernate-mapping package="com.tfj.domain">
<!-- name : 表示类名 table 表示 该类和哪个表映射 -->
<class name="Employee" table="employee">
<!-- id元素专门用于指定主键是如何生成,hibernate设计者认为,我们每一个表都应该有一个主键 -->
<!-- name:表示类的哪个属性是主键 -->
<id name="id" type="java.lang.Integer">
<!-- 指定主键生成策略 -->
<generator class="sequence">
<param name="sequence">emp_seq</param>
</generator>
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true"/>
</property>
<property name="email" type="java.lang.String">
<column name="email" not-null="true"/>
</property>
<property name="hiredate" type="java.util.Date">
<column name="hiredate" not-null="true"/>
</property>
</class>
</hibernate-mapping>
TestMain.java
package com.tfj.view; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.tfj.domain.Employee; public class TestMain { public static void main(String[] args) { // 添加一个雇员 // 1.得到Configuration Configuration configuration = new Configuration().configure(); // 2.得到SessionFactory(会话工厂,这是一个重量级的类,因此要保证在一个应用程序中只能有一个) SessionFactory sessionFactory = configuration.buildSessionFactory(); // 3. 从SessionFactory中取出一个Session对象(它表示 和数据库的出一次会话) Session session = sessionFactory.openSession(); // 4. 开始一个事务 Transaction transaction = session.beginTransaction(); // 保存一个对象到数据库(持久化一个对象) Employee emp = new Employee(); emp.setEmail("tufujiepuyang@foxmail.com"); emp.setName("tufujie"); emp.setHiredate(new java.util.Date()); // 不要给id,因为它是自增的 session.save(emp);// insert into employee (name,id,...) value(?,?,?) transaction.commit(); } }
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">
<!-- 该文件用于配置连接数据的种类,用户名,密码,ul ,驱动.. 连接池,二级缓存.. 有点类似strus struts-config.xml -->
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">manager</property>
<!-- 配置显示hibernate生成的 sql ,特别说明,在开发阶段设为true利于调试,在使用项目则设为false-->
<property name="show_sql">true</property>
<!-- 配置数据库的方言/ -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- 配置管理的对象映射文件 -->
<mapping resource="com/tfj/domain/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
原文:http://www.cnblogs.com/tufujie/p/4915927.html