Hibernate 配置

本文详细介绍了如何利用Hibernate框架在MyEclipse环境下进行数据库操作,包括配置文件的创建、数据库表单的建立、持久化类的设计、对象-关系映射文件的编写以及通过Hibernate API实现数据访问对象的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




-----------------------------------------------------

How to use Hibernate: 如何使用Hibernate(用MyEclipse)

1. Create hibernate.cfg.xml || hibernate.properties 创建Hibernate的配置文件 !!
2. Create tables in Database 创建表单(在数据库里)
3. Create PO(Persistent Objects) 创建持久化类
4. Create XXX.hbm.xml 创建对象-关系映射文件 !!!
5. Create DataAccess Objects 通过Hibernate API编写访问数据库的代码

-------------------------------------------
step1. Create hibernate.cfg.xml 创建Hibernate的配置文件

<hibernate-configuration>

<session-factory>
<!-- mapping files -->


<property name="connection.url">
jdbc:mysql://localhost:3306/hibernatebookstore?useUnicode=true&characterEncoding=gb2312
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>

<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect <!-- 2.0 -->
</property>

<!-- 3.0 org.hibernate.dialect.MySQLDialect -->

<property name="show_sql">
true
</property>

<mapping resource="com/liu/hibernate/po/Book.hbm.xml" />

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


或者Create hiberate.properties文件

connection.url=jdbc:mysql://localhost:3306/bookstoresql
connection.username=root
connection.password=root
connection.driver_class=com.mysql.jdbc.Driver
dialect=org.hibernate.dialect.MySQLDialect

------------------------------------------------
2. Create tables in Database 创建表单在数据库里

create table book( id bigint not null , title varchar(50) not null, price double, description varchar(200), primary key(id));

----------------------------------------------------------
step3. Create PO(Persistent Objects) 创建持久化类

public abstract class AbstractBook implements Serializable
public class Book extends AbstractBook

用MyEclipse 自动从数据库表单生成的两个类(一个抽象, 一个具体)
如果自己手工建, 可以只生成的一个具体类

---------------------------------------------------
step4. Create XXX.xml 创建对象-关系映射文件

Book.hbm.xml !!!!!!!!!!!!!!!!!!!!!!!

<hibernate-mapping package="com.liu.hibernate.po">

<class name="Book" table="book">
<id name="id" column="id" type="long">
<generator class="increment"/>
</id>

<property name="isbnNumber" column="isbnNumber" type="string" not-null="true" />
<property name="title" column="title" type="string" not-null="true" />
<property name="price" column="price" type="double" />
<property name="description" column="description" type="string" />
</class>

</hibernate-mapping>

在一般JAVA项目里, 配置文件放的路径为: 你的项目名称/hibernate.cfg.xml
or 你的项目名称/hibernate.properties
你的项目名称/你的包名/xxx.hbm.xml
你的项目名称必须设置在classpath中


在Web 项目里, 配置文件放的路径为: 你的Web应用名/WEB-INF/classes/hibernate.cfg.xml
or 你的Web应用名/WEB-INF/classes/hibernate.properties
你的Web应用名/WEB-INF/classes/你的包名/xxx.hbm.xml

--------------------------------------------------------
step5. Create DataAccess 通过Hibernate API编写访问数据库的代码

* 你可以用MyEclipse工具生成的HibernateSessionFactory来设定配置, 并获得Session

public void addBook(Book b){
Session sess= HibernateSessionFactory.getSession();
Transaction tx=sess.beginTransaction();

//do some work
sess.save(b);

tx.commit(); ////!!!!!!save book in DB
}

* 你可以自己生成SessionFactory对象, 并获得Session

1. 生成SessionFactory对象

//!!!!! 第一种用法 装载 hibernate.properties文件
SessionFactory sfactory=null;
try{
Configuration config=new Configuration();
config.addClass(packageName.Book.class);
// config.addClass("Book.hbm.xml");

sfactory=config.buildSessionFactory();
}catch(Exception e){ e.printStackTrace();}


//!!!!! 第二种用法 装载 hibernate.cfg.xml文件
SessionFactory sfactory=null;
try{
Configuration config=new Configuration();
config.configure("/hibernate.cfg.xml");
sfactory= config.buildSessionFactory();

//sfactory=new Configurtion().buildSessionFactory();
}catch(Exception e){ e.printStackTrace();}


Hibernate 就会在classpath中寻找相应的配置文件(hibernate.properties 或hibernate.cfg.xml).


2. 获得Session

Session se=sfactory.openSession();
Transaction tx=null;

tx=se.beginTransaction();

//do some work
se.delete("from Book as b ");// !!!delete all books

tx.commit();
se.close();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值