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

📄 userdaoimpl.java

📁 Struts+Hibernate综合运用
💻 JAVA
字号:
package com.chaoskz.hibernate.dao;

import java.util.List;

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

import com.chaoskz.hibernate.database.HibernateSessionFactory;
import com.chaoskz.hibernate.entity.User;

public class UserDAOImpl implements IUserDAO {

	public boolean doAddUser(User user) {
		Transaction transaction = null;
		try {
			Session session = HibernateSessionFactory.getSession();
			transaction = session.beginTransaction();
			session.save(user);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return false;
	}

	public boolean doDeleteUser(String ID) {
		Transaction transaction = null;
		try {
			Session session = HibernateSessionFactory.getSession();
			User u = (User) session.load(User.class, ID);
			transaction = session.beginTransaction();
			session.delete(u);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return false;
	}

	public List doGetAllUsers() {
		String sqlStr = "from User";
		List list = null;
		try {
			Session session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(sqlStr);
			list = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return list;
	}

	public boolean doUpdateUser(User user) {
		Transaction transaction = null;
		try {
			Session session = HibernateSessionFactory.getSession();
			transaction = session.beginTransaction();
			session.update(user);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			transaction.rollback();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return false;
	}

	public boolean doVerifyUser(User user) {
		String sqlStr = "from User as u where u.name = ? and u.password = ?";
		try {
			Session session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(sqlStr);
			query.setString(0, user.getName());
			query.setString(1, user.getPassword());
			if (query.list().size() > 0)
				return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return false;
	}
	
	public User doFindUserByID(String ID){
		
		String sqlStr = "from User as u where u.id='"+ID+"'";
		User user = null;
		try {
			Session session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(sqlStr);
			user = (User)query.uniqueResult();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return user;
		
	}
	public List doSearchUser(String key,String type){
		
		String sqlStr = "from User where "+type+" like '%"+key+"%'";
		List list = null;
		try {
			Session session = HibernateSessionFactory.getSession();
			Query query = session.createQuery(sqlStr);
			list = query.list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return list;
		
	}

}

⌨️ 快捷键说明

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