⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basedao.java

📁 一个简单的人员管理。运用了ajax和struts、hibernate框架开发。
💻 JAVA
字号:
package com.liu.ems.hibernate;

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

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


public class BaseDAO {
   
	/**
	 * 保存指定实体对象的数据
	 * @param obj
	 * @throws Exception
	 */
	public void insert(Object obj)throws Exception {
		Session ses=null;
		Transaction tr=null;
		try {
			ses=HibernateSessionFactory.getSession();
			tr=ses.beginTransaction();
			ses.save(obj);
			tr.commit();
		} catch (Exception e) {
			throw e;
		}finally{
			HibernateSessionFactory.closeSession();
		}
		
	} 
	
	/**
	 * 删除指定的实体对象
	 * @param obj
	 * @throws Exception
	 */
	public void delete(Object obj) throws Exception{
		Session ses=null;
		Transaction tr=null;
		try {
			ses=HibernateSessionFactory.getSession();
			tr=ses.beginTransaction();
			ses.delete(obj);
			tr.commit();
		} catch (Exception e) {
			throw e;
		}finally{
			HibernateSessionFactory.closeSession();
		}
		
		
	} 
	
	/**
	 * 更新指定的实体对象
	 * @param obj
	 * @throws Exception
	 */
	public void update(Object obj) throws Exception
	{
		Session ses=null;
		Transaction tr=null;
		try {
			ses=HibernateSessionFactory.getSession();
			tr=ses.beginTransaction();
			ses.update(obj);
			tr.commit();
		} catch (Exception e) {
			throw e;
		}finally{
			HibernateSessionFactory.closeSession();
		}
	} 
	
	/**
	 * 查询,根据hql语句查询数据
	 * @param hql
	 * @return 查询结果
	 * @throws Exception
	 */
	public List findAllHql(String hql) throws Exception
	{
		Session ses=null;
		List list=null;
		try {
			ses=HibernateSessionFactory.getSession();
			//String hql="from employee";
			Query query=ses.createQuery(hql);
			list=query.list();
		} catch (Exception e) {
			throw e;
		}
		finally{
			HibernateSessionFactory.closeSession();
		}
		return list;
 	}
	
	
	/**
	 * 根据书体类名和主健,查找指定的实体对象
	 * @param cs 实体 类名
	 * @param id 主健值
	 * @return 实体对象obj
	 * @throws Exception
	 */
	public  Object finById(Class cs,Serializable id) throws Exception{
		Object obj=null;
		Session ses=null;
		try {
			ses=HibernateSessionFactory.getSession();
			//String hql="From employee where empid=id";
			obj=ses.load(cs, id);
		} catch (Exception e) {
           throw e;			
		}
		finally{
			HibernateSessionFactory.closeSession();
		}
		return obj;
		
		
	} 
	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -