Hibernate基本开发步骤

本文详细介绍了如何使用Hibernate实现实体类与数据库表的映射,并通过配置文件和测试类展示了如何进行数据的增删查改操作。

1.新建一个Web Project ,导入相应jar文件

 

2.编写Util类

 1)CreateTable.java

 

package com.mingmei.util;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CreateTable {
	public static void main(String[] args) {
		Configuration configuration = new Configuration().configure();
		SchemaExport schemaExport = new SchemaExport(configuration);
		schemaExport.create(true, true);
	}
}

2)HibernateSessionFactory.java

package com.mingmei.util;

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

public class HibernateSessionFactory {
	// 1. 读取hibernate.cfg.xml文件
	static Configuration configuration = new Configuration().configure();

	public static Session getSession() {
		// 2. 创建SessionFactory对象
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.获取Session对象
		return sessionFactory.openSession();
	}

	public static void closeSession(Session session) {
		if (session.isOpen()) {
			session.close();
		}
	}
}

4.编写实体类

package com.tarena.pojo;

import java.io.Serializable;

public class User implements Serializable {
	private int id;
	private String name;
	private String pwd;
	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 String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}


 

5. 配置User.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 package="com.tarena.pojo">
	<class name="User" table="d_user">
		<id name="id" type="integer">
			<generator class="native"></generator>
		</id>
		<property name="name" ></property>
		<property name="pwd"></property>
		<property name="age"></property>
	</class>
</hibernate-mapping>


 

6. 配置核心配置文件hibernate.cfg.xml

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


7.编写测试类TestHibernate.java

package com.tarena.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.tarena.pojo.User;
import com.tarena.util.HibernateSessionFactory;

public class TestHibernate {
	@Test
	public void testSave() {
		Session session = HibernateSessionFactory.getSession();
		Transaction transaction = session.beginTransaction();
		transaction.begin();
		try {
			User user = new User();
			user.setAge(23);
			user.setName("maxiaoming");
			user.setPwd("1234");
			session.save(user);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
		} finally {
			HibernateSessionFactory.closeSession(session);
		}

	}

	@Test
	public void testLoad() {
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			User user = (User) session.load(User.class, 2); // ObjectNotFoundException
			System.out.println(user.getId() + "," + user.getName() + ","
					+ user.getPwd() + "," + user.getAge());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession(session);
		}

	}

	@Test
	public void testGet() {
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			User user = (User) session.get(User.class, 1);
			System.out.println(user);
			// System.out.println(user.getId() + "," + user.getName() + ","
			// + user.getPwd() + "," + user.getAge());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession(session);
		}

	}

	@Test
	public void testUpdate() {
		// 执行修改之前须调用load/get方法
		Session session = null;
		try {
			session = HibernateSessionFactory.getSession();
			session.beginTransaction().begin();
			User user = (User) session.load(User.class, 1);

			user.setName("maxianhai");
			user.setPwd("test");
			session.update(user);
			session.beginTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			session.beginTransaction().rollback();
		} finally {
			HibernateSessionFactory.closeSession(session);
		}
	}

	@Test
	public void testDelete() {
		Session session = null;
		try {

			session = HibernateSessionFactory.getSession();
			session.beginTransaction().begin();
			User user = new User();
			user.setId(2);
			session.delete(user);
			session.beginTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.beginTransaction().rollback();
		} finally {
			HibernateSessionFactory.closeSession(session);
		}

	}
}


 

 

 


 



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值