Hibernate1之例子

来源:互联网 发布:特效ae软件下载 编辑:程序博客网 时间:2024/05/29 13:28

Hibernate
1、描述
orm(object relation mapping框架,处于持久层
对jdbc的轻量级封装
2、优点
跨数据库,不需要重写业务层
这里写图片描述
这里写图片描述
这里写图片描述
入门案例 第二种方式
这里写图片描述

/**SessionFactory是重量级,做成单例一个数据库对应一个*/

表内容
表内容
这里写图片描述
1、建表
create table employee(id number primary key,name nvarchar2(64) not null,email nvarchar2(64) not null,hiredate date not null);
2、序列

create sequence emp_seqstart with 1increment by 1minvalue 1nomaxvaluenocyclenocache;

结构图
这里写图片描述
创建pojo

package net.hibernate.pojo;import java.util.Date;//对应表首字母大写 序列化,为唯一public class Employee {private Integer id;private String name;private String email;private 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 Date getHiredate() {    return hiredate;}public void setHiredate(Date hiredate) {    this.hiredate = hiredate;}}

Employee.hbm.xml文件

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">    <!-- 操作的是哪个包 --><hibernate-mapping package="net.hibernate.pojo">    <class name="Employee" table="employee">    <!-- id用与指定主键属性 -->        <id name="id" length="64" column="id">        <!-- generator用来指定主键生成策略hilo、uuid、increment、sequence、native -->        <!-- 如果不希望用序列,class="assigned" -->            <generator class="sequence">                <param name="sequence">emp_seq</param>            </generator>        </id>        <property name="name" type="string">            <column name="name" not-null="false" />        </property>         <property name="email"  not-null="false" type="java.lang.String" />         <property name="hiredate" not-null="false" type="java.util.Date"/>      </class></hibernate-mapping>

hibernate.cfg.xml配置文件

<!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory>    <property name="hibernate.dialect">        org.hibernate.dialect.Oracle9Dialect    </property>    <property name="hibernate.connection.driver_class">        oracle.jdbc.driver.OracleDriver    </property>    <property name="hibernate.connection.username">scott</property>    <property name="hibernate.connection.password">tiger</property>    <property name="hibernate.connection.url">        jdbc:oracle:thin:@localhost:1521:ORCL    </property>    <property name="hibernate.hbm2ddl.auto">update</property>    <!-- 控制台显示sql语句 -->    <property name="show_sql">true</property>    <property name="format_sql">true</property>    <!-- 映射文件 -->    <mapping resource="net/hibernate/pojo/Employee.hbm.xml" /></session-factory></hibernate-configuration>

工具类,获得session

package net.hibernate.view.util;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class HibernateUtil {private static SessionFactory sessionFactory;static{    Configuration config=new Configuration().configure();    sessionFactory=config.buildSessionFactory();}    public static Session getSession(){        return sessionFactory.openSession();     }}

测试代码

package net.hibernate.view;import java.util.Date;import org.hibernate.Session;import org.hibernate.Transaction;import net.hibernate.pojo.Employee;import net.hibernate.view.util.HibernateUtil;public class TestMain {    public static void main(String[] args) {        //完成crud操作        insertEmp();    }    public static void insertEmp(){        Employee emp=new Employee();        emp.setName("苏东坡");        emp.setEmail("2812394@qq.com");        emp.setHiredate(new Date());        Session session=HibernateUtil.getSession();        //事务        Transaction tr=session.beginTransaction();        try {            session.save(emp);            tr.commit();        } catch (Exception e) {            e.printStackTrace();            tr.rollback();        }finally{                       session.close();        }       }}
0 0