SSH入门Hibernate篇(二)——对数据库基本的增删改查

本文详细介绍了CustomerDao类的实现,包括使用Hibernate进行客户数据的增删改查操作,通过HQL语句查找特定数据,以及如何利用saveOrUpdate方法自动判断并执行保存或更新操作。

CustomerDao.java

package cn.edu.zjut.dao;

import java.util.List;

import org.hibernate.Query;

import cn.edu.zjut.po.Customer;

public class CustomerDao extends BaseHibernateDao{

	public CustomerDao() {
		// TODO Auto-generated constructor stub
	}
	
	//根据hql语句查找
	public List findByHql(String hql) { 
		
		try {
			String queryString = hql;
			Query queryObject = getSession().createQuery(queryString); 
			return queryObject.list();
		}catch (RuntimeException re) {
			//log.error("find by hql failed", re); 
			throw re;
		}
	}

	//查找全部,当然其实这个方法是不需要的,因为只要有了用hql语句查找,在service方法中就可以让hql语句为"from 实体类名"
	public List findAll() {
		
		try {
			String queryString = "from Customer";//这里是from 实体类名 而不是数据表名,这句话代表查询全部
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();		
		}catch(RuntimeException re){
			throw re;
		}finally {
			session.close();
		}
	}

	
	//增加一条记录
	public void save(Customer customer) {
		
		try {
			getSession().save(customer);
		}catch(RuntimeException re) {
			throw re;
		}
	}
	
	//更新一条记录
	public void update(Customer customer) {
		try {
			getSession().update(customer);
			getSession().flush();
		}catch(RuntimeException re) {
			throw re;
		}finally {
			getSession().close();
		}
	}
	
	//删除一条记录
	public void delete(Customer customer) {
		try {
			getSession().delete(customer);
			getSession().flush();
		}catch(RuntimeException re) {
			throw re;
		}finally {
			getSession().close();
		}
	}

}

这里不再需要在每个方法中加入:

Configuration cfg= new Configuration() .configure();
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(sr);
Session session = sf.openSession();

因为它继承了BaseHibernateDao,可以直接使用getSession()来获取session,而在BaseHibernateDao的getSession方法中又是用来HibernateUtil。这两个.java文件详见 我的另一篇博客
注意事务的使用

解释一下下面的saveOrUpdate方法,是在中间表的时候让它自己判断是要save还是update

package com.searchSub.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.searchSub.po.Customer;


public class CustomerDAO extends BaseHibernateDao implements ICustomerDAO {

	public void save(Customer transientInstance) {
		System.out.println("execute --save()-- method.");
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			session.save(transientInstance);
			tran.commit();
		} catch (RuntimeException re) {
			if (tran != null)
				tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}

	

	public void delete(Customer transientInstance) {
		System.out.println("execute --delete()-- method.");
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			session.delete(transientInstance);
			tran.commit();
		} catch (RuntimeException re) {
			if (tran != null)
				tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}

	public void update(Customer transientInstance) {
		System.out.println("execute --update()-- method.");
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			session.merge(transientInstance);
			tran.commit();
		} catch (RuntimeException re) {
			if (tran != null)
				tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}

	public List findByHQL(String hql) {
		System.out.println("execute --findByHQL()-- method.");
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			String queryString = hql;
			Query queryObject = getSession().createQuery(queryString);
			tran.commit();
			return queryObject.list();
		} catch (RuntimeException re) {
			if (tran != null)
				tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}
	
	public Customer findById(int id) {
		System.out.println("execute --findById()-- method.");
		Customer cust = null;
		Transaction tran = null;
		Session session = null;
		try {
			session=getSession();
			tran = session.beginTransaction();
			cust = (Customer) session.get(Customer.class, id);
			tran.commit();
		}catch (RuntimeException re) {
			if (tran != null)
				tran.rollback();
			throw re;
		}finally {
			session.close();
		}
		return cust;
	}

	public void saveOrUpdate(Customer transientInstance) {
		System.out.println("execute --save()-- method.");
		Transaction tran = null;
		Session session = null;
		try {
			session = getSession();
			tran = session.beginTransaction();
			session.saveOrUpdate(transientInstance);
			tran.commit();
		} catch (RuntimeException re) {
			if (tran != null)
				tran.rollback();
			throw re;
		} finally {
			session.close();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值