hibernate3.2(一)第一个hibernate程序

:roll: 步骤:
1.创建一个java project;
2.创建User Libraries,加入如下jar:
* hibernate-3.2/Hibernate3.jar(核心jar)
* hibernate-3.2/lib/下的全部jar
* MySql jdbc驱动
注意:创建User Library 的时候不要点[System library]的对勾.
3.在工程中build path > Add libraries > User Library > 自定义library .
4.在工程中src/下加入hibernate.cfg.xml,可以从hibernate3.2/etc/下找到。
(Hibernate支持hibernate.properties和两种文件格式的配置,大多用
hibernate.cfg.xml)。
5.hibernate.cfg.xml的写法,删除多余部分,只剩下session-factory 节点,
然后参照模板文件hibernate.properties完善配置,hibernate.properties
中全是以键值对的形式存在,用在hibernate.cfg.xml中一般键做name属性,值做
value节点值:


<!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>
</hibernate-configuration>

6.为了便于调试,最好在src/下加入log4j配置文件(hibernate3.2/etc/下可以找到)

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<!-- 一个数据库对应一个sessionfactory -->
<session-factory>
//jdbc:mysql://localhost/数据库的名字 或者 //jdbc:mysql://局域网ip/数据库的名字
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>
//如果这里换成了其他的数据库,只要修改值就可以,因为hibernate.cfg.xml数据库方言的存在,所以非常方便
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
//Hibernate适配器,又称方言,它带来了很好的移植性
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>

<property name="hibernate.hbm2ddl.auto">update</property>
设置使用工具类生成数据库表的时候,只修改修改过的部分,不是完全覆盖。

7.Hibernate正确的使用方式是创建实体类,写好映射文件,让Hibernate生成数据库表,
示例,新建一个实体类:

package com.wyx.hibernate;

import java.util.Date;

public class User {
private String id;//给实体一个唯一性的标识
private String name;
private String password;
private Date createTime;
private Date expireTime;

//getter and setter
....
}

8.定义实体类的映射文件,完成 O → R 的映射mapper,文件名格式:xxx.hbm.xml,通常
放在和实体类一个目录下,该文件可以参考Hibernate3\eg\org\hibernate\aution\中的映射文件。

<?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>
<!-- 删掉其他的 只剩下根节点 -->
</hibernate-mapping>


<?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>
<!-- 对类进行映射,name = "完整路径" , 默认存的表和实体类的名字一致,
可以用 table = "xxx"更改生成的数据库表-->
<class name="com.wyx.hibernate.User">
<!-- 标识 数据表默认字段和实体属性名一致,可用column="xxx"来重新定义字段名-->
<id name="id">
<!-- 主键的生成策略 uuid全局的唯一标识,32位字符串,一般一万年不会重复-->
<generator class="uuid"/>
</id>
<!-- 除了标识以外,都用property来映射 ,生成的字段可以重命名-->
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>

注意:id标签必须写在property的前面

9.xxx.hbm.xml写好之后,Hibernate还不知道这个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>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- resource ="映射文件的完整路径,不是包名!"-->
<!-- 这样Hibernate就会把xxx.hbm.xml加载进来,分析并生成数据表 -->
<mapping resource="com/wyx/hibernate/User.hbm.xml"/>

</session-factory>
</hibernate-configuration>

10.hibernate不会自动把实体类映射成数据表,需要我们编写hbm2ddl工具类,把实体类
生成数据库表。

package com.wyx.hibernate;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;


public class ExportDB {

public static void main(String[] args) {
//读取hibernate.cfg.xml文件 , 如果不加.configure()默认会读取hibernate.properties
Configuration cfg =new Configuration().configure();

SchemaExport export = new SchemaExport(cfg);

export.create(true, true);
}

}

11.打开数据库新建数据库hibernate_first和数据表User(Hibernate.cfg.xml
中的名字),执行main()后查看数据库表中字段的情况,Hibernate已经为我们
在表中添加好了字段。
12.开发客户端,在表中插入数据:

package com.wyx.hibernate;

import java.util.Date;

import org.hibernate.HibernateException;
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
Configuration cfg = new Configuration().configure();
//创建sessionfactory,一个数据库对应一个sessionfactory,
//如果有多个sessionfactory,则hibernate.cfg.xml中要有多个
//sessionfactory节点
SessionFactory factory =cfg.buildSessionFactory();

//不要用classic下的Session创建,那是hibernate2.x用的
//这个Session不是http的session,理解为对connection对象的封装
Session session = null;
try {
//从工厂创建一个session(类似于创建连接)
session = factory.openSession();

//开启事务,hibernate默认的autocommit是false
//所以要手动开启事务,手动提交
session.beginTransaction();
//实体类User new出来的对象user对应表中的一条记录
User user =new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//调用save方法保存进DB
session.save(user);
//用session获得上文开启的事物并提交
session.getTransaction().commit();
} catch (HibernateException e) {
e.printStackTrace();
// 出现问题之后,回滚事务
session.getTransaction().rollback();
}finally
{
//最后关闭session(关闭连接)
if(session != null)
{
//判断session是打开的才关闭
if(session.isOpen())
{
session.close();
}
}

}
}

}


执行Client,查看打印信息,Hibernate为我们打印了很多信息,如果不想看太多,可以进
log4j中加#注释掉,但是log4j.rootLogger =warn , stuout 不要注释,这样再次执行clent 控制台什么提示也没有了,但操作时成功的,数据库又多了一条数据,可以让Hibernate只显示sql语句,修改hibernate.cfg.xml,加入show_sql值:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--打印sql语句 方便调试-->
<property name="hibernate.show_sql">true</property>
<!--实体类的映射文件别忘了加入hibernate.cfg.xml -->
<mapping resource="com/wyx/hibernate/User.hbm.xml"/>

</session-factory>
</hibernate-configuration>

Hibernate Tools 下载地址:
[url]http://www.jboss.org/tools/download/stable/3_1_GA.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值