Hibernate系列教程01

Hibernate是个众所周知的ORM框架,功能强大。下面我们开始来一步一步学习这个框架。

一、去官方网站下载Hibernate最新版本(http://www.hibernate.org/downloads.html

二、解压下载的文件

三、在项目中加入必要的JAR包,最少需要以下所列jar包。

    1. 根目录下的 hibernate3.jar

    2. lib/required目录下的所有jar包

    3. lib/jpa目录下的所有jar包

    4. Hibernate中缺少的slf4j-nop-1.6.1.jar(http://www.slf4j.org 需注意版本应和Hibernate中版本相同)

    5. 当前使用的数据库的驱动

四、建立应用所需要的表(我是在名为hibernage的数据库中建立了一个book表,结构如下所示)

五、编写POJO及其隐射文件xxx.hbm.xml

    Book.java

package ch01.pojo;
import java.util.Date;

public class Book {
	private Integer bookId;
	private String bookName;
	private Date publishDate;
	private Double bookPrice;

	public Integer getBookId() {
		return bookId;
	}

	public void setBookId(Integer bookId) {
		this.bookId = bookId;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public Double getBookPrice() {
		return bookPrice;
	}

	public void setBookPrice(Double bookPrice) {
		this.bookPrice = bookPrice;
	}
}

   Book.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="ch01.pojo.Book" table="book" catalog="hibernate">
		<id name="bookId" type="java.lang.Integer">
			<column name="BookId">
				<comment>图书编号</comment>
			</column>
			<generator class="identity"/>
		</id>
		<property name="bookName" type="java.lang.String">
			<column name="BookName">
				<comment>书名</comment>
			</column>
		</property>
		<property name="bookPrice" type="java.lang.Double">
			<column name="BookPrice">
				<comment>书价</comment>
			</column>
		</property>
		<property name="publishDate" type="java.util.Date">
			<column name="PublishDate">
				<comment>出版日期</comment>
			</column>
		</property>
	</class>
</hibernate-mapping>

六、编写Hibernate配置文件hibernate.cfg.xml

    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">
<hibernate-configuration>
  <session-factory>
  	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  	<property name="hibernate.connection.username">root</property>
  	<property name="hibernate.connection.password">root</property>
  	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  	<property name="show_sql">true</property>
  	
  	<mapping resource="ch01/pojo/Book.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

七、编写第一个测试文件

    Test.java

package ch01.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import ch01.pojo.Book;

public class Test {
	public static void main(String[] args) {
		Book book = new Book();
		book.setBookName("精通Hibernate");
		book.setBookPrice(50.04);
		book.setPublishDate(new Date());
		
		SessionFactory factory = new Configuration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction tx = session.beginTransaction();
		session.save(book);
		tx.commit();
		session.close();
	}
}

八、检查程序并运行.

猜你喜欢

转载自tarring.iteye.com/blog/1073506
今日推荐