Hibernate3 初体验

 版本:hibernate-distribution-3.6.0.Final

1. 下载相关jar包

    1.1 Hibernate3框架包

        1.1.1 下载地址:http://sourceforge.net/projects/hibernate/files/

    1.2 Hibernate3依赖包

        1.2.1 slf4j日志包 :slf4j-nop-1.6.1.jar

                  地址:http://www.slf4j.org/

    1.3 数据库驱动包

        1.3.1 mysql驱动:mysql-connector-java-5.1.30-bin.jar

                 地址:http://dev.mysql.com/downloads/connector/j/

2. Demo工程配置

    2.1 新建工程 org.hibernate3.demo

    2.2 添加依赖包到工程,依赖包如下:

  • hibernate3.jar 
  • antlr-2.7.6.jar
  • commons-collections-3.1.jar
  • dom4j-1.6.1.jar
  • javassist-3.12.0.GA.jar
  • jta-1.1.jar
  • slf4j-api-1.6.1.jar
  • hibernate-jpa-2.0-api-1.0.0.Final.jar
  • mysql-connector-java-5.1.30-bin.jar
  • slf4j-nop-1.6.1.jar

说明:hibernate3.jar在框架包根目录;antlr-2.7.6.jar,commons-collections-3.1.jar,dom4j-1.6.1.jar,

           javassist-3.12.0.GA.jar,jta-1.1.jar,slf4j-api-1.6.1.jar在框架包目录lib\required下;

           hibernate-jpa-2.0-api-1.0.0.Final.jar在框架包lib\jpa下;mysql-connector-java-5.1.30-bin.jar

           为MySQL驱动包;slf4j-nop-1.6.1.jar在依赖包slf4j-1.6.1根目录。

    2.3 在工程下新建包和模型类org.hibernate3.demo.Demo

            

package org.hibernate3.demo;

public class Demo {

	private int demoId;

	private String demoName;

	private String demoDescription;

	public int getDemoId() {
		return demoId;
	}

	public void setDemoId(int demoId) {
		this.demoId = demoId;
	}

	public String getDemoName() {
		return demoName;
	}

	public void setDemoName(String demoName) {
		this.demoName = demoName;
	}

	public String getDemoDescription() {
		return demoDescription;
	}

	public void setDemoDescription(String demoDescription) {
		this.demoDescription = demoDescription;
	}
}

    2.4 在模型类同路径添加配置文件 Demo.hbm.xml

           

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate3.demo">
	<class name="Demo" table="t_demo">
		<id name="demoId" column="demo_id">
			<generator class="identity"/>
		</id>
		<property name="demoName" column="demo_name" type="java.lang.String" not-null="true"/>
		<property name="demoDescription" column="demo_description" type="java.lang.String"/>
	</class>
</hibernate-mapping>

    2.5 在类路径下添加Hibernate主配置文件 hibernate.cfg.xml

           

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost/demo</property>

		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>

        <mapping resource="org//hibernate3/demo/Demo.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

    2.6 添加测试类 org.hibernate3.demo.DemoTest

           

package org.hibernate3.demo;

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

public class DemoTest {
	public static void main(String[] args) {
		Configuration cfg = new Configuration().configure();
		SessionFactory sessionFactory = cfg.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		transaction.begin();

		Demo demo = new Demo();
		demo.setDemoName("FirstDemo");
		demo.setDemoDescription("This is the first demo of  hibernate3.");
		session.save(demo);

		transaction.commit();
		session.close();
		sessionFactory.close();
	}
}

    2.7 运行测试并查看数据库

           
   
 

    2.8 本工程文件结构

           
   
 

猜你喜欢

转载自ihuning.iteye.com/blog/2189192
今日推荐