您的位置:首页 > 编程语言 > Java开发

Hibernate第一节:第一个Hibernate项目

2011-11-20 09:50 246 查看
第一个Hibernate项目
1、创建java项目

HibernateFirst

2、创建User Library,加入依赖包

* HIBERNATE_HOME/lib/*.jar

* HIBERNATE_HOME/hibernate3.jar

* 加入数据库驱动(Oracle驱动)

3、提供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>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">admin</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<mapping resource="org/xiaolong/entity/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>

4、建立实体类User.java

package org.xiaolong.entity;
import java.util.Date;
public class UserInfo {
private Integer userid;
private String username;
private String password;
private Date birthday;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}

5、提供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 package="org.xiaolong.entity">
<class name="UserInfo" table="userinfo" schema="ADMIN">
<id name="userid" column="id" type="int">
<generator class="native"></generator>
</id>
<property name="username" insert="true" length="50" type="string"></property>
<property name="password" not-null="true"></property>
<property name="birthday" type="date"></property>
</class>
</hibernate-mapping>

6、将User.hbm.xml文件加入到hibernate.cfg.xml文件中

<mapping resource="org/xiaolong/entity/UserInfo.hbm.xml"/>
7、编写工具类ExoprtDB.java,将hbm生成ddl,也就是hbm2ddl

package org.xiaolong.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
* 将hbm生成ddl
* @author xiaolong
*
*/
public class ExportDB {
public static void main(String[] args) {
//默认读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
//param1 print the DDL to the console
//param2 export the script to the database
export.create(true, true);
}

}

下面我们来执行该工具类ExportDB,手动创建UserInfo表,执行后控制台打印结果:



再去数据库中查看,会发现表已成功创建:

这里我们也可以使用Hibernate自带的功能区生成数据库表,只需在hibernate.cfg.xml文件中配置如下信息即可:

<property name="hbm2ddl.auto">update</property>

8、创建测试类UserDAOTest,添加用户数据到oracle

package org.xiaolong.test;
import java.util.Date;
public class UserDAOTest {
@Test
public void addUser() {
// 读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
// 建立SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
// 取得session
Session session = null;
try {
session = factory.openSession();
// 开启事务
session.beginTransaction();
UserInfo user = new UserInfo();
user.setUsername("xiaolong");
user.setPassword("123");
user.setBirthday(new Date());
// 保存UserInfo对象
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>

最好加入log4j配置文件,将该配置文件拷贝到src下,便于程序的调试

查看控制台打印结果:





OK,第一个Hibernate项目运行成功!当然其中还有很多细节没有说明,需要了解的去查下资料即可。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息