Hibernate 开发步骤


本文介绍Hibernate的第一个HelloWorld入门程序。

需求:完成客户的添加功能


一、下载并导入Hibernate支持jar包

到hibernate官网下载hibernate-release-5.0.7.Final的资源包,地址为:

http://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/hibernate-release-5.0.7.Final.zip/download

如果上面链接无法下载,可以使用下面链接下载:

  • 解压目录,目录结构如下:

  • 建立Java项目

找到Hibernate根目录下的lib/required,导入里面的所有jar包到项目中:

二、在mysql数据库创建表

-- 建立数据库
create database hibernate;

-- 客户表
CREATE TABLE t_customer(
	c_id INT PRIMARY KEY AUTO_INCREMENT,
	c_name VARCHAR(20),
	c_gender CHAR(1),
	c_age INT,
	c_level VARCHAR(20)
);

三、建立客户实体类

/**
 * 客户实体类
 * @author http://www.yiidian.com
 */
public class Customer implements Serializable{

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String gender;
	private Integer age;
	private String level;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getLevel() {
		return level;
	}
	public void setLevel(String level) {
		this.level = level;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", gender=" + gender
				+ ", age=" + age + ", level=" + level + "]";
	}
	
}

四、编写*.hbm.xml对象映射文件(重点)

文件要求:
1)文件名称: 实体类名称.hbm.xml
2)文件存放的位置:和实体类存放到同一个目录下

<?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">

<!-- 
	ORM映射配置  - http://www.yiidian.com
 -->   
<hibernate-mapping package="com.yiidian.domain">
 	<!-- 
 		name:类名
 		table:表名
 	 -->
 	<class name="Customer" table="t_customer">
 		<!-- 主键 -->
 		<id name="id" column="c_id">
 			<generator class="native"></generator>
 		</id>
 		<!-- 其他属性 -->
 		<property name="name" column="c_name"></property>
 		<property name="gender" column="c_gender"></property>
 		<property name="age" column="c_age"></property>
 		<property name="level" column="c_level"></property>
 	</class>		
 
 
</hibernate-mapping>   

五、编写hibernate.cfg.xml文件

注意:该文件建议放在项目的src目录下

<?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>
		<!-- 1.连接数据库参数 -->
		<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>
		
		<!-- hibernate方言 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<!-- 2.hibernate扩展参数 -->
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<!-- *.hbm.xml文件 -->
		<mapping resource="com/yiidian/domain/Customer.hbm.xml"/>
	</session-factory>
</hibernate-configuration>	

六、编写测试代码

/**
 * hibernate的HelloWorld测试类
 * @author http://www.yiidian.com
 *
 */
public class Demo {

	@Test
	public void test1(){
		Customer customer = new Customer();
		customer.setName("老王");
		customer.setAge(40);
		customer.setGender("男");
		customer.setLevel("VIP客户");
		
		//1.读取hibernate.cfg.xml文件
		Configuration cfg = new Configuration();
		cfg.configure(); 
		
		//2.创建SessionFactory工厂
		SessionFactory factory = cfg.buildSessionFactory();
		
		//3.创建Session对象
		Session session = factory.openSession();
		
		//4.开启事务
		Transaction tx = session.beginTransaction();
		
		//5.执行添加操作
		session.save(customer);
		
		//6.提交事务
		tx.commit();
		
		//7.关闭资源
		session.close();
	}
}

七、运行,查看结果

右键Demo测试类,点击Run As - Junit Test:

控制台显示:

Hibernate: 
    insert 
    into
        t_customer
        (c_name, c_gender, c_age, c_level) 
    values
        (?, ?, ?, ?)

查看数据库记录,多了一条客户数据:

到此,Hibernate的HelloWorld程序执行成功!

 

源码下载:http://pan.baidu.com/s/1geWklk3