Hibernate3回顾-1-部署

web备份版本,详见doc版本。

一、背景(部署简单回顾)

我们知道,一个Hibernate快速上手的简单流程是这样。

1引入对应jar包。

中间涉及log4的jar包和配置,略。

2 实体类

 package com.test.entity;
import java.util.Date; /**
* 你可以看到这个类对属性的存取方法(getter and setter method)使用了标准
JavaBean 命名约定,同时把类属性(field)的访问级别设成私有的(private)。这是推荐的
设计,但并不是必须的 */
public class Event {
private Long id;
private String title;
private Date date;
//所有的持久化类(persistent classes)都要求有无参的构造器,因为 Hibernate 必须使用 Java
//反射机制来为你创建对象
public Event() {}
//getter/setter
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}

3实体类映射文件

Event.hbm.xml

 <?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 package="org.hibernate.tutorial.domain">
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native" />
</id>
</class>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" />
</hibernate-mapping>

4配置hibernate.properties或者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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!--
具体的配置信息可参见hibernate_home/etc/hibernate.properties相关配置项
如何要移植数据时,只要将下面数据库信息修改就可以了。
-->
<!-- 配置mysql数据库连接串 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3036/hibernate_first</property>
<!-- 配置mysql数据库jdbc驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 配置mysql数据库连接用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 配置mysql数据库连接用户密码 -->
<property name="hibernate.connection.password">root</property>
<!--配置数据库适配器(使用何中数据库)-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否显示hibernate的SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 实体类导出至数据库时,如果存在的表处理方式:
hibernate.hbm2ddl.auto :(create-drop、create、update、validate)
-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置实体类映射文件 位于property之后
映射文件要求为完整路径,目录之前使用/隔开
-->
<mapping resource="com/test/mapping/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>

5-1启动

方式一使用hibernate工具类将对象模型生成关系模型(hbm to ddl)

(也就是实体类生成数据库中的表)

 import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* Hibernate工具<br/>
* 将对象模型生成关系模型(将对象生成数据库中的表)
* 把hbm映射文件转换成DDL
* 生成数据表之前要求已经存在数据库
* 注:这个工具类建立好后,以后就不用建立了。以后直接Copy来用。
/
public class ExportDB {
public static void main(String[] args){
/*
* org.hibernate.cfg.Configuration类的作用:
* 读取hibernate配置文件(hibernate.cfg.xml或hiberante.properties)的.
* new Configuration()默认是读取hibernate.properties
* 所以使用new Configuration().configure();来读取hibernate.cfg.xml配置文件
*/
Configuration cfg = new Configuration().configure(); /*
* org.hibernate.tool.hbm2ddl.SchemaExport工具类:
* 需要传入Configuration参数
* 此工具类可以将类导出生成数据库表
*/
SchemaExport export = new SchemaExport(cfg); /*
* 开始导出
* 第一个参数:script 是否打印DDL信息
* 第二个参数:export 是否导出到数据库中生成表
*/
export.create(true, true); }
}

5-2启动

方式二:将引用部署于web容器。根据hibernate.cfg.xml中的配置:

<property name="hibernate.hbm2ddl.auto">create 或者update</property> Hibernate将自动更新表格结构。

6 client常规操作

以下代码来源wtj276

 import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class Client { public static void main(String[] args){ //1读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure(); /*
* 2创建SessionFactory
* 一个数据库对应一个SessionFactory
* SessionFactory是线程安全的。
*/
SessionFactory factory = cfg.buildSessionFactory(); //3创建session
//此处的session并不是web中的session,而是HibernateSession
//session只有在用时,才建立concation,session还管理缓存。
//session用完后,必须关闭。
//session是非线程安全,一般是一个请求一个session.
Session session = null; try { session = factory.openSession(); //5手动开启事务(可以在hibernate.cfg.xml配置文件中配置自动开启事务)
session.beginTransaction(); User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
/*
* 6增删改查操作
保存数据等操作,此处的数据是保存对象,这就是hibernate操作对象的好处,
* 我们不用写那么多的JDBC代码,只要利用session操作对象,至于hibernat如何存在对象,这不需要我们去关心它,
* 这些都有hibernate来完成。我们只要将对象创建完后,交给hibernate就可以了。
*/
session.save(user); //7.1提交事务
session.getTransaction().commit(); } catch (Exception e) {
e.printStackTrace();
//7.2异常,回滚事务
session.getTransaction().rollback();
} finally {
if (session != null) {
//8关闭session
session.close();
}
}
}
}

为了方便跟踪sql语句执行,可以在hibernate.hbm.xml中加入下以代码:

<property name="hibernate.show_sql">true</property>

完成

上一篇:【MySQL】insert批量插入优化方案


下一篇:2017-4-25/设计缓存(LFU)