一个简单的利用hibernate实现增删改查的例子

下面这个例子非常简单,只是用于实现一个对oracle数据库中student表的一个简单的增删改查,其中查询和删除都是根据主键id来查询。

目录结构

图1:目录结构

hibernate.cfg.xml中的代码如下:

<?xml version='1.0' encoding='utf-8'?>
<!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.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
		<property name="connection.username">yedward</property>
		<property name="connection.password">yedward</property>
		<property name="dialect">org.hibernate.dialect.OracleDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<mapping resource="net/yedward/db/mapping/Student.hbm.xml" />
	</session-factory>
</hibernate-configuration>

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 package="net.yedward.db.domain">
    <class name="Student" table="student">
        <id name="id" column="id"></id>
        <property name="name" column="name"></property>
        <property name="age" column="age"></property>
    </class>
</hibernate-mapping>

Student.java中的代码如下:

package net.yedward.db.domain;

import java.io.Serializable;

public class Student implements Serializable {
	private static final long serialVersionUID = 5631142352656445262L;
	private int id; // 注意在表中id是主键
	private String name;
	private int age;

	public int getAge() {
		return age;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

}

StudentDAO.java中的代码如下:

package net.yedward.db.dao;

import net.yedward.db.domain.Student;

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

public class StudentDao {
	// main方法,用于测试
	public static void main(String[] args) {
		delete(3);
	}
	
	// 增加
	public static void add(Student student) {
		Configuration cfg = new Configuration();
		SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(student);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}

	// 查询,id是主键
	public static Student findById(int id) {
		Configuration cfg = new Configuration();
		SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂
		Session session = sf.openSession();
		// 查询不需要事务transaction
		Student student = (Student) session.get(Student.class, id);
		session.close();
		sf.close();
		return student;
	}

	// 修改
	public static void update(Student student) {
		Configuration cfg = new Configuration();
		SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂
		Session session = sf.openSession();
		session.beginTransaction();
		session.update(student);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
	
	// 删除
	public static void delete(int id) {
		Configuration cfg = new Configuration();
		SessionFactory sf = cfg.configure().buildSessionFactory(); // 加载配置文件去构建Session工厂
		Session session = sf.openSession();
		session.beginTransaction();
		Student student = new Student();
		student.setId(id);
		session.delete(student);
		session.getTransaction().commit();
		session.close();
		sf.close();
	}
}

上面的这些代码中,StudentDAO.java中的获得session的代码可以直接封装成一个工具类来调用,这里没有这样做了,以及hibernate的映射也可以用Annotation来写,这里也没这样做了,以后补充。

本文标题:一个简单的利用hibernate实现增删改查的例子

本文链接:http://yedward.net/?id=244

本文版权归作者所有,欢迎转载,转载请以文字链接的形式注明文章出处。

相关文章