【Hibernate学习之路1】Hibernnate入门
来源:互联网 发布:单相1.5kw两极数据 编辑:程序博客网 时间:2024/06/05 08:42
Hibernnate入门
在本文中的实例使用Oracle10g以及其中的Emp表作为示范。
1. 持久化
- 在了解持久化之前必须先了解数据的几个状态,数据状态如下:
- 数据保存在内存中(瞬时状态)
- 数据保存在数据库中并可以被Session操作(持久状态)
- 数据库保存在数据库中,并且没有处在Session的生命周期。(脱管状态)
- 持久化:把瞬时对象保存在数据库中的过程叫做持久化。
2. Hibernnate概述
- Hibernnate是一个持久化技术。
- 是一个把数据对象保存到关系型数据库中的数据持久化框架
- ORM技术(完成对象数据到关系型数据库映射的机制成为对象关系映射,简称ORM)
3. 使用Hibernnate的三个准备
- 添加Hibernate支持
在需要的项目名称右击-Build Path->Configure Build Path..->Java Build Path->Library->Add Library->MyEclipse Library->Hibernate 3.x Core Library - 编写实体类与映射文件
实体类
package com.web.bean;public class Emp implements Serializable{// 声明私有属性 private Integer empno; private String ename; private String job; private Integer Mgr; private String hiredate;//注意是String类型 private Double sal; private Double comm; private Integer deptno;// 有参无参构造方法 public Emp() { } public Emp(Integer empno, String ename, String job, Integer mgr, String hiredate, Double sal, Double comm, Integer deptno) { this.empno = empno; this.ename = ename; this.job = job; this.Mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.deptno = deptno; }//每个属性的get/set方法 public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return Mgr; } public void setMgr(Integer mgr) { Mgr = mgr; } public String getHiredate() { return hiredate; } public void setHiredate(String hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; }}
- 不能是一个final类
- 实现Serializable接口
- 有无参构造函数
- 属性小写
- 每个属性都要有get/set方法
映射文件
<?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><!--将项目中的bean与数据库表对应--> <class name="com.web.bean.Emp" table="emp"> <!--设置主键以及其对应字段--> <id name="empno" column="empno"> <generator class="assigned"></generator> </id> <!--将bean中的属性与表中的数据项一一对应--> <property name="ename" column="ename"></property> <property name="job" column="job"></property> <property name="mgr" column="mgr"></property> <property name="hiredate" column="hiredate"></property> <property name="sal" column="sal"></property> <property name="comm" column="comm"></property> <property name="deptno" column="deptno"></property> </class></hibernate-mapping>
最好在项目中单独创建一个专门用于存放xxx.hbm.xml的hbm包。
3. 编写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> <!--数据库驱动--> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </property> <!--数据库地址--> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <!--账号密码--> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <!--数据库“方言”--> <property name="dialect"> org.hibernate.dialect.Oracle10gDialect </property> <!--是否显示SQL语句--> <property name="show_sql">true</property> <!--对应的映射表--> <mapping resource="com/web/hbm/Emp.hbm.xml" /></session-factory></hibernate-configuration>
注意hibernate.cfg.xml放在src文件夹下,创建过程如下:
4. 使用Hibernnate的7个开发步骤
- 创建Configuration
- 创建SessionFactory
- 创建Seesion
- 开始一个事务
- 持久化操作
- 提交事务
- 关闭Session
public class Test { public static void main(String[] args) { Session session = null; Transaction tran = null; try { Configuration config = new Configuration().configure();//创建Configuration SessionFactory factory = config.buildSessionFactory();//创建SessionFactory session = factory.openSession();//创建Session tran = session.beginTransaction();//开始一个事务 Emp e = new Emp(); e.setEmpno(1234); e.setEname("顾一城"); e.setJob("CLERK"); e.setMgr(7902); e.setHiredate("1994/1/27"); e.setSal(800.0); e.setComm(300.0); e.setDeptno(20); session.save(e);//持久化操作 tran.commit();//提交事务 } catch (Exception e) { e.printStackTrace(); tran.rollback();//回滚事务 } finally { session.close();//关闭Session } }}
0 0
- 【Hibernate学习之路1】Hibernnate入门
- hibernate学习之路(hibernate入门配置)
- Hibernate 学习入门1
- Hibernate的学习之路二(入门)
- Hibernate的学习之路四(入门代码)
- Hibernate学习之入门案例笔记
- Hibernate入门学习之配置文件的理解
- Hibernate入门学习笔记1
- Hibernate学习总结--入门1
- hibernate学习之路
- 【Hibernate学习笔记】hibernate入门
- Hibernate 学习笔记(1):入门
- hibernate 入门学习
- hibernate validate入门学习
- java学习:Hibernate入门
- hibernate学习笔记--入门
- Hibernate入门学习
- hibernate 入门学习demo
- 日经春秋 20150503
- HDU 1874 畅通工程续
- HDU 1548 A strange lift
- 日经社説 20150503 憲法のどこが不備かもっと説明せよ
- KMP算法详解
- 【Hibernate学习之路1】Hibernnate入门
- 设计模式-外观模式的理解
- HDU 2680 Choose the best route
- 《软件定义网络-SDN与OpenFlow解析》第一章
- HDU 2066 一个人的旅行
- STL优先队列详解
- HDU 1385 Minimum Transport Cost
- 欢迎使用CSDN-markdown编辑器
- 【CUDA】初步了解PageLocked host memory的mapped memory功能使用