zoukankan      html  css  js  c++  java
  • dao层的泛型实现(2种方法)

    一:

    package com.wzs.test2.dao;
    
    import java.util.List;
    
    public interface CommonDAO {
    	public <T> void sava(T entity); // 保存用户,无返回值;
    
    	public <T> void remove(T entity); // 删除用户
    
    	public <T> void update(T entity); // 更新用户
    
    	public <T> T findById(Class<T> entityClass, Integer id); // 通过id来查找某一个用户;
    
    	public <T> List<T> findAll(Class<T> entityclass); // 使用范型List<>。查询全部的用户信息
    
    }
    

    package com.wzs.test2.dao.impl;
    
    import java.util.List;
    
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    
    import com.wzs.test2.dao.CommonDAO;
    
    public class CommonDAOImpl extends HibernateDaoSupport implements CommonDAO {
    	@SuppressWarnings("unchecked")
    	public <T> List<T> findAll(Class<T> entityclass) {
    		String hql = "from " + entityclass.getName() + " as a order by a.id desc";
    		return this.getHibernateTemplate().find(hql);
    	}
    
    	@SuppressWarnings("unchecked")
    	public <T> T findById(Class<T> entityClass, Integer id) {
    		return (T) this.getHibernateTemplate().get(entityClass, id);
    	}
    
    	public <T> void remove(T entity) {
    		this.getHibernateTemplate().delete(entity);
    	}
    
    	public <T> void sava(T entity) {
    		this.getHibernateTemplate().save(entity);
    	}
    
    	public <T> void update(T entity) {
    		this.getHibernateTemplate().update(entity);
    	}
    
    }
    

    二:

    package com.wzs.test.dao;
    
    import java.io.Serializable;
    import java.util.Collection;
    
    public interface BaseDao<T> {
    	public void saveEntity(T t);
    
    	public Collection<T> get();
    
    	public void updateEntity(T t);
    
    	public void deleteEntity(Serializable id);
    
    	public T getEntityById(Serializable id);
    
    }
    

    package com.wzs.test.dao.impl;
    
    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    import java.util.Collection;
    
    import org.springframework.orm.hibernate3.HibernateTemplate;
    
    import com.wzs.test.dao.BaseDao;
    
    public class BaseDaoImpl<T> implements BaseDao<T> {
    
    	private HibernateTemplate hibernateTemplate;
    
    	private Class classt;
    
    	public BaseDaoImpl() {
    		// ParameterizedType就是泛型,关键部分
    		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
    		this.classt = (Class) type.getActualTypeArguments()[0];// <T>
    		System.out.println(type.getRawType());
    	}
    
    	public void deleteEntity(Serializable id) {
    		T t = this.getEntityById(id);
    		hibernateTemplate.delete(t);
    
    	}
    
    	public Collection<T> get() {
    		System.out.println(classt.getSimpleName());
    		return this.hibernateTemplate.find("from " + classt.getSimpleName());
    	}
    
    	public T getEntityById(Serializable id) {
    		return (T) this.hibernateTemplate.get(classt, id);
    	}
    
    	public void saveEntity(T t) {
    		this.hibernateTemplate.save(t);
    
    	}
    
    	public void updateEntity(T t) {
    		this.hibernateTemplate.update(t);
    	}
    
    	// set&&get
    	public HibernateTemplate getHibernateTemplate() {
    		return hibernateTemplate;
    	}
    
    	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    		this.hibernateTemplate = hibernateTemplate;
    	}
    
    }
    

    package com.wzs.test.dao;
    
    public interface StudentDao<Student> extends BaseDao<Student> {
    
    }
    

    package com.wzs.test.dao.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.wzs.test.dao.StudentDao;
    import com.wzs.test.dao.bean.Student;
    
    @Component
    public class StudentDaoImpl extends BaseDaoImpl<Student> implements StudentDao<Student> {
    
    }
    

    package com.wzs.test.dao.bean;
    
    public class Student {
    
    }
    

    參考:

    http://www.cnblogs.com/shenliang123/archive/2012/04/05/2433134.html

    http://blog.csdn.net/qiaoge134/article/details/20228139

  • 相关阅读:
    2015总结篇
    Android应用性能优化实践
    Android国外学习资源汇总
    直接拿来用!十大Material Design开源项目
    selenium12-读取文件 excel
    selenium11-自动化练习读取文件txt
    selenium10-python3部分代码复习
    selenium09-自动化练习案例
    selenium08-测试用例模块化与数据分离
    selenium07-处理 alter 对话框
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5398927.html
Copyright © 2011-2022 走看看