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
类与类之间的关系
类实现了某一个接口
类继承了某一个类
引用
SSH项目Dao层和Service层及Action的重用的更多相关文章
- 搭建DAO层和Service层代码
第一部分建立实体和映射文件 1 通过数据库生成的实体,此步骤跳过,关于如何查看生成反向工程实体类查看SSH框架搭建教程-反向工程章节 Tmenu和AbstractorTmenu是按照数据库表反向工程形 ...
- 分层 DAO层,Service层,Controller层、View层
前部分摘录自:http://blog.csdn.net/zdwzzu2006/article/details/6053006 DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务 ...
- Java中Action层、Service层、Modle层和Dao层的功能区分
一.Java中Action层.Service层.Modle层和Dao层的功能区分: 首先,这是现在最基本的分层方式,结合了SSH架构. modle层就是对应的数据库表的实体类.(即domain) Da ...
- java中Action层、Service层和Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...
- DAO层,Service层,Controller层、View层 的分工合作
DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...
- [转]DAO层,Service层,Controller层、View层
来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...
- DAO层,Service层,Controller层、View层
DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...
- DAO层,Service层,Controller层、View层介绍
来自:http://jonsion.javaeye.com/blog/592335 DAO层 DAO 层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DA ...
- DAO层,Service层,Controller层、View层协同工作机制
转自 http://www.blogdaren.com/post-2024.html DAO层:DAO层主要是做数据持久层的工 作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计D ...
- DAO层,Service层,Controller层、View层、entity层
1.DAO(mapper)层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就 ...
随机推荐
- Eclipse配置C++11环境详细介绍
转:https://blog.csdn.net/wgxh05/article/details/54021049 本文记录Eclipse配置C++11开发所有作者遇到的情况,包括跨工程文件编译,内联文件 ...
- Springboot项目搭建(3)-shiro登录
shiro简述+实现简单登录:https://www.jianshu.com/p/7f724bec3dc3
- PHP基础学习笔记3
一.检索表单信息 PHP 中的 $_GET 和 $_POST 变量用于检索表单中的信息,比如用户输入 提交的表单: <form action="welcome.php" me ...
- GO学习之 安装Go语言及搭建Go语言开发环境
一.下载 1.下载地址 Go官网下载地址:https://golang.org/dl/ Go官方镜像站(推荐):https://golang.google.cn/dl/ 2.版本的选择 Windows ...
- 【题解】Rusty String [CF827E]
[题解]Rusty String [CF827E] 传送门:\(\text{Rusty String}\) \(\text{[CF827E]}\) [题目描述] 多组数据,每组数据给出一个由 \(V, ...
- 20-02-27 hive表的几个问题
1.hive表的动态分区 2.hive 表如何修改列名 3.group by 对统计指标的影响 (group by 的本质) 4.row_number 对数据的影响
- 【C语言】创建一个函数,并调用比较两个数的大小
#include <stdio.h> int max(int x,int y) { if(x>=y) return x; else return y; } main() { int ...
- P1217
最快的办法就是打表了...不然怎么都会TLE. 先计算出给定最大范围内的所有回文质数: #include <bits/stdc++.h> using namespace std; #def ...
- C语言:将3*5矩阵中第k列的元素左移到第0列,第k列以后的每列元素依次左移,原来左边的各列依次绕到右边。-在m行m列的二维数组中存放如下规律的数据,
//将3*5矩阵中第k列的元素左移到第0列,第k列以后的每列元素依次左移,原来左边的各列依次绕到右边. #include <stdio.h> #define M 3 #define N 5 ...
- 【渗透测试】Squirrelmail远程代码执行漏洞+修复方案
最近网上有点不太平,爆出各种漏洞,等下会把近期的漏洞复现一下,发出来.安全圈的前辈总是默默的奉献,在这里晚辈们只能站在巨人的肩膀上,跟紧前辈们的步伐,走下去. -------------------- ...