以前做设计的时候dao接口和它的实现了,这样子就不必写这么多的重复代码了。但由于对反射没有了解,除非依赖hibernate的其他组件,否则写不出来。不过,有了反射,我们可以通过泛型来实现我们想要做的功能了。
首先是接口:
package com.sms.dao.base;
import java.util.List;
public interface BaseDao<T> {
public void add(T entity) throws Exception;
public void delete(T entity) throws Exception;
public void update(T entity) throws Exception;
public T findById(Integer id) throws Exception;
/*
* 得到从startIndex开始大小为pageSize的列表
*/
public List<T> getPageList(int startIndex , int pageSize) throws Exception;
/*
* 得到总数
*/
public long getAmount();
}
然后是实现类:
package com.sms.dao.base.impl;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hib