Hibernate学习总结之基础篇

hibernate 是对 jdbc 进行轻量级封装的   orm 框架,充当项目的持久层 .

hiberante 可以用在 j2se 项目,也可以用在 j2ee (web 项目中 )

struts 是 web 框架 , 所以用在 web 项目


创建 employe 表 .

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


domain对象:

package com.sina.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.hbml.xml 配置文件 :

<!DOCTYPE hibernate-mapping PUBLIC

       "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.sina.domain">

       <class name="Employee" table="employee">

       <!-- id 元素用于指定主键属性 -->

       <id name="id" column="id" type="java.lang.Integer">

       <!-- 该元素用于指定主键值生成策略 hilo native increment sequence uuid -->

       <generator class="sequence">

      <param name="sequence">emp_seq</param>

       </generator>

       </id>

       <!-- 对其它属性还有配置 -->

       <property name="name" type="java.lang.String">

       <column name="name" not-null="false"  />

       </property>

       <property name="email" type="java.lang.String" >

       <column name="email" not-null="false"/>

       </property>

       <property name="hiredate" type="java.util.Date">

       <column name="hiredate" not-null="false" />

       </property>

       </class>      

</hibernate-mapping>


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>

       <!-- hibernate 设计者,给我们提供了一写常用的配置 -->

       <!-- 配置使用的 driver -->

       <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

       <property name="connection.username">scott</property>

       <property name="connection.password">tiger</property>

       <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:sina</property>

       <!-- 配置 dialect 方言 , 明确告诉 hibernate 连接是哪种数据库 -->

       <property name="dialect">org.hibernate.dialect.OracleDialect</property>

       <!-- 显示出对于 sql -->

       <property name="show_sql">true</property>

       <!-- 指定管理的对象映射文件 -->

       <mapping resource="com/sina/domain/Employee.hbm.xml"/>

</session-factory>

</hibernate-configuration>

 

测试类:

 

Java代码 复制代码  收藏代码
  1. package com.sina.view;   
  2.   
  3. import java.sql.Date;   
  4.   
  5. import org.hibernate.Session;   
  6. import org.hibernate.SessionFactory;   
  7. import org.hibernate.Transaction;   
  8. import org.hibernate.cfg.Configuration;   
  9.   
  10. import com.sina.domain.Employee;   
  11.   
  12. public class TestMain {   
  13.   
  14.     /**  
  15.      * @param args  
  16.      */  
  17.     public static void main(String[] args) {   
  18.   
  19.     //添加一个雇员   
  20.         //1.得到Configuration   
  21.         Configuration configuration= new Configuration().configure();   
  22.         //2.得到SessionFactory(会话工厂,这是一个重量级的类,因此要保证在一个应用程序中只能有一个)   
  23.         SessionFactory sessionFactory=configuration.buildSessionFactory();   
  24.           
  25.         //3. 从SessionFactory中取出一个Session对象(它表示 和数据库的出一次会话)   
  26.         Session session=sessionFactory.openSession();   
  27.         //4. 开始一个事务   
  28.         Transaction transaction = session.beginTransaction();   
  29.         //保存一个对象到数据库(持久化一个对象)   
  30.         Employee emp=new Employee();   
  31.         emp.setEmail("[email protected]");   
  32.         emp.setHiredate(new java.util.Date());   
  33.         emp.setName("sina");   
  34.         //不要给id,因为它是自增的   
  35.         session.save(emp);//insert into employee (name,id,...) value(?,?,?)   
  36.         transaction.commit();   
  37.          
  38. }   
  39.   
  40. }  

你可能感兴趣的:(Hibernate学习总结之基础篇)