Hibernate第一节:第一个Hibernate项目

第一个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项目运行成功!当然其中还有很多细节没有说明,需要了解的去查下资料即可。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值