第一个hibernate项目,
Hibernate中文“冬眠”,我们将对象保存到数据库的这个行为比喻成对象的冬眠。
Hibernate优点:提高生产力(不用开发枯燥的jdbc),开发更对象化,移植性(自动生成针对不同厂家的sql---方言),支持透明持久化(轻量级),没有侵入性。
Hibernate 弱点,不适用的情况:1,批量操作;2,需要使用数据库的特定优化机制,因为sql是由hibernate控制生成的,个人无法控制。
Hibernate的目标是对于开发者通常的数据持久化相关的编程任务,解放其中的95%。对于以数据为中心的程序来说,它们往往只在数据库中使用存储过程来实现商业逻辑,hibernate可能不是最好的解决方案。
正确开发步骤:
先建立对象模型(领域模型)
建立映射关系
根据映射关系导出相应的表。
现实中先考虑数据库。(观念慢慢改变)
使用hibernate步骤:
1、新建java项目
2、创建User Library,加入如下jar
* HIBERNATE_HOME/hibernate3.jar
* HIBERNATE_HOME/lib/*.jar
* MySql jdbc驱动
3、创建hibernate配置文件hibernate.cfg.xml,为了便于调试最好加入log4j配置文件
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bjsxt</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
//文件名可以随意取,习惯User.hbm.xml格式
<mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、定义实体类
package com.bjsxt.hibernate;
import java.util.Date;
public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getExpireTime() {
return expireTime;
}
public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
5、定义User类的映射文件User.hbm.xml
<hibernate-mapping>
<class name="com.bjsxt.hibernate.User">
<id name="id">
//uuid 据说10000不会重复
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
6、将User.hbml.xml文件加入到hibernate.cfg.xml文件中
<mapping resource="com/bjsxt/hibernate/User.hbm.xml"/>
7、编写hbm2ddl工具类,将实体类生成数据库表
public class ExportDB {
public static void main(String[] args) {
//hibernate支持两种配置文件 .properties和.xml
//读取.properties文件,不经常使用
Configuration cfg = new Configuration();
//读取hibernate.cfg.xml文件,一般使用这种
Configuration cfg = new Configuration().configure();
//工具类,生成ddl,将对象转化成表
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
8、开发客户端
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//创建SessionFactory,每个数据库对应一个,跟二级缓存相关
//重量级对象,创建比较耗时,线程安全的,可以只创建一次
SessionFactory factory = cfg.buildSessionFactory();
Session session = null;
try {
//session是用到的时候去连接池中拿,对实体对象生命周期管理,缓存相关,
//非线程安全的,用完之后必须关闭
session = factory.openSession();
//开启事务
session.beginTransaction();
User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存数据
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
为了方便跟踪sql执行,在hibernate.cfg.xml文件中加入<property name="hibernate.show_sql">true</property>