45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:MVC与三层架构的详细介绍

MVC与三层架构的详细介绍

2016-09-02 11:26:46 来源:www.45fan.com 【

MVC与三层架构的详细介绍

一、MVC架构
Struts是一个不错的MVC架构,我一直以来都用它,通过简单的配置即可将view,controler,和Model结合起来。View主要以JSP来实现,因为它是面向标签的,所以对于网页设计人员提供了很好的接口。FormBean是介于JSP和Action之间的中间数据载体,它肩负着数据从JSP到ACTION的传递过程。Action是流程的中转站,不同的业务在不同的Action中以不同的Model调用来实现。Model就是实现具体业务的操作过程,不过这种过程是一种在较高水平上的实现。
总之,MVC架构实现了三层结构中的两层,即表现层和业务层,另外还有一层被称之为持久化层。
二、三层架构
正如以上所说的,三层架构即“表现层”,“业务层”,“持久化层”。表现层实现的代表作品是Struts框架,业务层实现的代表作品是Spring,持久层实现的代表作品是Hibernate。不过在我的开发中Spring被省掉了,因为我认为业务过于简单,还不至于用Spring来实现。下面我将具体的来说说我的三层实现。
1、三种Bean
在我的实现中,总共有三种Bean,它们都是为保存对象状态而存在的。持久层中,这种Bean就是POJO对象。它是面向数据库的,或者说多少得照顾到数据库的实现,因为我习惯于用PowerDesigner来设计数据库,做出来的结构也是比较中规中矩的,而如果直接用Hibernate来实数据库的话,就不那么干净了。所以POJO对象的设计就不能完全面向对象。业务层中的Bean我把它称之为Entity,这是一个完全面向程序逻辑的Bean。它可能是好几个POJO的集合。对于业务逻辑来,这种被称之为Entity的Bean做到了“拿来就用”,很便于业务的进行。在显示层中的Bean就是FormBean了,主要用于传入数据所用。
POJO仅生存于持久化层,到了业务层就将数据交给Entity了。而Entity不仅生存于业务层,它还被传到了JSP中,用于显示。
2、Pojo与Dao
我认为这是数据与操作的关系,Dao只在呼操作,而Pojo仅用于保存数据。下面是我Dao接口的实个现。
public interface Dao {

//********** operattion method *********
void resetPO(Pojo pojo);//重新设置POJO实体
String save();//保存或更新POJO实体
Pojo load();//通过ID值获取确切的POJO
List find(short by);//通过某一属性值来获取POJO
void close();//显示调用,以关闭session,至此该DAO将不再可用。
}
所有的涉及具体操作的Dao类均实现它。三个方法把Hibernate的查询语句封装起来。在业务层中只关心save(),load(),或find()。下面是一个具体UserDao的实现。
public class UserDao implements Dao {

public final short _ALL = 0;
public final short _BY_NAME = 1;
public final short _BY_ACCOUNT = 2;
public final short _BY_PASSWORD = 3;
public final short _BY_ACCOUNT_PASSWORD = 4;

private UserPo userPo;
private static Log log = LogFactory.getLog(DocumentDAO.class);
private Session session;

//************ initial method ***************
public UserDao() {
userPo = new UserPo();
session = HibernateUtil.currentSession();
}

public UserDao(UserPo user){
this.userPo = user;
session = HibernateUtil.currentSession();
}

//************ method begin ***************

/**
* resetPO()
*重新加载POJO对象
* @param pojo Pojo
*/
public void resetPO(Pojo pojo) {
this.userPo = (UserPo)pojo;
}


/**
* save()
* 保存UserPo对象于数据库,如果是一个已持久的对象,则进行update操作
* @return String
*/
public String save() {
String oid = null;
try{
//Session session = HibernateUtil.currentSession() 已经被提至构造函数中初始化了
Transaction tx = session.beginTransaction();
if (userPo.getId() == null) {
oid = (String)session.save(userPo);//如果这是一个新的pojo则进行insert操作。
session.flush();
tx.commit();
}else{
session.update(userPo,userPo.getId());//如果该pojo已被初始化过则进行update操作
session.flush();
tx.commit();
}
}catch(HibernateException ex){
System.out.println("//********* error **********///n UserDao.save()出现异常!");
log.error("UserDao.save()出现异常!",ex);
}
return oid;
}

/**
* get()
* 通过id值,从数据库中获得指定的UserPo对象
* @return Pojo
*/
public Pojo load() {
UserPo tmp = new UserPo();
try{
//Session session = HibernateUtil.currentSession() 已经被提至构造函数中初始化了
tmp = (UserPo) session.get(UserPo.class, userPo.getId()); //用确切存在的ID值获得对象
}catch(HibernateException hbe){
hbe.printStackTrace();
}
if (tmp != null) {
userPo = tmp;
return userPo;
}
else
return null;
}

/**
* find()
* 从数据库中查找一个User对象
* @return Pojo
*/
public List find(short by) {
//Session session = HibernateUtil.currentSession() 已经被提至构造函数中初始化了
Query query = null;
String where=null;
switch(by){
case 0 :
where = "";
break;
case 1 :
where = " where us.name='"+userPo.getName()+"'";
break;
case 2 :
where = " where us.account='"+userPo.getAccount()+"'";
break;
case 3 :
where = " where us.password='"+userPo.getPassword()+"'";
break;
case 4 :
where = " where us.account='"+userPo.getAccount()+"' and us.password='"+userPo.getPassword()+"'";
}
query = session.createQuery("from UserPo us"+where);
return query.list();
}

public void close(){
HibernateUtil.closeSession();
}

}
其中HibernateUtil是一个Hibernate方法类,它提供线程安全的Session。和关闭这个Session的方法(虽然关闭Session过于简单)。每个Dao是由DaoFactory来产生的。DaoFactory也有一个公共的接口。如下:
public interface DaoFactory {
/**
* DAO对象工厂,获取各个数据实体的DAO对象,返回一个统一的接口 Dao
* @return Dao
*/
Dao getDocumentDAO();
Dao getDocHeadDAO();
Dao getDocAccessoryDAO();
Dao getUserDao(User u);

}
下面是一个基于Hibernate的Dao的实现。
public class HbDaoFactory implements DaoFactory {

public HbDaoFactory() {
}

/**
* getDocumentDao
*
* @return Dao
*/
public Dao getDocumentDAO() {
/**@todo Implement this com.cecs.dao.DaoFactory method*/
return new com.cecs.dao.DocumentDAO();
}

/**
* getDocHeadDao
*
* @return Dao
*/
public Dao getDocHeadDAO() {
/**@todo Implement this com.cecs.dao.DaoFactory method*/
throw new java.lang.UnsupportedOperationException("Method getDocHeadDAO() not yet implemented.");
}

/**
* getDocAccessoryDao
*
* @return Dao
*/
public Dao getDocAccessoryDAO() {
/**@todo Implement this com.cecs.dao.DaoFactory method*/
throw new java.lang.UnsupportedOperationException("Method getDocAccessoryDAO() not yet implemented.");
}

/**
* getUserDao
*获取UserDao对象
* @return Dao
*/
public Dao getUserDao(User u) {
return new UserDao(u);
}

}
这样一但不用Hibernate来实现持久层,也可以很方便改为其它的Dao而不用修改业务层。
 

本文地址:http://www.45fan.com/a/question/71165.html
Tags: MVC 三层 架构
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部