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&characterEncoding=utf8&autoReconnect=true&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.java和Student.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());
}
}