J2EE系列之Hibernate4学习笔记(二)--Hibernate4增删改查体验

上一篇博客中使用hibernate4成功创建了表t_student,并向这个表中添加了一个数据。现在来体验hibernate4实现数据库表的增删改查操作。

一、HibernateUtil封装

使用hibernate对数据库操作的时候都要首先获得一个SessionFactory,这里把获得SessionFactory的代码进行封装。新建一个包com.test.util,在里面新建一个HibernateUtil类:

package com.test.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {
	
	private static final SessionFactory sessionFactory = buildSessionFactory();

	private static SessionFactory buildSessionFactory(){
		Configuration configuration=new Configuration().configure(); // 实例化配置文件
		ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); // 实例化服务登记
	    return configuration.buildSessionFactory(serviceRegistry); // 获取Session工厂
	}
	

	public static SessionFactory getSessionFactory() {
		// TODO Auto-generated method stub
		return sessionFactory;
	}
}

调用HibernateUtil.getSessionFactory既可以获得SessionFactory对象了。

二、XML版增删改查实现

1.上一篇博客中实现了往表t_student中添加数据的代码,现在把这个代码进行一下修改,修改如下:

package com.test.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test.model.Student;
import com.test.util.HibernateUtil;

public class StudentTest {
	
	private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); // 获取Session工厂
	private void add(){
		
	    Session session=sessionFactory.openSession(); // 生成一个session
	    session.beginTransaction(); // 开启事务
	    
	    Student s=new Student();
	    s.setName("张三");
	    session.save(s);
	    
	    session.getTransaction().commit(); // 提交事务
	    session.close(); // 关闭session
	}
	
	
	
	
	public static void main(String[] args) {
		
	    StudentTest studentTest = new StudentTest();
	    studentTest.add();
	    
	}
}
这里把添加操作写成了函数add,运行程序,可以实现向表t_student中添加数据。

2.删除功能代码如下:

private void delete(){
		
		Session session=sessionFactory.openSession(); // 生成一个session
	    session.beginTransaction(); // 开启事务
	    
	    Student student = (Student)session.get(Student.class, Long.valueOf(1));
	    session.delete(student);
	    session.getTransaction().commit(); // 提交事务
	    session.close(); // 关闭session
	}

首先通过session.get函数获取到Student这个类生成的表中主键为1的对象,然后调用session.delete方法删除该对象。

3.修改功能代码如下:

private void update(){
		Session session=sessionFactory.openSession(); // 生成一个session
	    session.beginTransaction(); // 开启事务
	    
	    Student student = (Student)session.get(Student.class, Long.valueOf(2));
	    student.setName("张三2");
	    session.save(student);
	    session.getTransaction().commit(); // 提交事务
	    session.close(); // 关闭session
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值