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>

 

测试类:

package com.sina.view;

import java.sql.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.sina.domain.Employee;

public class TestMain {

    /**
     * @param args
     */
    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("[email protected]");
        emp.setHiredate(new java.util.Date());
        emp.setName("sina");
        //不要给id,因为它是自增的
        session.save(emp);//insert into employee (name,id,...) value(?,?,?)
        transaction.commit();
      
}

}

猜你喜欢

转载自chenzheng8975.iteye.com/blog/1606142