泛型

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

类与类之间的关系

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

SSH项目Dao层和Service层及Action的重用的更多相关文章

  1. 搭建DAO层和Service层代码

    第一部分建立实体和映射文件 1 通过数据库生成的实体,此步骤跳过,关于如何查看生成反向工程实体类查看SSH框架搭建教程-反向工程章节 Tmenu和AbstractorTmenu是按照数据库表反向工程形 ...

  2. 分层 DAO层,Service层,Controller层、View层

    前部分摘录自:http://blog.csdn.net/zdwzzu2006/article/details/6053006 DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务 ...

  3. Java中Action层、Service层、Modle层和Dao层的功能区分

    一.Java中Action层.Service层.Modle层和Dao层的功能区分: 首先,这是现在最基本的分层方式,结合了SSH架构. modle层就是对应的数据库表的实体类.(即domain) Da ...

  4. java中Action层、Service层和Dao层的功能区分

    Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...

  5. DAO层,Service层,Controller层、View层 的分工合作

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  6. [转]DAO层,Service层,Controller层、View层

    来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...

  7. DAO层,Service层,Controller层、View层

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  8. DAO层,Service层,Controller层、View层介绍

    来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...

  9. DAO层,Service层,Controller层、View层协同工作机制

    转自 http://www.blogdaren.com/post-2024.html DAO层:DAO层主要是做数据持久层的工 作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计D ...

  10. DAO层,Service层,Controller层、View层、entity层

    1.DAO(mapper)层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就 ...

随机推荐

  1. SVN代码迁移到GITlab

    ==================================================================================================== ...

  2. c# pcm

    using System; using System.IO; using System.Text; using System.Windows.Forms; using System.Runtime.I ...

  3. Java内存模型(JMM)那些事

    本文是库存文章,去年年底学习了慕课网的并发编程课程,今年年初看完了<深入理解Java虚拟机>这本书,但是很多内容忘得差不多了,打算写写博客回忆一下那些忘在脑后的知识点. 温故而知新 更多J ...

  4. Shiro&Jwt验证

    此篇基于 SpringBoot 整合 Shiro & Jwt 进行鉴权 相关代码编写与解析 首先我们创建 JwtFilter 类 继承自 BasicHttpAuthenticationFilt ...

  5. 爬虫 - Scrapy中间件

    前提:看Scrapy架构图 不管什么Middlewares,都写在middlewares.py里面. 然后在settings.py里的DOWNLOADER_MIDDLEWARES或者SPIDER_MI ...

  6. 03-Spring的IOC示例程序(通过类型获取对象)

    根据bean类型从IOC容器中获取bean的实例 ①test测试类 @Test public void Test02() { //获取spring容器对象 ApplicationContext app ...

  7. css常见问题汇总

    1. 如果我想显示两行文字第二行超出部分‘...’? 限制在一个块元素显示的文本的行数. -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit proper ...

  8. 7、源与值(Source/Values)

    学习目录:树莓派学习之路-GPIO Zero 官网地址:https://gpiozero.readthedocs.io/en/stable/source_values.html 环境:UbuntuMe ...

  9. html解析のBeautifulSoup

    引子: 使用python爬虫对爬取网页进行解析的时候,如果使用正则表达式,有很多局限,比如标签中出现换行,或者标签的格式不规范,都有可能出现取不到数据,BeautifulSoup作为一个专门处理htm ...

  10. 火爆微信朋友圈的Excel速成班视频课程

    Excel速成班视频课程,一共有10节课,附带课件. 目录结构如下: 目录:/2020032-Excel速成班视频 [4.6G] ┣━━课件 [1.9M] ┃ ┣━━第八课Excel实用技巧12例.x ...