How to use Hibernate: 如何使用Hibernate(用MyEclipse)
step1. Create Hibernate.cfg.xml 创建Hibernate的配置文件 !!
step2. Create tables in Database 创建表单(在数据库里)
step3. Create PO(Persistent Objects) 创建持久化类
step4. Create XXX.hbm.xml 创建对象-关系映射文件 !!!
step5. Create DataAccess Objects 通过Hibernate API编写访问数据库的代码
-------------------------------------------
step1. Create Hibernate.cfg.xml 创建Hibernate的配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- mapping files -->
<property name="connection.autocommit">true</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/bookstoresql
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
</session-factory>
</hibernate-configuration>
------------------------------------------------
step2. Create tables in Database 创建表单在数据库里
create table book( id bigint not null auto_increment, title varchar(50) not null, price double, description varchar(200), primary key(id));
注意: 用Mysql 数据库, table Type 应设为InnoDB
----------------------------------------------------------
step3. Create PO(Persistent Objects) 创建持久化类
public abstract class AbstractBook implements Serializable
public class Book extends AbstractBook
用MyEclipse 自动从数据库表单生成的两个类(一个抽象, 一个具体)
如果自己手工建, 可以只生成的一个具体类
---------------------------------------------------
step4. Create XXX.hbm.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){
Transaction tx=null;
try{
Session sess= HibernateSessionFactory.currentSession();
tx=sess.beginTransaction();
//do some work
sess.save(b);
tx.commit(); ////!!!!!!save book in DB
}catch(Exception e){
e.printStackTrace();
if(tx!=null){
try{
tx.rollback();
}catch (Exception ee){ ee.printStackTrace();}
}
}finally{
try{
HibernateSessionFactory.closeSession();
}catch (Exception ee){ ee.printStackTrace();}
}
}