userdao.java

来自「《JSP网站开发典型模块与实例精讲》一书光盘源码」· Java 代码 · 共 148 行

JAVA
148
字号
package book.example.photoprint.dao;

import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import book.example.photoprint.exception.DBException;
import book.example.photoprint.po.User;
import book.example.photoprint.util.HibernateSessionFactory;

public class UserDAO implements IUserDAO  {

	public void addUser(User user) throws DBException {
		Session session;
		Transaction tx = null;
		try {
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			session.saveOrUpdate(user);
			tx.commit();
		} catch (HibernateException e) {

			try {
				tx.rollback();

			} catch (HibernateException e1) {
				e1.printStackTrace();

			}
			e.printStackTrace();
			throw new DBException("保存对象失败!");
		} finally {
			try {
				HibernateSessionFactory.closeSession();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
		}
	}

	public void updateUser(User user) throws DBException {
		Session session;
		Transaction tx = null;
		try {
			session = HibernateSessionFactory.currentSession();
			tx = session.beginTransaction();
			session.update(user);
			tx.commit();
		} catch (HibernateException e) {

			try {
				tx.rollback();

			} catch (HibernateException e1) {
				e1.printStackTrace();

			}
			e.printStackTrace();
			throw new DBException("更新对象失败!");
		} finally {
			try {
				HibernateSessionFactory.closeSession();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
		}
	}

	public void deleteUser(String userid) throws HibernateException {
		Session session = HibernateSessionFactory.currentSession();
		Transaction tx = session.beginTransaction();
		User user = (User) session.load(User.class, userid);
		session.delete(user);
		tx.commit();
		HibernateSessionFactory.closeSession();
	}

	public User getUser(String userid) throws DBException {
		Session session;
		User user = null;
		try {
			session = HibernateSessionFactory.currentSession();
			user = (User) session.get(User.class, userid);
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DBException("获取对象失败!");
		} finally {
			try {
				HibernateSessionFactory.closeSession();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
		}
		return user;
	}

	public User getUserByUsername(String username) throws DBException {
		Session session;
		User user = null;
		try {
			session = HibernateSessionFactory.currentSession();
			List list = session
					.createQuery(
							"from book.example.photoprint.po.User user where user.username=:username")
					.setString("username", username).list();
			if (list != null && list.size() > 0) {
				user = (User) list.get(0);
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DBException("获取对象失败!");
		} finally {
			try {
				HibernateSessionFactory.closeSession();
			} catch (HibernateException e) {
				e.printStackTrace();
			}
		}
		return user;
	}

	public List getUserList(String username) throws DBException {
		List list = null;
		Session session;
		try {
			session = HibernateSessionFactory.currentSession();
			list = session
					.createQuery(
							"from book.example.photoprint.po.User user where user.username like ?")
					.setString(0, "%" + username + "%").list();
			System.out.println(">>>>>>>>>>>>>>>>>"+list.size());

		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DBException("获取对象失败!");
		} finally {
			try {
				HibernateSessionFactory.closeSession();
			} catch (HibernateException e) {
				e.printStackTrace();
				throw new DBException("关闭session失败!");
			}
		}
		return list;
	}
}

⌨️ 快捷键说明

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