泛型在Web中的作用
来源:互联网 发布:娄烨 知乎 编辑:程序博客网 时间:2024/05/08 19:12
当我们写网页的时候,常常会有多个DAO,我们要写每次都要写好几个DAO,这样会有点麻烦。
那么我们想要的效果是什么呢??只写一个抽象DAO,别的DAO只要继承该抽象DAO,就有对应的方法了。
要实现这样的效果,肯定是要用到泛型的。因为在抽象DAO中,是不可能知道哪一个DAO会继承它自己,所以是不知道其具体的类型的。而泛型就是在创建的时候才指定其具体的类型。
- 抽象DAO
public abstract class BaseDao<T> { //模拟hibernate.... private Session session; private Class clazz; //哪个子类调的这个方法,得到的class就是子类处理的类型(非常重要) public BaseDao(){ Class clazz = this.getClass(); //拿到的是子类 ParameterizedType pt = (ParameterizedType) clazz.getGenericSuperclass(); //BaseDao<Category> clazz = (Class) pt.getActualTypeArguments()[0]; System.out.println(clazz); } public void add(T t){ session.save(t); } public T find(String id){ return (T) session.get(clazz, id); } public void update(T t){ session.update(t); } public void delete(String id){ T t = (T) session.get(clazz, id); session.delete(t); }}
- 继承抽象DAO,该实现类就有对应的增删改查的方法了。
CategoryDao
public class CategoryDao extends BaseDao<Category> {}
BookDao
public class BookDao extends BaseDao<Book> {}
1 0
- 泛型在Web中的作用
- xml在Web中的作用
- 视图在WEB开发中的作用
- context-param在web.xml中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- web.xml中的contextConfigLocation在spring中的作用
- 10038---web.xml中的contextConfigLocation在spring中的作用
- WSDL在Web Service中的意义和作用
- Web Service在2005企业应用集成中的作用调查报告
- PHP7和HHVM的性能之争
- 一种局部ui界面切换解决方案
- session 入库
- java数据结构与算法分析学习2(泛型)
- Extjs替换自带theme
- 泛型在Web中的作用
- springMVC–参数绑定常用的注解
- 在Linux下安装配置JDK
- Android cache和files的区别
- Python 执行shell命令:system
- 安卓开发——安卓5.0以上版本如何去除Button自带阴影效果
- RCC_APB1和RCC_APB2的区别
- 螺旋矩阵输出
- osg自定义场景物体和设置状态集