struts2.2-jquery-plugin学习DAO层和Service层文件(四)

(一)DAO层的写法:

DAO接口:

package com.newsicom.jxc.basicInfo.goodsUnits.dao;

import java.util.List;

import com.newsicom.jxc.basicInfo.common.dao.IBaseDao;
import com.newsicom.jxc.basicInfo.common.util.PageBean;

public interface IGoodsUnitsDao<T> extends IBaseDao<T> {
	/**
	 * 保存对象
	 * @param t
	 */
	public void create(T t);
	/**
	 * 添加商品单位
	 * @param transientInstance
	 */
	public void save(T transientInstance) ;

	/**
	 * 删除商品
	 * @param persistentInstance
	 */
	public void delete(T persistentInstance) ;

	/**
	 * 根据id查找商品单位
	 * @param id
	 * @return
	 */
	public T findById(Class<T> clazz,java.lang.Integer id) ;

	/**
	 * 根据id删除商品单位
	 * @param id
	 */
	public void deleteByID(java.lang.Integer id);

	/**查找所有的商品单位
	 * @return
	 */
	public List<T> findAll();

	/**
	 * 分页查询
	 * @param sql
	 * @param index
	 * @param pageSize
	 * @return
	 */
	public PageBean<T> getT(String hql,int index,int pageSize);

	/**
	 * 用hibernate分页
	 * @param sql
	 * @param index
	 * @param pageSize
	 * @return
	 */
	public List<T> getPageT(String hql,int index,int pageSize);

	/**
	 * 更新
	 * @param productUnits
	 */
	public void update(T t);
}

 DAO实现类:

package com.newsicom.jxc.basicInfo.goodsUnits.dao.imp;

import java.util.List;

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

import com.newsicom.jxc.basicInfo.common.model.GoodsUnits;
import com.newsicom.jxc.basicInfo.common.model.ProductUnits;
import com.newsicom.jxc.basicInfo.common.util.PageBean;
import com.newsicom.jxc.basicInfo.goodsUnits.dao.IGoodsUnitsDao;

public class GoodsUnitsDao extends HibernateDaoSupport implements IGoodsUnitsDao<GoodsUnits> {

	/* (non-Javadoc)
	 * @see com.newsicom.jxc.basicInfo.goodsUnits.dao.IGoodsUnitsDao#create(java.lang.Object)
	 */
	public void create(GoodsUnits t) {
		getHibernateTemplate().persist(t);
	}

	public void save(GoodsUnits transientInstance) {
		getHibernateTemplate().save(transientInstance);
	}

	public void delete(GoodsUnits persistentInstance) {
		getHibernateTemplate().delete(persistentInstance);
	}

	public GoodsUnits findById(Class<GoodsUnits> clazz, Integer id) {
		int intId=id.intValue();
		return getHibernateTemplate().get(GoodsUnits.class,intId);
	}

	public void deleteByID(Integer id) {
		int intId=id.intValue();
		GoodsUnits goodsUnits=findById(GoodsUnits.class,intId);
		getHibernateTemplate().delete(goodsUnits);
		
	}

	public List<GoodsUnits> findAll() {
		return getHibernateTemplate().find("from GoodsUnits");
	}

	public PageBean<GoodsUnits> getT(String hql, int index, int pageSize) {
		PageBean pb=new PageBean();
		pb.setIndex(index);
		pb.setPageSize(pageSize);
		pb.setTotal(findAll().size());
		List<GoodsUnits> pageData=getPageT(hql,(index-1)*pageSize,pageSize);
		pb.setT(pageData);
		return pb;
	}

	public List<GoodsUnits> getPageT(String hql, int index, int pageSize) {
		Query q = getSession().createQuery(hql);   
		q.setFirstResult(index);;   
		q.setMaxResults(pageSize);;   
		List<GoodsUnits> list = q.list();
		return list;
	}

	public void update(GoodsUnits t) {
		getHibernateTemplate().update(t);
		
	}

}

 (二)Service层:

service层接口:

package com.newsicom.jxc.basicInfo.goodsUnits.service;

import java.util.List;

import com.newsicom.jxc.basicInfo.common.util.PageBean;

public interface IGoodsUnitsService<T> {
	/**
	 * 保存对象
	 * @param t
	 */
	public void create(T t);
	/**
	 * 添加商品单位
	 * @param transientInstance
	 */
	public void save(T transientInstance) ;

	/**
	 * 删除商品
	 * @param persistentInstance
	 */
	public void delete(T persistentInstance) ;

	/**
	 * 根据id查找商品单位
	 * @param id
	 * @return
	 */
	public T findById(Class<T> clazz,java.lang.Integer id) ;

	/**
	 * 根据id删除商品单位
	 * @param id
	 */
	public void deleteByID(java.lang.Integer id);

	/**查找所有的商品单位
	 * @return
	 */
	public List<T> findAll();

	/**
	 * 分页查询
	 * @param sql
	 * @param index
	 * @param pageSize
	 * @return
	 */
	public PageBean getT(String hql,int index,int pageSize);

	/**
	 * 用hibernate分页
	 * @param sql
	 * @param index
	 * @param pageSize
	 * @return
	 */
	public List<T> getPageT(String hql,int index,int pageSize);

	/**
	 * 更新
	 * @param productUnits
	 */
	public void update(T t);
}

 service层实现:

package com.newsicom.jxc.basicInfo.goodsUnits.service.imp;

import java.util.List;

import com.newsicom.jxc.basicInfo.common.model.GoodsUnits;
import com.newsicom.jxc.basicInfo.common.util.PageBean;
import com.newsicom.jxc.basicInfo.goodsUnits.dao.IGoodsUnitsDao;
import com.newsicom.jxc.basicInfo.goodsUnits.service.IGoodsUnitsService;

public  class GoodsUnitsService implements IGoodsUnitsService<GoodsUnits> {

	private IGoodsUnitsDao<GoodsUnits> goodsUnitsDao;
	public void create(GoodsUnits t) {
		this.goodsUnitsDao.create(t);
		
	}

	public void save(GoodsUnits transientInstance) {
		this.goodsUnitsDao.save(transientInstance);
	}

	public void delete(GoodsUnits persistentInstance) {
		this.goodsUnitsDao.delete(persistentInstance);
	}

	public GoodsUnits findById(Class<GoodsUnits> clazz, Integer id) {
		return (GoodsUnits) this.goodsUnitsDao.findById(clazz, id);
	}

	public void deleteByID(Integer id) {
		this.deleteByID(id);
	}

	public List<GoodsUnits> findAll() {
		return this.findAll();
	}

	public PageBean<GoodsUnits> getT(String hql, int index, int pageSize) {
		return this.goodsUnitsDao.getT(hql, index, pageSize);
	}

	public List<GoodsUnits> getPageT(String hql, int index, int pageSize) {
		return this.goodsUnitsDao.getPageT(hql, index, pageSize);
	}

	public void update(GoodsUnits t) {
		this.goodsUnitsDao.update(t);
	}

	public void setGoodsUnitsDao(IGoodsUnitsDao<GoodsUnits> goodsUnitsDao) {
		this.goodsUnitsDao = goodsUnitsDao;
	}

	
	

}

 关于DAO和service在applicationContext.xml的配置请查看第二篇文章关于配置

你可能感兴趣的:(DAO,service)