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

📄 orderdaoimp.java

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

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.Salesorder;


public class OrderDaoImp implements UnifyDao {

	public boolean deleteTableById(String id) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran =null;
		try {
			Salesorder order = (Salesorder) session.load(Salesorder.class, id);
			tran=session.beginTransaction();

			session.delete(order);
			tran.commit();
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
			tran.rollback();
		}finally{
			HibernateSessionFactory.closeSession();
		}
		return false;
	}
	
	public boolean deleteTableById(int id) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tran =null;
		try {
			Salesorder order = (Salesorder) session.load(Salesorder.class, id);
			tran=session.beginTransaction();
			session.delete(order);
			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 Salesorder as s order by s.odate 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 Salesorder u";
			totalRows = (Integer)session.createQuery(query).uniqueResult();
			
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}

		return totalRows;
	}

	public List getTable(int catId,int pageSize, int startRow) {
		try {
			Session session = HibernateSessionFactory.getSession();
			String hql = "from Salesorder as u where u.category.id=:catId";
			Query q = session.createQuery(hql);
			q.setInteger("catId", catId);
			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 catId) {
		int totalRows = 0;
		try {
			Session session = HibernateSessionFactory.getSession();
			String query = "select count(*) from Salesorder u where u.category.id="+catId;
			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 Salesorder as s where s.name 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 Salesorder as s where s.name 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();
		Salesorder order = null;
		try {
			String hql = "from Salesorder as u left join fetch u.lineitems where u.id=:id order by u.odate desc";
			Query q = session.createQuery(hql);
			q.setInteger("id", id);
			order = (Salesorder) q.uniqueResult();
			return order;
		} 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 Salesorder as s order by s.odate desc";
			Query q = session.createQuery(hql);
			q.setMaxResults(10);
			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();
		Salesorder order =null;
		String hql = "from Salesorder u where u.name=:Salesordername";
		try {
			AbstractQueryImpl q = (AbstractQueryImpl) session.createQuery(hql);
			q.setString("Salesordername", name);
			order = (Salesorder) q.uniqueResult();
			return order;
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		}finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public List getTable(int id) {
		try {
			Session session = HibernateSessionFactory.getSession();
			String hql = "from Salesorder as u where u.user.id=:id";
			Query q = session.createQuery(hql);
			q.setInteger("id", id);
			List<Salesorder> l = q.list();
			return l;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return null;
	}

	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 + -