zoukankan      html  css  js  c++  java
  • 浅谈: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>

    希望对大家有帮助。

  • 相关阅读:
    android IntentService生命周期问题
    日志
    python for android : BeautifulSoup 有 bug
    光电耦合器简单介绍以及作用
    cocos2dx 3.1从零学习(五)——动画
    openssl之EVP系列之9---EVP_Digest系列函数的一个样例
    html5 SVG
    CSS选择器
    ISCC2014-reverse
    哇塞!HTML5 实现的雨滴效果 CSS发抖
  • 原文地址:https://www.cnblogs.com/yghjava/p/5715386.html
Copyright © 2011-2022 走看看