365 shop Dao And Daoの実装インタフェース


1.ProductDao.java and ProductDaoHibernate.java
package com.v12.shop.dao;

import java.util.List;

import com.v12.shop.model.Product;

public interface ProductDao {
	
	public void add(Product p);
	
	public void delete(Integer id);
	
	public void update(Product p);
	
	public Product queryById(Integer id);
	
	public List<Product> queryByCategoryId(Integer cid);
}


//   


package com.v12.shop.dao.impl.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.v12.shop.dao.ProductDao;
import com.v12.shop.model.Product;

public class ProductDaoHibernate extends HibernateDaoSupport implements ProductDao {

	public void add(Product p) {
		getHibernateTemplate().save(p);

	}

	public void delete(Integer id) {
		Product p = (Product)getHibernateTemplate().get(Product.class, id);
		getHibernateTemplate().delete(p);

	}

	public List<Product> queryByCategoryId(Integer cid) {
		String hql="from Product p where p.category.id="+cid;
		
		return getHibernateTemplate().find(hql);
	}

	public Product queryById(Integer id) {
		
		return (Product)getHibernateTemplate().get(Product.class, id);
	}

	public void update(Product p) {
		getHibernateTemplate().update(p);

	}

}

 2.CategoryDao.java and CategoryDaoHibernate.java
package com.v12.shop.dao;

import java.util.List;

import com.v12.shop.model.Category;

public interface CategoryDao {
	public void add(Category c);
	
	public void delete(Integer id);
	
	public void update(Category c);
	
	public Category queryById(Integer id);
	
	public List<Category> queryAll();
}

//   

package com.v12.shop.dao.impl.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.v12.shop.dao.CategoryDao;
import com.v12.shop.model.Category;

public class CategoryDaoHibernate extends HibernateDaoSupport implements CategoryDao {

	public void add(Category c) {
		getHibernateTemplate().save(c);

	}

	public void delete(Integer id) {
		Category c = (Category)getHibernateTemplate().get(Category.class, id);
		getHibernateTemplate().delete(c);

	}

	public List<Category> queryAll() {
		
		return getHibernateTemplate().find("from Category order by id desc");
	}

	public Category queryById(Integer id) {
		
		
		return (Category)getHibernateTemplate().get(Category.class, id);
	}

	public void update(Category c) {
		getHibernateTemplate().update(c);

	}

}


 3.OrderDao.java and OrderDaoHibernate.java
package com.v12.shop.dao;

import com.v12.shop.model.Order;

public interface OrderDao {
	public void add(Order o);
	
	public void update(Order o);
	
	public Order queryById(Integer id);
	
	public Pager queryByPage(int pagesize, int offset);
}

//   


package com.v12.shop.dao.impl.hibernate;

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

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

import com.v12.shop.dao.OrderDao;
import com.v12.shop.dao.Pager;
import com.v12.shop.model.Order;

public class OrderDaoHibernate extends HibernateDaoSupport implements OrderDao {

	public void add(Order o) {
		getHibernateTemplate().save(o);

	}

	public Order queryById(Integer id) {
		
		return (Order)getHibernateTemplate().get(Order.class, id);
	}

	public Pager queryByPage(final int pagesize,final int offset) {
		HibernateTemplate ht = getHibernateTemplate();
		
		List list= ht.find("select count(id) from Order");
		
		Pager pager = new Pager();
		
		pager.setMaxResult(new Integer(list.get(0).toString()));
		
		final String hql="from Order order by id desc";
		List list2 = ht.executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
					Query query = session.createQuery(hql);
					query.setMaxResults(pagesize);
					query.setFirstResult(offset);
					
				return query;
			}
			
		});
		pager.setResultList(list2);
		return pager;
	}

	public void update(Order o) {
		getHibernateTemplate().update(o);

	}

}

 4.ページングPager.java
package com.v12.shop.dao;

import java.util.List;

public class Pager {
	private List resultList;
	private Integer maxResult;
	
	
	public Pager(){
		
	}
	public Pager(List resultList,Integer maxResult){
		this.resultList=resultList;
		this.maxResult=maxResult;
	}
	public List getResultList() {
		return resultList;
	}
	public void setResultList(List resultList) {
		this.resultList = resultList;
	}
	public Integer getMaxResult() {
		return maxResult;
	}
	public void setMaxResult(Integer maxResult) {
		this.maxResult = maxResult;
	}
	
	
}