【前言】
学习Hibernate已经有很长的一段时间了,今天来补充一下如何搭建Hibernate开发环境以及例子的应用。
【开发环境】
win10+jdk1.7 + MyEclipse + Tomcat5.0 + MySQL
【开发实例】
1、整体图
2、jar包
步骤:右击项目——>Build Path——>Add Library——>User Library——>Next——>User Library——>New——> HIBERNATE3——>OK
hibernate3.jar Hibernate的核心包
dom4j-1.6.1.jar dom4j读取xml文件包
mysql-connector-Java-3.1.13-bin.jar MySQL的jdbc驱动包
log4j-1.2.11.jar 记录日志框架
必须要引入的jar:
commons-logging-1.0.4.jar 抽象的日志记录框架
commons-collections-2.1.1jar 各种集合类和集合工具类的封装
cglib-2.1.3.jar 动态代理,Hibernate用它来实现PO字节码的动态生成
asm.jar cglib需要依赖的jar,ASM字节码库
3、提供hibernate.cfg.xml文件,完成基本的配置
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bjpowernode</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!--
<property name="hibernate.format_sql">true</property>
-->
<!--将User.hbm.xml文件加入到hibernate.cfg.xml文件中 -->
<mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、建立实体类User.java
package com.bjpowernode.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.hbm.xml文件,完成实体类的映射
<hibernate-mapping>
<class name="com.bjpowernode.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime" type="date"/>
<property name="expireTime" type="date"/>
</class>
</hibernate-mapping>
6、编写工具类ExoprtDB.java,将hbm生成ddl,也就是hbm2ddl
/**
* 将hbm生成ddl
* @author Administrator
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
7、建立客户端类Client,添加用户数据到mysql
public class Client {
public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//取得session
Session session = null;
try {
session = factory.openSession();
//开启事务
session.beginTransaction();
User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存User对象
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
}
}
最好加入如下配置项,方便观察hibernate sql的生成:
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>