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

📄 productdaoimpl.java

📁 ssh 实例spring对hibernate的封装
💻 JAVA
字号:
package com.ssh.hibernate.dao;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ssh.hibernate.pojo.Product;

/**
 * Class, implements Interface ProductDao
 *@author T.Xin.Jun
 */
public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao {

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#delete(com.ssh.hibernate.pojo.Product)
	 */
	@Override
	public boolean delete(Product product) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().delete(product);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#findByName(java.lang.String)
	 */
	@Override
	public List<Product> findByName(final String name, final int start, final int count) {
		// TODO Auto-generated method stub
		return (List<Product>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query = session.createQuery("from Product p where p.name like :sortName");
				query.setString("sortName", "%" + name + "%");
				query.setFirstResult(start);
				query.setMaxResults(count);
				return query.list();
			}
			
		});
	}

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#list()
	 */
	@Override
	public List<Product> list(final int start,final int count) {
		// TODO Auto-generated method stub
		return (List<Product>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query = session.createQuery("from Product");
				query.setFirstResult(start);
				query.setMaxResults(count);
				return query.list();
			}
			
		});
	}

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#load(java.lang.Long)
	 */
	@Override
	public Product load(Long id) {
		// TODO Auto-generated method stub
		return (Product)this.getHibernateTemplate().get(Product.class, id);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#save(com.ssh.hibernate.pojo.Product)
	 */
	@Override
	public boolean save(Product product) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().save(product);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#update(com.ssh.hibernate.pojo.Product)
	 */
	@Override
	public boolean update(Product product) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().update(product);
			return true;
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#findBySort(java.lang.String)
	 */
	@Override
	public List<Product> findBySort(final String sortName, final int start, final int count) {
		// TODO Auto-generated method stub
		return (List<Product>) this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query = session.createQuery("from Product p where p.sort.name like :sortName");
				query.setString("sortName", "%" + sortName + "%");
				query.setFirstResult(start);
				query.setMaxResults(count);
				return query.list();
			}
			
		});
	}

	/*
	 * (non-Javadoc)
	 * @see com.ssh.hibernate.dao.ProductDao#getTotalCount(java.lang.String, java.lang.String)
	 */
	@Override
	public int getTotalCount(final String type, final String keyword){
		// TODO Auto-generated method stub
		int totalCount = 0;
		totalCount = ((Integer)this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql = "select count(*) from Product p ";
				try {
					if("product_name".equals(type)){
						hql = hql + "where p.name like '%" + new String(keyword.getBytes(), "UTF-8") + "%'";
					}else if("sort_name".equals(type)){
						hql = hql + "where p.sort.name like '%" + new String(keyword.getBytes(), "UTF-8") + "%'";
					}
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Query query = session.createQuery(hql);
				return query.setMaxResults(1).uniqueResult();
			}
			
		})).intValue();
		return totalCount;
	}

}

⌨️ 快捷键说明

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