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

📄 userdaoimp.java

📁 struts hibernate框架 商场买卖
💻 JAVA
字号:
package com.shopping.dao.imp;

import java.util.Iterator;
import java.util.List;

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

import com.shopping.dao.UnifyDao;
import com.shopping.util.HibernateSessionFactory;
import com.shopping.vo.User;


public class UserDaoImp implements UnifyDao {

	public boolean deleteTableById(String id) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran =null;
		try {
			User user = (User) session.get(User.class, id);
			tran=session.beginTransaction();

			session.delete(user);
			tran.commit();
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return false;
	}

	public List getTable(int pageSize, int startRow) {
		try {
			Session session = HibernateSessionFactory.getSession();
			String hql = "from User as u order by u.rdate desc";
			Query q = session.createQuery(hql);
			q.setMaxResults(pageSize);
			q.setFirstResult(startRow);
			List l = q.list();
			return l;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return null;
	}

	public int getRows() {
		int totalRows = 0;
		try {
			Session session = HibernateSessionFactory.getSession();
			String query = "select count(*) from User u";
			totalRows = (Integer)session.createQuery(query).uniqueResult();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}

		return totalRows;
	}

	public List getTable(String keyword,int pageSize, int startRow) {
		try {
			Session session = HibernateSessionFactory.getSession();
			String hql = "from User as s where s.username like '%"+keyword+"%'";
			Query q = session.createQuery(hql);
			q.setMaxResults(pageSize);
			q.setFirstResult(startRow);
			List l = q.list();
			return l;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return null;
	}

	public int getRows(String keyword) {
		int totalRows = 0;
		try {
			Session session = HibernateSessionFactory.getSession();
			String query = "select count(*) from User as s where s.username like '%"+keyword+"%'";
			totalRows = (Integer)session.createQuery(query).uniqueResult();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}

		return totalRows;
	}

	public Object getTableById(int id) {
		Session session = HibernateSessionFactory.getSession();
		User user =null;
		try {
			user = (User) session.get(User.class, id);
			return user;
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally {
			HibernateSessionFactory.closeSession();
		}
		return null;
	}

	public boolean insertTable(Object object) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran =null;
		
		try {
			tran=session.beginTransaction();
			session.save(object);
			tran.commit();
			return true;
		} catch (HibernateException e) {
			tran.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return false;
	}

	public boolean updateTable(Object object) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran =null;
		
		try {
			tran=session.beginTransaction();
			session.update(object);
			tran.commit();
			return true;
		} catch (HibernateException e) {
			tran.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return false;
	}

	public List getTable() {
		try {
			Session session = HibernateSessionFactory.getSession();
			String hql = "from User as s order by s.rdate desc";
			Query q = session.createQuery(hql);
			q.setMaxResults(8);
			q.setFirstResult(0);
			List l = q.list();
			return l;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return null;
	}

	public Object getTableByName(String name) {
		Session session = HibernateSessionFactory.getSession();
		User user =null;
		String hql = "from User u where u.username=:username";
		try {
			AbstractQueryImpl q = (AbstractQueryImpl) session.createQuery(hql);
			q.setString("username", name);
			user = (User) q.uniqueResult();
			return user;
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		}finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public List getTable(int pid) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getRows(int catId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List getTable(int catId, int pageSize, int startRow) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean deleteTableById(int id) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran =null;
		try {
			User user = (User) session.get(User.class, id);
			tran=session.beginTransaction();

			session.delete(user);
			tran.commit();
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return false;
	}

	public int getRows2(int catId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List getTable2(int catId, int pageSize, int startRow) {
		// TODO Auto-generated method stub
		return null;
	}

	public int getRows(String keyword, int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int getRows2(String keyword, int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List getTable(String keyword, int id, int pageSize, int startRow) {
		// TODO Auto-generated method stub
		return null;
	}

	public List getTable2(String keyword, int id, int pageSize, int startRow) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean deleteTableByPid(int pid) {
		// TODO Auto-generated method stub
		return false;
	}

}

⌨️ 快捷键说明

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