Hibernate3学习笔记1

1、数据要持久化实现方案 EJB;JDBC;ORM。

2、比较

        内存消耗      性能               开发效率

EJB       大        不同系统不同                         底

 

ORM     中        不同系统不同                       有工具速度较快

 

JDBC    小        性能最好但要交较高                麻烦

 

3、应用体系结构

struts2<------------------>dao

                              ^         |

                               |         |

                               |      hibernate<------>数  

                               v        |                       据

                              模型<---- 映射文件<-->  库

4、java project hibernate demo

(1)导入hibernate包和数据库包

 

 

(2)配置文件hibernate.cfg.xml  放在src下

<?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="hibernate.connection.username">student</property>
        <property name="hibernate.connection.password">student</property>  
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:xe</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
       
        <!--
        <property name="hibernate.connection.url">
         jdbc:mysql://localhost:3306/message?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;autoReconnectForPools=true
        </property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">
         com.mysql.jdbc.Driver
        </property>
         -->
       
  <property name="hibernate.cache.provider_class">
   org.hibernate.cache.EhCacheProvider
  </property>
        <property name="show_sql">true</property>
       
        <!-- Mapping File list -->
        <mapping resource="com/weixun/bean/Student.hbm.xml" />

    </session-factory>
</hibernate-configuration>

 

(3)写实体对象Student.javaStudent.hbm.xml文件

Student.java

 

package com.weixun.bean;

import java.util.Date;

public class Student {
 private Long id;
 private String num;
 private String name;
 private Date birthday;
 private Float height;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getNum() {
  return num;
 }

 public void setNum(String num) {
  this.num = num;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Date getBirthday() {
  return birthday;
 }

 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }

 public Float getHeight() {
  return height;
 }

 public void setHeight(Float height) {
  this.height = height;
 }

}

 

Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.weixun.bean">
 <class name="Student" table="student"> 
  <id name="id" column="id">
           <generator class="sequence">
             <param name="sequence">SEQ_STUDENT</param>     
   </generator>
  </id>
  <property name="num" column="num"/>
  <property name="name" column="name"/>
  <property name="birthday" column="birthday"/>
  <property name="height" column="height"/>  
 </class> 
</hibernate-mapping>

(4)测试类

package com.weixun.test;

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

import com.weixun.bean.Student;

public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  //得到map信息
  SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
  //得到session
  Session session = sessionFactory.openSession();
  Student stu=(Student)session.get(Student.class, new Long(2));
  System.out.println(stu.getName());
 }

}

                                       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值