hibernate快速入门案例

本文详细介绍了一个基于Hibernate框架的开发流程,包括项目的创建、数据库表的设计、Domain对象与映射文件的构建、配置文件的设置及基本的CRUD操作示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开发流程

1.创建一个项目

2.画出简单的项目框架图

3.引入hibernate开发包(http://www.hibernate.org)


hibernate开发方式的三种方式:

1由Domain object->mapping->db(官方推荐)

2由DB开始,用工具生成mapping和Domain object(使用较多)

3由映射文件开始


4.创建employe表

create table employee(
id int primary key,

name varchar(40) not null,

email varchar(40) not null,

hiredate date not null

)

5.开发domain对象和对象映射文件

在类路径src下新建包cn.java.domain,创建类Employee

package cn.java.domain;

public class Employee implements java.io.Serializable{
	
	/**该domain对象按照规范序列化,目的是可以唯一的标识该对象,同时在网络和文件传输
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private String email;
	private java.util.Date hiredate;
	
	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 getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public java.util.Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(java.util.Date hiredate) {
		this.hiredate = hiredate;
	}
	
}


对象关系映射文件作用:作用是指定domain对象和表的映射关系,文件取名有规范:domain对象.hbm.xml。一般放在和domain对象同一文件下。

<?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="cn.java.domain">
	<class name="Employee" table="employee">
		<!-- id元素用于指定主键属性 -->
		<id name="id" column="id" type="java.lang.Integer">
			<!-- 该元素用于指定主键生成策略 -->
			<generator class="increment"/>
		</id>
		
		<!-- 对其他属性还有配置 -->
		<property name="name" type="java.lang.String">
			<column name="name" not-null="false"></column>
		</property>
		<property  name="email" type="java.lang.String">
			<column name="email" not-null="false"/>
		</property>
		<property name="hiredate" type="java.util.Date">
			<column name="hiredate" not-null="false"/>
		</property>
	</class>
</hibernate-mapping>

6.手动配置我们的hibernate.cfg.xml文件,该文件用于配置链接的数据库的类型、driver、用户名、密码等。

<?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="connection.driver_class">com.mysql.jdbc.Driver</property>	
		<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hbm2ddl.auto">update</property>
		<property name="show_sql">true</property>
		<mapping resource="cn/java/domain/Employee.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
7.手动测试

package cn.java.junit;

import static org.junit.Assert.*;

import java.util.Date;

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

import cn.java.domain.Employee;
import cn.java.utils.MySessionFactory;

public class Test {

	@org.junit.Test
	public void test() {
		//1.创建Configuration,该对像用于读取hibernate.cfg.xml并完成初始化
		Configuration config =new Configuration().configure();
		//2.创建SessionFactory,这是一个会话工厂,是一个重量级的对象
		SessionFactory sf =config.buildSessionFactory();
		//3.创建Session相当于jdbc connnection
		Session sess =sf.openSession();
		//4.对hibernate而言。要求程序员,在进行增加,删除,修改的时候要使用事务
		Transaction tx = sess.beginTransaction();
		//添加雇员
		Employee e=new Employee();
		e.setName("lb");
		e.setEmail("lb@163.com");
		e.setHiredate(new Date());
		sess.save(e); //==>insert into ...被hibernate封装
		tx.commit();
		sess.close();
		sf.close();
	}
	
	@org.junit.Test
	public void update(){
<span style="white-space:pre">			</span>//修改用户
			Session session = MySessionFactory.getSessionFactory().openSession();
			Transaction ts =session.beginTransaction();
			//load方法通过主键属性获取实例对象
<span style="white-space:pre">			</span>Employee e = session.load(Employee.class, 1);
			e.setName("xxx");
			ts.commit();
			session.close();
	}
	
	@org.junit.Test
	public void delete(){
		Session session = MySessionFactory.getSessionFactory().openSession();
		Transaction ts =session.beginTransaction();
		Employee e = session.load(Employee.class, 1);
		session.delete(e);
		ts.commit();
		session.close();
		
	}
}

由于SessionFactory比较耗内存,我们可以把SessionFactory做成单例,创建一个工具类

package cn.java.utils;

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

public class MySessionFactory {
	private static SessionFactory sf=null;    
	private MySessionFactory(){
		
	}
	public static SessionFactory getSessionFactory(){
		if(sf==null){
			sf = new Configuration().configure().buildSessionFactory();
		}
		return sf;
	}
}

注:
<span style="white-space:pre">			</span>Session session = MySessionFactory.getSessionFactory().openSession();
			Transaction ts =session.beginTransaction();
			Employee e = session.load(Employee.class, 1);
			e.setName("xxx");-->select * from employee where id =3;底层还是调用了sql语句
			ts.commit();
			session.close();






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值