Hibernate对数据库增删查改的封装

1.创建session的封装

package cn.itcast.utils;

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

public class HibernateUtils {
	private static SessionFactory sf;
	static{
		//加载主配置文件,并创建Session的工厂
		sf=new Configuration().configure().buildSessionFactory();
	}
	//创建Session对象
	public static Session getSession(){
		return sf.openSession();
	}

}

2.增删查改的封装

package cn.itcast.b_crud;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import cn.itcast.a_hello.Employee;
import cn.itcast.utils.HibernateUtils;

public class EmployeeDaoImpl implements IEmployeeDao{

	@Override
	//保存数据
	public void save(Employee emp) {
		Session session=null;
		Transaction tx=null;
		try{
			//创建Session对象
		   session=(Session) HibernateUtils.getSession();
		   //开启事物
		   tx=session.beginTransaction();
		   //保存数据
		   session.save(emp);
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			//提交事物
			tx.commit();
			//关闭session
			session.close();
		}
		
	}

	@Override
	//更新数据
	public void update(Employee emp) {
		Session session=null;
		Transaction tx=null;
		try{
		   session=(Session) HibernateUtils.getSession();
		   tx=session.beginTransaction();
		   session.update(emp);
		  
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			tx.commit();
			session.close();
		}
		
	}
    //根据员工id查询
	@Override
	public Employee findById(Serializable id) {
		Session session=null;
		Transaction tx=null;
		try{
		   session=(Session) HibernateUtils.getSession();
		   tx=session.beginTransaction();
		   return (Employee) session.get(Employee.class,id);
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			tx.commit();
			session.close();
		}
	}

	@Override
	//查询全部
	public List<Employee> getAll() {
		Session session=null;
		Transaction tx=null;
		try{
		   session=(Session) HibernateUtils.getSession();
		   tx=session.beginTransaction();
		   
		   //HQL查询
		   Query q=session.createQuery("from Employee");
		   return q.list();
		  
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			tx.commit();
			session.close();
		}
		
	}

	@Override
	//根据姓名查询
	public List<Employee> getAll(String employeeName) {
		Session session=null;
		Transaction tx=null;
		try{
		   session=(Session) HibernateUtils.getSession();
		   tx=session.beginTransaction();
		   Query q=session.createQuery("from Employee where empName=?");
		   //注意,参数从0开始
		   q.setParameter(0, employeeName);
		   //执行查询
		   return q.list();
		  
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			tx.commit();
			session.close();
		}
	
	}

	@Override
	//分页查询
	public List<Employee> getAll(int index, int count) {
		Session session=null;
		Transaction tx=null;
		try{
		   session=(Session) HibernateUtils.getSession();
		   tx=session.beginTransaction();
		   Query q=session.createQuery("from Employee");
		   //设置分页参数
		   q.setFirstResult(index);//查询的起始行
		   q.setMaxResults(count);//查询返回的行数
		   List<Employee> list=q.list();
		   
		   return list;
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			tx.commit();
			session.close();
		}
	
	}

	@Override
	//根据id删除用户
	public void delete(Serializable id) {
		Session session=null;
		Transaction tx=null;
		try{
			session=(Session) HibernateUtils.getSession();
			tx=session.beginTransaction();
			Object obj=session.get(Employee.class, id);
			if(obj!=null){
			   session.delete(obj);
			}
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally{
			tx.commit();
			session.close();
		}
		
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值