淺談: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>

 

希望對大家有幫助。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM