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

📄 petownerdaoimpl.java

📁 用Struts+hibernate写的宠物医院
💻 JAVA
字号:
package com.sean.pet.hibernate.dao.impl;

import java.util.ArrayList;
import java.util.List;

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

import com.sean.pet.hibernate.HibernateSessionFactory;
import com.sean.pet.hibernate.dao.PetownerDao;
import com.sean.pet.hibernate.po.Petowner;

public class PetownerDaoImpl implements PetownerDao {
	
	Session session = null;
	
	public void delete(String id) {
		// TODO Auto-generated method stub
		Transaction tr = null;
		try {
			session = HibernateSessionFactory.getSession();
			tr = session.beginTransaction();
			Petowner po = (Petowner) session.load(Petowner.class, id);
			session.delete(po);
			tr.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			
		}
		HibernateSessionFactory.closeSession();
	}

	@SuppressWarnings("unchecked")
	public List<Petowner> findAll() {
		// TODO Auto-generated method stub
		Transaction tr = null;
		List<Petowner> list = new ArrayList<Petowner>();
		try {
			session = HibernateSessionFactory.getSession();
			tr = session.beginTransaction();
			String hql = "from Petowner";
			Query q = session.createQuery(hql);
			list = q.list();
			tr.commit();
			return list;
		} catch (HibernateException e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			
		}
		HibernateSessionFactory.closeSession();
		return null;
	}

	public Petowner findByID(String id) {
		// TODO Auto-generated method stub
		Petowner po = null;
		Transaction tr = null;
		try {
			session = HibernateSessionFactory.getSession();
			tr = session.beginTransaction();
			String hql = "from Petowner where id=?";
			Query q = session.createQuery(hql);
			q.setString(0, id);
			po = (Petowner)q.uniqueResult();
			tr.commit();
			return po;
		} catch (HibernateException e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			
		}
		HibernateSessionFactory.closeSession();
		return null;
	}

	public void save(Petowner po) {
		// TODO Auto-generated method stub
		Transaction tr = null;
		try {
			session = HibernateSessionFactory.getSession();
			tr = session.beginTransaction();
			session.save(po);
			tr.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			
		}
		HibernateSessionFactory.closeSession();
	}

	public void update(Petowner po) {
		// TODO Auto-generated method stub
		Transaction tr = null;
		try {
			session = HibernateSessionFactory.getSession();
			tr = session.beginTransaction();
			session.merge(po);
			tr.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		
	}

}

⌨️ 快捷键说明

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