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

泛型是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. ORACLE清理、截断监听日志文件(listener.log)

    在ORACLE数据库中,如果不对监听日志文件(listener.log)进行截断,那么监听日志文件(listener.log)会变得越来越大,想必不少人听说过关于"LISTENER.LOG日 ...

  2. Linux LVM学习总结——放大LV容量

    本篇介绍LVM管理中的命令lvresize,我们先创建一个卷组VG VolGroup02,它建立在磁盘/dev/sdc (大小为8G)上.创建逻辑卷LV时,我们故意只使用了一小部分.具体情况如下所示 ...

  3. IntelliJ IDEA 教程设置讲解

    IntelliJ IDEA 常用设置讲解 说明 IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.Ant.JUnit.CVS整合.代码审查 ...

  4. PostgreSQL-角色、库、模式、表

    由于不了解postgresql的psql工具,安装完数据库后就直接用pgadmin或navicat来连接操作,在确认初始化后的库中默认有些什么东西后竟然一直无处下手,在还没有了解pg大致体系的情况下搞 ...

  5. andrioid 分享到其它(短信,qq,微信等功能)

    public static void share(Context context, String text) { Intent intent = new Intent(Intent.ACTION_SE ...

  6. 关于Finereport移动端报表二次开发的两个小例子

    例1:刷新页面 1. 问题描述 A超链至B填报,B提交数据后返回A时,A自动刷新显示新的数据. 2. 解决方案 1. contentPane.setAppearRefresh();  //在A的加载结 ...

  7. 浅谈Oracle权限体系

    对于数据库来讲,安全性的重要程度不言而喻,今天我们就来聊一聊Oracle的权限体系. 1.账户管理 在此之前,先解释下一个容易混淆的概念:模式.所谓模式,指的是用户账户所拥有的一组对象(比如表,索引, ...

  8. Shell基础学习小结

    0 shell基础概念 Shell是解释性语言,使用脚本编程语言的好处是,它们多半运行在比编译型语言还高的层级,能够轻易处理文件与目录之类的对象:缺点是它们的效率通常不如编译型语言.Shell命令有本 ...

  9. UI自动化,你值得拥有

    去年春节联欢晚会,为了那张“敬业福”,全家都卯足了劲儿“咻一咻”,连节目都顾不上看了.当时我就想,要是能自动化该多好,不停点击屏幕,屏幕不疼手还疼呢,何况还不好分心,生怕错过了“敬业福”.玩“咻一咻” ...

  10. Appium学习实践(五)遇到的坑(记录自己工作中遇到的坑以及解决方案,不定时更新)

    1.错误截图,有时候测试用例执行错误的话,相对于复杂的log,一张错误截图也许能更明确哪里出的问题(当然有时,截图+log还是最好了) 坑:本来是想测试用例fail的时候捕获异常来执行截图操作,但是由 ...