在进入今天的主题之前,我们先理解一下什么是泛型:

泛型是java中一种类型,泛型是被参数化的类型。

类型-->class

参数化-->class类型可以是任意参数

泛型存在的意义:泛型可以在编译的时候,告诉class传递的参数是什么类型,如果类型发送错误,在编译的时候,就会报错。

泛型所表现的形式:

1、java的list,set等集合所表现的。

    //这里面的E表示的就是泛型,
List<E> list = new ArrayList<E>();
//下面传入泛型的具体实例
List<Department> departmentList = new ArrayList<Department>();
//这个说明,在departmentList集合中,只能存放Department这一种类型

2、在类中的体现

package cn.ygh.boke;

public interface PersonDao<T> {
//这里的T也是泛型
}
package cn.ygh.boke;

public class PersonDaoImpl<T> implements PersonDao<T> {
//PersonDaoImpl这个类实现了PersonDao这个泛型类,那么他就要赋值给这个泛型类的泛型传值
/*
* PersonDaoImpl<T>的这个泛型T相当于给PersonDao<T>的T传值
* 总之,谁加泛型,谁给值
    这种方式也是我们下面主要介绍的方式
*/
}

3、

public class Person <T extends Collection<E>>{

}

介绍了什么三种泛型的表现形式,我们在介绍一下泛型的用法:泛型可以使用在某个类的属性或者方法(包括方法的参数和返回值)上

下面我来举一个比较实用的例子:例子基于hibernate,spring框架。

需求:要求写一个泛型的dao类,完成对所有对象进行简单的增删改查。

分析:我们在使用java操作数据库,因为hibernate的操作是基于对象,而我们从数据库中获取一行数据,也是需要把这行数据变成一个对象的

如果对每个不同的对象写一个xxxDao的类,把数据库的关系模型变成java的对象模型,实在是太过麻烦,能不能就写一个类,在类里面传入泛型,

让类根据泛型,泛型一个实例对象呢?

答案是可以的。

首先我们来写一个接口BaseDao,里面有增删改查的一些方法

//在这个类中,我们引入了泛型T,代表需要返回或者操作的java对象
public interface BaseDao<T> {
public Collection<T> getAllEntries();
public T getEntryById(Serializable id);
public void saveEntry(T t);
public void updateEntry(T t);
public void deleteEntryById(Serializable id);
}

然后我们再一个类BaseDaoImpl实现它,

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection; import org.springframework.orm.hibernate3.HibernateTemplate; import cn.itcast.oa.dao.base.BaseDao; //子类里面的T是给父类里面T赋值,子类里面的T是实参,父类里面的t是形参
public class BaseDaoImpl<T> implements BaseDao<T> { public HibernateTemplate hibernateTemplate;
private Class classt;//泛型的Class
/*
* 在父类中,要执行一段代码,执行的时间是在子类创建对象的时候,那么有两种解决方案
* 1、使用static代码块
* 2、利用父类的构造函数
*
* 分析:如果需要使用到this,那么就要使用父类的构造函数,如果不需要使用daothis,可以使用static代码块
* 因为下面需要使用this,获取ParameterizedType,所以使用父类的构造函数
* 如何获取泛型里面的class
*/
public BaseDaoImpl(){//使用父类的构造函数来实现对获取泛型的Class
//ParameterizedType,就是泛型,这里的this不是BaseDaoImpl,而是BaseDaoImpl的子类的this,
     //也就是一个类会继承BaseDaoImpl,然后通过它来获取其父类BaseDaoImpl的泛型T
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
//获取到泛型的Class
this.classt = (Class) type.getActualTypeArguments()[0];
}   /*
  有人会问,为什么要获取泛型的Class,对应hibernate操作数据库来说,获取一个泛型的实例,需要通过泛型的Class来获取
  说白了,没有泛型的Class,就无法获取泛型的实例对象,也就没办法返回给service层实例对象
*/
public Collection<T> getAllEntries() {
return this.getHibernateTemplate().find("from "+this.classt.getName());
}

   //很明显的可以看出,我们的返回值使用的是T类型,不依赖任何的实例对象
public T getEntryById(Serializable id) {
// TODO Auto-generated method stub
return (T) this.hibernateTemplate.get(this.classt, id); }
   //这个方法也是,下面的方法都是这个特地,我就不异议介绍了
public void saveEntry(T t) {
// TODO Auto-generated method stub
this.hibernateTemplate.save(t);
} public void updateEntry(T t) {
// TODO Auto-generated method stub
this.hibernateTemplate.update(t);
} public void deleteEntryById(Serializable id) {
T t = this.getEntryById(id);
this.getHibernateTemplate().delete(t); } public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
} public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}

下面来具体实现这个类,首先我们创建一个JavaBean:Department

import java.io.Serializable;
import java.util.Set; public class Department implements Serializable{ private Long did;//部门id
private String dname;//部门名称
private String description;//部门描述
private Set<User> users;//部门里面所有的用户 public Long getDid() {
return did;
}
public void setDid(Long did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
} }

创建一个接口DepartmentDao<T>接口,里面有对Department操作的方法,让它继承BaseDao<T>这个接口

import java.io.Serializable;
import java.util.Collection; import cn.itcast.oa.dao.base.BaseDao;
import cn.itcast.oa.domain.Department; public interface DepartmentDao<T> extends BaseDao<T>{
//对department操作的各种方法
public void saveDepartment(Department department);
public void updateDepartment(Department department);
public void deleteDepartmentById(Serializable id,String deleteMode);
public Collection<Department> getAllDepartments();
public Department getDepartmentById(Serializable id);
}

在创建DepartmentDao的一个子类DepartmentDaoImpl,实现DepartmentDao,继承BaseDaoImpl,把泛型传递过去

这样这个DepartmentDaoImpl,就可以操作对department的增删改查(只是简单的实现)。

到这里,我们可以发现,BaseDaoImpl<Department> 和DepartmentDao<Department>可以无限次重用,只要往这两个类传递泛型

那么就可以直接完成对泛型对象的增删改查的操作。

import java.io.Serializable;
import java.util.Collection;
import java.util.Set; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.itcast.oa.dao.DepartmentDao;
import cn.itcast.oa.dao.base.impl.BaseDaoImpl;
import cn.itcast.oa.domain.Department;
import cn.itcast.oa.domain.User;
import cn.itcast.oa.utils.DeleteModel; //传递泛型
public class DepartmentDaoImpl extends BaseDaoImpl<Department> implements DepartmentDao<Department>{ public void saveDepartment(Department department) {
//直接使用父类的泛型方法进行操作
// TODO Auto-generated method stub
this.saveEntry(department);
} public void updateDepartment(Department department) {
//直接使用父类的泛型方法进行操作
// TODO Auto-generated method stub
this.updateEntry(department);
} public void deleteDepartmentById(Serializable id,String deleteMode) {
// TODO Auto-generated method stub
Department department = (Department) this.getHibernateTemplate().get(Department.class, id);
if(DeleteModel.DEL.equals(deleteMode)){
this.getHibernateTemplate().delete(department);
}else if(DeleteModel.DEL_PRE_RELEASE.equals(deleteMode)){
Set<User> users = department.getUsers();
for(User user:users){
user.setDepartment(null);//解除关系
}
this.getHibernateTemplate().flush();
this.getHibernateTemplate().delete(department);
}else{//进行级联删除
Set<User> users = department.getUsers();
for(User user:users){
user.setDepartment(null);//解除关系部门关系
user.setPosts(null);//解除岗位关系
this.getHibernateTemplate().flush();
this.getHibernateTemplate().delete(user);
}
//删除部门
this.getHibernateTemplate().delete(department);
} } public Collection<Department> getAllDepartments() {
//直接使用父类的泛型方法进行操作
return this.getAllEntries();
} public Department getDepartmentById(Serializable id) {
//直接使用父类的泛型方法进行操作
return this.getEntryById(id);
} }

泛型的知识到这里就已经结束了,下面赋上hibernate配置文件和spring配置文件,让大家可以更好的理解

Department.hbm.xml:Department的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="cn.itcast.oa.domain.Department" table="department">
<id name="did" type="java.lang.Long" length="5">
<generator class="increment"></generator>
</id>
<property name="dname" type="java.lang.String" length="20"></property>
<property name="description" type="java.lang.String" length="500"></property>
<set name="users" inverse="true">
<key>
<column name="did"></column>
</key>
<one-to-many class="cn.itcast.oa.domain.User"/>
</set>
</class>
</hibernate-mapping>

applicationContext.xml:spring的总的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 导入其他的spring配置文件-->
<import resource="applicationContext-db.xml"/>
<import resource="applicationContext-person.xml"/>
<import resource="applicationContext-department.xml"/>
</beans>

applicationContext-db.xml:spring的关于hibernate和数据库的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean> <tx:advice transaction-manager="transactionManager" id="tx">
<tx:attributes>
<tx:method name="save*" read-only="false"/>
<tx:method name="update*" read-only="false"/>
<tx:method name="delete*" read-only="false"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut expression="execution(* cn.itcast.oa.service.impl.*.*(..))" id="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform"/>
</aop:config> <!-- <bean id="baseDdaoImpl" class="cn.itcast.oa.dao.base.impl.BaseDaoImpl" abstract="true">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean> -->
</beans>

applicationContext-department.xml:spring的关于department的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="departmentDao" class="cn.itcast.oa.dao.impl.DepartmentDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean> <bean id="departmentService" class="cn.itcast.oa.service.impl.DepartmentServiceImpl">
<property name="departmentDao">
<ref bean="departmentDao"/>
</property>
</bean>
<bean id="departmentAction" class="cn.itcast.oa.struts.action.DepartmentAction" scope="prototype">
<property name="departmentService">
<ref bean="departmentService"/>
</property>
</bean> </beans>

希望对大家有帮助。

浅谈:java泛型与dao重用的更多相关文章

  1. 浅谈Java泛型之<? extends T>和<? super T>的区别

    关于Java泛型,这里我不想总结它是什么,这个百度一下一大堆解释,各种java的书籍中也有明确的定义,只要稍微看一下就能很快清楚.从泛型的英文名字Generic type也能看出,Generic普通. ...

  2. 浅谈Java——泛型DAO

    首先解释一下为什么要学习泛型DAO.平时在写DAO的时候是一个接口对应一个实现类,实现类里面要写很多的操作数据库的方法.当我们有很多的javaben的时候我们会写很多的接口和实现类,并且里面的代码都是 ...

  3. 浅谈Java泛型中的extends和super关键字(转)

    通配符 在本文的前面的部分里已经说过了泛型类型的子类型的不相关性.但有些时候,我们希望能够像使用普通类型那样使用泛型类型: 向上造型一个泛型对象的引用 向下造型一个泛型对象的引用 向上造型一个泛型对象 ...

  4. 浅谈Java泛型中的extends和super关键字

    泛型是在Java 1.5中被加入了,这里不讨论泛型的细节问题,这个在Thinking in Java第四版中讲的非常清楚,这里要讲的是super和extends关键字,以及在使用这两个关键字的时候为什 ...

  5. 浅谈Java泛型中的? extends E和?super E

    https://blog.csdn.net/zymx14/article/details/78073757

  6. 浅谈java类集框架和数据结构(2)

    继续上一篇浅谈java类集框架和数据结构(1)的内容 上一篇博文简介了java类集框架几大常见集合框架,这一篇博文主要分析一些接口特性以及性能优化. 一:List接口 List是最常见的数据结构了,主 ...

  7. 龙生九子-浅谈Java的继承

    龙生九子-浅谈Java的继承 书接上回,我们之前谈过了类和对象的概念,今天我来讲一讲面向对象程序设计的另外一个基本概念-继承 目录 为什么需要继承 自动转型与强制转型 继承能干啥 复写和隐藏 supe ...

  8. 浅谈Java的反射机制和作用

    浅谈Java的反射机制和作用 作者:Java大师 欢迎转载,转载请注明出处 很多刚学Java反射的同学可能对反射技术一头雾水,为什么要学习反射,学习反射有什么作用,不用反射,通过new也能创建用户对象 ...

  9. 浅谈Java的throw与throws

    转载:http://blog.csdn.net/luoweifu/article/details/10721543 我进行了一些加工,不是本人原创但比原博主要更完善~ 浅谈Java异常 以前虽然知道一 ...

随机推荐

  1. winform窗体(五)——布局方式

    一.默认布局 ★可以加panel,也可以不加: ★通过鼠标拖动控件的方式,根据自己的想法布局.拖动控件的过程中,会有对齐的线,方便操作: ★也可选中要布局的控件,在工具栏中有对齐工具可供选择,也有调整 ...

  2. SQL*LOADER错误总结

    在使用SQL*LOADER装载数据时,由于平面文件的多样化和数据格式问题总会遇到形形色色的一些小问题,下面是工作中累积.整理记录的遇到的一些形形色色错误.希望能对大家有些用处.(今天突然看到自己以前整 ...

  3. native2ascii.exe 字符转码与反转码

    本人最近在做OAF的二次开发,在看别人写的代码时,发现总有类似这样的语句:”\u65e0\u6548\u7684GP\u9879\u76ee\u7f16\u53f7“,这些语句其实是用Java_hom ...

  4. 设置共享,实现Linux和Windows之间的共享

    设置共享,实现Linux和Windows之间的共享 前提: 安装虚拟机.可以参考:在Windows上安装虚拟机详细图文教程 安装Linux.可以参考:在VMware Workstation里的Linu ...

  5. C# 记录错误日志

    程序的错误日志如何记录下来? 可以在遇到异常时,Catch异常,然后把异常的信息输出到txt文件中即可 /// <summary> /// 错误日志 /// </summary> ...

  6. Eclipse不自动编译java文件的终极解决方案

    最近我的eclipse经常犯傻,项目中总是有很多,启动项目也是没有启动类.查了下项目中生成的class文件,我靠竟然没有,或者还是以前的.原来是eclipse犯傻了,它没帮我自动编译java文件.一般 ...

  7. 报表软件JS开发引用HTML DOM的windows对象

    HTML DOM是W3C标准(是HTML文档对象模型的英文缩写,Document Object Model for HTML). HTML DOM定义了用于HTML的一些列标准的对象,以及访问和处理H ...

  8. Unity Shader入门

    Unity Shader入门 http://www.cnblogs.com/lixiang-share/p/5025662.html http://www.manew.com/blog-30559-1 ...

  9. Mui沉浸模式以及状态栏颜色改变

    沉浸模式只需要设置下就可以  ios:  打开应用的manifest.json文件,切换到代码视图,在plus -> distribute -> apple 下添加UIReserveSta ...

  10. LinkedIn的即时消息:在一台机器上支持几十万条长连接

    最近我们介绍了LinkedIn的即时通信,最后提到了分型指标和读回复.为了实现这些功能,我们需要有办法通过长连接来把数据从服务器端推送到手机或网页客户端,而不是许多当代应用所采取的标准的请求-响应模式 ...