Hibernate的第一个例子说明
1、创建User Library,加入如下jar
hibernate3.jar
lib/*.jar
MySql jdbc驱动 mysql-connector-java-5.1.7-bin.jar
2、创建hibernate配置文件hibernate.cfg.xml,为了便于调试最好加入log4j配置文件
3、定义实体类User
package com.wsz.entity;
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;
}
}
4、定义User类的映射文件User.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>
<!--这里没有定义class的属性table,那么默认的表就是user-->
<class name="com.wsz.entity.User">
<!--这里没有定义实体对象属性对应的列名,那么默认就是属性名id-->
<id name="id">
<!--定义id的生成策略-->
<generator class="uuid" />
</id>
<property name="name" />
<property name="password" />
<property name="createTime" />
<property name="expireTime" />
</class>
</hibernate-mapping>
5、将User.hbml.xml文件配置到hibernate.cfg.xml文件中
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!--一个数据库一个session-factory定义-->
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<!--数据库方言,不同数据库的方言不同-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--执行时会输出sql语句,方便调试-->
<property name="hibernate.show_sql">true</property>
<!--所有的实体对象的配置文件都要在这里关联起来-->
<mapping resource="com/wsz/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、创建数据库,打开mysql控制台,创建测试数据库”hibernate”
Create database hibernate;
Use hibernate;
show tables;
7、编写工具类ExportDB并执行,将实体类生成数据库表
注意:为了方便跟踪sql执行,在hibernate.cfg.xml文件中加入<property name="hibernate.show_sql">true</property>
package com.wsz.entity;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
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);
}
}
desc user;能显示user表的结构
8、客户端调用
package com.wsz.entity;
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) {
// 读取hibernate.cfg.xml文件,如果自定义名称,就把名称传进去
// 如果是 new Configuration(),默认读取的是 hibernate.properties配置文件
Configuration cfg = new Configuration().configure();
// 创建SessionFactory,这个对象是线程安全的,所有人可以共享
// 由于创建SessionFactory消耗大,所以这里最好抽取成一个单例对象的方法.供所有线程调用.
SessionFactory factory = cfg.buildSessionFactory();
// Session对象不是线程安全的,所以应该每次使用完就关闭,不然可能出现异常
// 多个方法使用同一个Session时,可以使用
// new Configuration().configure().buildSessionFactory().getCurrentSession()
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());
// 保存数据
session.save(user);
// 提交事务
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
// 回滚事务
session.getTransaction().rollback();
} finally {
if (session != null) {
if (session.isOpen()) {
// 关闭session
session.close();
}
}
}
}
}
工具类: SessionFactory
package com.wsz.entity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
// factory.getCurrentSession()可以用于同一线程的多个方法,以保证使用同一个Session
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}