一、首先在Eclipse创建一个项目,在项目中创建一个文件夹取名“lib”,用来存放和hibernate相关的jar包
在本例中,需要用到的jar包如下
1、数据库的驱动包
2、hibernate.jar主要依赖包
3、dom4j包
4、log4包
5、commons-logging包
6、commons-collections包
7、cglib包
8、asm包
9、asm-commons包
10、jta包
二、在hibernate.cfg.xml中配置信息,如下所示
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/hibernateTest
</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">xxx</property>
<!-- 设置方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 在控制台打印出sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 根据需要自动创建数据库 -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<!--加入实体的配置文件-->
<mapping resource="com/hibernate/Category.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三、创建java实体bean——持久化类
package com.niit.hibernate;
public class Category {
private int id;
private String categoryName;
private String categoryInfo;
private int categoryOrder;
private Date categoryFoundTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getCategoryInfo() {
return categoryInfo;
}
public void setCategoryInfo(String categoryInfo) {
this.categoryInfo = categoryInfo;
}
public int getCategoryOrder() {
return categoryOrder;
}
public void setCategoryOrder(int categoryOrder) {
this.categoryOrder = categoryOrder;
}
public Date getCategoryFoundTime() {
return categoryFoundTime;
}
public void setCategoryFoundTime(Date categoryFoundTime) {
this.categoryFoundTime = categoryFoundTime;
}
四、创建hbm文件,文件名 Category.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.niit.hibernate">
<class name="Category" table="t_category" lazy="true">
<id name="id">
<generator class="native"><!--uuid 不生成序列 对应实体中的String -->
</generator>
</id>
<property name="categoryName" type="string" length="50"></property>
<property name="categoryInfo" type="string" length="100"></property>
<property name="categoryOrder" type="integer" length="9"></property>
<property name="categoryFoundTime" type="date"></property>
</class>
<!-- 可以在一个hbm文件中加入多个类的配置,但是一般不这样用,更多的是选择多个hbm文件加入hibernate.cfg.xml文件中 -->
<class name="Category2" table="t_category2" lazy="true">
<id name="id">
<generator class="uuid"></generator>
</id>
<property name="categoryName" type="string" length="50"></property>
<property name="categoryInfo" type="string" length="100"></property>
<property name="categoryOrder" type="integer"></property>
<property name="categoryFoundTime" type="date"></property>
</class>
</hibernate-mapping>
五、hibernateTools工具
public class HibernateTools {
/*
* 将对象持久化到数据库
*/
public boolean save(Object obj){
boolean b = false;
//加载配置文件hibernate.cfg.xml
Configuration cfg=new Configuration().configure();
//创建SessionFactory
SessionFactory factory=cfg.buildSessionFactory();
Session session=null;
try{
//创建session,并开启事务
session=factory.openSession();
session.beginTransaction();
//保存->缓存
session.save(obj);
//提交->发出语句
//持久对象->数据库中存在该记录,纳入session管理
session.getTransaction().commit();
b= true;
}catch(Exception ex){
b= false;
ex.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
return b;
}
下面几行是用来生成表结构
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
六、测试
Junit测试
public void testSave2(){
HibernateTools tools=new HibernateTools();
Category2 category=new Category2();
category.setCategoryName("xxx");
category.setCategoryInfo("lll");
category.setCategoryOrder(1);
category.setCategoryFoundTime(new Date());
//断言
Assert.assertEquals(true,tools.save(category));
}
转载请注明出处 http://blog.csdn.net/di1nuli/article/details/7082860