Live2d Test Env

SSH项目Dao层和Service层及Action的重用

泛型

1、泛型的定义   

1、泛型是一种类型
    1、关于Type
            //是一个标示接口,该标示接口描述的意义是代表所有的类型
        public interface Type {
        }
    2、Type的分类
          Class<T>
          ParameterizedType  泛型
          ......

    2、泛型的结构

    public Person<T>{
    
    }
    public interface ParameterizedType extends Type {
        Type[] getActualTypeArguments();  <T>
        Type getRawType();                Person
        Type getOwnerType();              Person<T>
    }

    3、参数的传递

        1、第一种
        ArrayList<E>
        ArrayList<Person> al = new ArrayList<Person>();  在执行该代码的时候就把Person传递给E了
    2、第二种情况
        public interface BaseDao<T>{
        
        }
        public class BaseDaoImpl<T> implements BaseDao<T>{
        
        }
        public class PersonDaoImpl extends BaseDaoImpl<Person>{}

各个类的组成

 

  1、BaseDao       

对crud的接口进行了抽象设计

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

public interface BaseDao<T>{
    public void saveEntry(T t);
    public void deleteEntry(Serializable id);
    public void updateEntry(T t);
    public Collection<T> queryEntry();
    public T getEntryById(Serializable id);
    public Set<T> getEntrysByIds(Serializable[] ids);
}
BaseDao.java

2、BaseDaoImpl       

对crud做一个公共的实现

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;

import com.itheima09.oa.dao.base.BaseDao;

/**
 * 该类不能被实例化
 * @author zd
 *
 * @param <T>
 */
public abstract class BaseDaoImpl<T> implements BaseDao<T>{
    @Resource(name="hibernateTemplate")
    public HibernateTemplate hibernateTemplate;
    
    private Class entityClass; //实体bean的class形式
    //持久化类的 标示符的名称
    private String identifierPropertyName;
    
    /**
     * 该init方法是由spring容器来调用的
     */
    @PostConstruct
    public void init(){
        /*
         * 获取到实体bean的标示符的属性的名称
         */
        this.identifierPropertyName = this.hibernateTemplate.getSessionFactory()
            .getClassMetadata(entityClass)
            .getIdentifierPropertyName();
    }
    
    public BaseDaoImpl(){
        //this代表具体的类的对象
        //this.getClass().getGenericSuperclass() = BaseDaoImpl<T>
        /**
         * 如果该类被实例化,则this代表BaseDaoImpl的对象
         *    this.getClass就是该对象的字节码的形式
         *    this.getClass().getGenericSuperclass()代表该对象的父类即Object
         *       所以这行代码得出的是一个Class而不是一个ParameterizedType
         */
        ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
        //得到参数的部分
        this.entityClass = (Class)type.getActualTypeArguments()[0];
        System.out.println(type.getRawType());//rawType=BaseDaoImpl
    }
    
    @Override
    public void saveEntry(T t) {
        // TODO Auto-generated method stub
        this.hibernateTemplate.save(t);
    }

    @Override
    public void deleteEntry(Serializable id) {
        // TODO Auto-generated method stub
        T t = (T)this.hibernateTemplate.get(this.entityClass, id);
        this.hibernateTemplate.delete(t);
    }

    @Override
    public void updateEntry(T t) {
        // TODO Auto-generated method stub
        this.hibernateTemplate.update(t);
    }

    @Override
    public Collection<T> queryEntry() {
        // TODO Auto-generated method stub
        return this.hibernateTemplate.find("from "+this.entityClass.getName());
    }

    @Override
    public T getEntryById(Serializable id) {
        // TODO Auto-generated method stub
        return (T)this.hibernateTemplate.get(this.entityClass, id);
    }
    
    public Set<T> getEntrysByIds(Serializable[] ids){
        StringBuffer buffer = new StringBuffer();
        buffer.append("from "+this.entityClass.getName());
        buffer.append(" where "+this.identifierPropertyName+" in(");
        for(int i=0;i<ids.length;i++){
            if(i==ids.length-1){
                buffer.append(ids[i]);
            }else{
                buffer.append(ids[i]+",");
            }
        }
        buffer.append(")");
        return new HashSet<T>(this.hibernateTemplate.find(buffer.toString()));
    }
}
BaseDaoImpl.java

  3、PersonDao       

是一个具体的dao 

import com.itheima09.oa.dao.base.BaseDao;
import com.itheima09.oa.domain.Person;

public interface PersonDao extends BaseDao<Person>{
    
}
PersonDao.java

4、PersonDaoImpl    

    是一个具体的dao的实现

import org.springframework.stereotype.Repository;

import com.itheima09.oa.dao.PersonDao;
import com.itheima09.oa.dao.base.impl.BaseDaoImpl;
import com.itheima09.oa.domain.Person;

@Repository("personDao")
public class PersonDaoImpl extends  BaseDaoImpl<Person> implements PersonDao{
    
}
PersonDaoImpl.java

  5、BaseService

        对crud进行声明

public interface BaseService<T> {
    public void saveEntry(T t);
    public void deleteEntry(Serializable id);
    public void updateEntry(T t);
    public Collection<T> queryEntry();
    public T getEntryById(Serializable id);
}
BaseService.jav

  6、BaseServiceImpl

        调用baseDao,对BaseService进行crud的实现

import java.io.Serializable;
import java.util.Collection;

import org.springframework.transaction.annotation.Transactional;

import com.itheima09.oa.dao.base.BaseDao;
import com.itheima09.oa.service.base.BaseService;

public abstract class BaseServiceImpl<T> implements BaseService<T>{
    
    //声明一个抽象方法,用于子类进行实现
    public abstract BaseDao<T> getBaseDao();
    
    @Transactional
    public void saveEntry(T t) {
        // TODO Auto-generated method stub
        this.getBaseDao().saveEntry(t);
    }

    @Transactional
    public void deleteEntry(Serializable id) {
        // TODO Auto-generated method stub
        this.getBaseDao().deleteEntry(id);
    }

    @Transactional
    public void updateEntry(T t) {
        // TODO Auto-generated method stub
        this.getBaseDao().updateEntry(t);
    }

    @Transactional(readOnly=true)
    public Collection<T> queryEntry() {
        // TODO Auto-generated method stub
        return this.getBaseDao().queryEntry();
    }

    @Transactional(readOnly=true)
    public T getEntryById(Serializable id) {
        // TODO Auto-generated method stub
        return this.getBaseDao().getEntryById(id);
    }
}
BaseServiceImpl.java

  7、PersonService

import com.itheima09.oa.domain.Person;
import com.itheima09.oa.service.base.BaseService;

public interface PersonService extends BaseService<Person>{
}
PersonService.java

  8、PersonServiceImpl

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.itheima09.oa.dao.PersonDao;
import com.itheima09.oa.dao.base.BaseDao;
import com.itheima09.oa.domain.Person;
import com.itheima09.oa.service.PersonService;
import com.itheima09.oa.service.base.impl.BaseServiceImpl;

@Service("personService")
public class PersonServiceImpl extends BaseServiceImpl<Person> implements PersonService{
    @Resource(name="personDao")
    private PersonDao personDao;
    
    @Override
    public BaseDao<Person> getBaseDao() {
        // TODO Auto-generated method stub
        return this.personDao;
    }
}
PersonServiceImpl.java

 9、 BaseAction

import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import org.springframework.beans.BeanUtils;

import com.itheima09.oa.service.base.BaseService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

    //public abstract BaseService<T> getBaseService();
    
    private Class modelDriverClass;
    private Long id;
    
    //跳转到列表页面的常量
    public static final String LISTACTION = "listAction";
    //跳转到更新页面的常量
    public static final String UPDATEUI = "updateUI";
    //跳转到增加页面的常量
    public static final String ADDUI = "addUI";
    //action跳转到action
    public static final String ACTION2ACTION = "action2action";
    
    public String listAction = LISTACTION;
    public String addUI = ADDUI;
    public String updateUI = UPDATEUI;
    public String action2action = ACTION2ACTION;
    
    public void setId(Long id) {
        this.id = id;
    }

    private T t;
    
    public BaseAction() {
        //获取 BaseAction<T>
        ParameterizedType type = (ParameterizedType)this.getClass().getGenericSuperclass();
        //获取T的class形式
        this.modelDriverClass = (Class)type.getActualTypeArguments()[0];
        try {
            this.t = (T) this.modelDriverClass.newInstance();//为t创建对象
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    
    @Override
    public T getModel() {
        // TODO Auto-generated method stub
        return this.t;
    }
    
    /**
     * 查询
     */
    public String showData(){
        //Collection<T> dataList = this.getBaseService().queryEntry();
        //System.out.println(dataList.size());
        ActionContext.getContext().put("dataList", null);
        return "list";
    }
    
    /**
     * 跳转到增加的页面
     */
    public String addUI(){
        return "addUI";
    }
    
    /**
     * 增加
     */
    public String add() throws Exception{
        Object obj = this.modelDriverClass.newInstance();
        BeanUtils.copyProperties(this.getModel(), obj);
        T t = (T)obj;
        //this.getBaseService().saveEntry(t);
        return "action2action";
    }
    
    /**
     * 跳转到修改的页面
     */
//    public String updateUI(){
//        //T t = this.getBaseService().getEntryById(this.id);
//        ActionContext.getContext().getValueStack().push(t);
//        return "updateUI";
//    }
}
BaseAction.java

10、PersonAction

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima09.oa.domain.Person;
import com.itheima09.oa.service.PersonService;
import com.itheima09.oa.service.base.BaseService;
import com.itheima09.oa.struts2.action.base.BaseAction;

@Controller("personAction")
@Scope("prototype")
public class PersonAction extends BaseAction<Person>{
    
    @Resource(name="personService")
    private PersonService personService;

//    @Override
//    public BaseService<Person> getBaseService() {
//        // TODO Auto-generated method stub
//        return this.personService;
//    }

}
PersonAction.java

 

类与类之间的关系 

  类实现了某一个接口
  类继承了某一个类
  引用

posted @ 2018-08-22 00:15  麦奇  阅读(1110)  评论(0编辑  收藏  举报