一个Hibernate的Hello World, 基于Hibernate 4.0

先在官网上下载最近的Hibernate完整包, 目前最新的是hibernate-release-4.0.0.Final http://www.hibernate.org/downloads .

 

打开Myeclipse, 新建一个JavaProject. 

1.导入Hibernate需要的Jar包.

  需要加入的包有:hibernate-release-4.0.0.Final里面Lib目录下Required的所有Jar包.

2.加入Hibernate配置文件hibernate.cfg.xml, 文件内容可参考文档(这里使用的是么Mysql):

<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/hibernate
</property>
<property name="connection.username">用户名</property>
<property name="connection.password">密码</property>

<!-- JDBC connection pool (use the built-in)
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>

<!-- Enable Hibernate's automatic session context management
<property name="current_session_context_class">thread</property>
-->
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.internal.NoCacheProvider
</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup
<property name="hbm2ddl.auto">update</property>
-->
<property name="myeclipse.connection.profile">
Hibernate_Test
</property>
<mapping resource="jyu/hibernate/model/Student.hbm.xml" />
<mapping class="jyu.hibernate.model.Teacher" />
</session-factory>
</hibernate-configuration>

3.新建一个实体类Student, 并成长get, set方法:

package jyu.hibernate.model;

public class Student {
private int id;
private String name;
private int age;


public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}


}


4.在数据库中建好对应的表Student, 有id, name ,age三个字段.

5.编写实体与关系的映射文件:Student.hbm.xml

<?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>
<class name="jyu.hibernate.model.Student">
<id name="id" column="id"/>
<property name="name" />
<property name="age" />
</class>

</hibernate-mapping>

6.在hibernate.cfg.xml配置文件中加入Student.hbm.xml:在 </session-factory> 之前加入:<mapping resource="jyu/hibernate/model/Student.hbm.xml" />

7.测试:StudentTest

 

package jyu.hibernate.model;

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

public class StudentTest {

public static void main(String[] agrs){
Student st = new Student();
st.setId(2);
st.setName("学生");
st.setAge(20);

Configuration cfg = new Configuration(); //读取配置hibernate文件
SessionFactory sf = cfg.configure().buildSessionFactory(); //根据配置文件生成事务工厂
Session session = sf.openSession();
session.beginTransaction(); //事务开启
session.save(st); //保存对象
session.getTransaction().commit();
session.close();
sf.close();
}
}

运行, 可以查看到数据库中已经插入了一条记录.


这种方法是比较常用的使用XML来映射实体与关系的方法, 另有使用Annotation(注解)方式也能完成映射功能.

 

本文为尚学堂马士兵Hibernate视频教程笔记.




  

posted @ 2012-01-18 18:39  天如er  阅读(3626)  评论(0编辑  收藏  举报