前几天在看一个项目视频,里面用到了dao层泛型重构,但是service层就没有那样,我想知道service层泛型重构怎么写,dao层我已经会了,求详细的代码,好人一生平安。这是dao层重构代码- public interface BaseDao<T> {
- public Collection<T> getAllEntry();
- public T getEntryById(Serializable id);
- public void saveEntry(T t);
- public void updateEntry(T t);
- public void delteEntry(Serializable id);
- }
复制代码- public class BaseDaoImpl<T> implements BaseDao<T> {
- private Class classt;
- @Resource(name="hibernateTemplate")
- public HibernateTemplate hibernateTemplate;
- public BaseDaoImpl() {
- ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
- this.classt = (Class)type.getActualTypeArguments()[0];
- }
-
- @Override
- public Collection<T> getAllEntry() {
- return this.hibernateTemplate.find("from " + this.classt.getName());
- }
- @Override
- public T getEntryById(Serializable id) {
- return (T) this.hibernateTemplate.get(this.classt, id);
- }
- @Override
- public void saveEntry(T t) {
- this.hibernateTemplate.save(t);
- }
- @Override
- public void updateEntry(T t) {
- this.hibernateTemplate.update(t);
- }
- @Override
- public void delteEntry(Serializable id) {
- this.hibernateTemplate.delete(this.getEntryById(id));
- }
- }
复制代码- public interface DepartmentDao<T> extends BaseDao<T>{
- }
复制代码- @Repository(value="departmentDao")
- public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao<Department> {
- }
复制代码 |