浅谈:java泛型与dao重用
在进入今天的主题之前,我们先理解一下什么是泛型:
泛型是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重用的更多相关文章
- 浅谈Java泛型之<? extends T>和<? super T>的区别
关于Java泛型,这里我不想总结它是什么,这个百度一下一大堆解释,各种java的书籍中也有明确的定义,只要稍微看一下就能很快清楚.从泛型的英文名字Generic type也能看出,Generic普通. ...
- 浅谈Java——泛型DAO
首先解释一下为什么要学习泛型DAO.平时在写DAO的时候是一个接口对应一个实现类,实现类里面要写很多的操作数据库的方法.当我们有很多的javaben的时候我们会写很多的接口和实现类,并且里面的代码都是 ...
- 浅谈Java泛型中的extends和super关键字(转)
通配符 在本文的前面的部分里已经说过了泛型类型的子类型的不相关性.但有些时候,我们希望能够像使用普通类型那样使用泛型类型: 向上造型一个泛型对象的引用 向下造型一个泛型对象的引用 向上造型一个泛型对象 ...
- 浅谈Java泛型中的extends和super关键字
泛型是在Java 1.5中被加入了,这里不讨论泛型的细节问题,这个在Thinking in Java第四版中讲的非常清楚,这里要讲的是super和extends关键字,以及在使用这两个关键字的时候为什 ...
- 浅谈Java泛型中的? extends E和?super E
https://blog.csdn.net/zymx14/article/details/78073757
- 浅谈java类集框架和数据结构(2)
继续上一篇浅谈java类集框架和数据结构(1)的内容 上一篇博文简介了java类集框架几大常见集合框架,这一篇博文主要分析一些接口特性以及性能优化. 一:List接口 List是最常见的数据结构了,主 ...
- 龙生九子-浅谈Java的继承
龙生九子-浅谈Java的继承 书接上回,我们之前谈过了类和对象的概念,今天我来讲一讲面向对象程序设计的另外一个基本概念-继承 目录 为什么需要继承 自动转型与强制转型 继承能干啥 复写和隐藏 supe ...
- 浅谈Java的反射机制和作用
浅谈Java的反射机制和作用 作者:Java大师 欢迎转载,转载请注明出处 很多刚学Java反射的同学可能对反射技术一头雾水,为什么要学习反射,学习反射有什么作用,不用反射,通过new也能创建用户对象 ...
- 浅谈Java的throw与throws
转载:http://blog.csdn.net/luoweifu/article/details/10721543 我进行了一些加工,不是本人原创但比原博主要更完善~ 浅谈Java异常 以前虽然知道一 ...
随机推荐
- 在Ubuntu上单机安装Hadoop
最近大数据比较火,所以也想学习一下,所以在虚拟机安装Ubuntu Server,然后安装Hadoop. 以下是安装步骤: 1. 安装Java 如果是新机器,默认没有安装java,运行java –ver ...
- MongoDBV3.0.7版本(shard+replica)集群的搭建及验证
集群的模块介绍: 从MongoDB官方给的集群架构了解,整个集群主要有4个模块:Config Server.mongs. shard.replica set: Config Server:用来存放集群 ...
- linux命令详解:md5sum命令
前言 在网络传输.设备之间转存.复制大文件等时,可能会出现传输前后数据不一致的情况.这种情况在网络这种相对更不稳定的环境中,容易出现.那么校验文件的完整性,也是势在必行的. 使用说明 md5sum命令 ...
- Keystone 命令汇总
Keystone 命令汇总 目录 [隐藏] 1 用户(User) 1.1 查看用户列表 1.2 创建用户 1.3 删除用户 1.4 显示用户详细信息 1.5 更新用户的密码 1.6 赋予用户一个角 ...
- Neteans 切换用户语言为英语
Go to Netbeans installation directory, for example, C:\\Program Files\\NetBeans 6.0.1\\etc Open &quo ...
- 【小白的CFD之旅】07 CFD常识
学了一周的流体力学,小白对于流体力学有了基本的了解,但是流体力学涵盖的内容何其之多,一周的时间怎么可能学得好呢,很多的概念都是模棱两可.为了在一个月之后能够应用CFD,小白又找到了黄师姐. “师姐,看 ...
- Eclipse导入MyEclipse创建的web项目报错的解决方法
将myeclipse中开发的动态web项目直接引入到eclipse中继续开发,Eclipse中会报项目有错,如下图
- Libevent初探
Libevent 是一个用C语言编写的.轻量级的开源高性能网络库,主要有以下几个亮点:事件驱动( event-driven),高性能;轻量级,专注于网络,不如 ACE 那么臃肿庞大:源代码相当精炼.易 ...
- [Template]高精度模板
重新写一下高精度模板(不要问我为什么) 自认为代码风格比较漂亮(雾 如果有更好的写法欢迎赐教 封装结构体big B是压位用的进制,W是每位长度 size表示长度,d[]就是保存的数字,倒着保存,从1开 ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...