DAO设计模式和Servlets [英] DAO Design Pattern and Servlets

查看:137
本文介绍了DAO设计模式和Servlets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Oracle的网站上阅读DAO设计模式,我正在使用JSP,Servlet,纯Java对象和MVC模式的上下文中了解下图。在我的情况下,BusinessObject是我的servlet,TransferObject是我的java类,只有属性,mutator和访问器(DTO)?



例如,如果我有一个servlet(控制器)中的代码

  DTO.setFirstName(request.getParameter(firstName)); 
DTO.setLastName(request.getParameter(lastName));
DAO.save(DTO);

http://java.sun.com/blueprints/corej2eepatterns/Patterns/images09/figure09_02.jpg

解决方案

几乎。处理演示逻辑的控制器和处理数据访问逻辑的DAO之间应该有一个包含业务对象的业务层。



主要职责这些业务对象是向控制器提供业务服务。




  • 他们是一个门面

  • 来封装应用程序的业务逻辑

  • 以划分交易

  • 以使用一个或几个DAO来获取,查找和保留对象。



此图层非常重要,因为您希望能够在单个事务中对数据库执行多个操作。 Web控制器不应该负责处理这个问题。此外,其他客户端可以使用与Web控制器(Swing客户端,批处理等)相同的业务服务。



业务对象通常使用会话EJB或春季服务。



他们也很有用,能够




  • 单位通过嘲笑业务对象来测试控制器

  • 单元通过嘲笑DAO来测试业务逻辑


I am reading about the DAO design pattern on Oracle's website and I'm trying to understand the below image in the context of using JSP's, Servlets, plain java objects, and the MVC pattern. In my case would the BusinessObject be my servlet and the TransferObject be my java class with only properties, mutators, and accessors (DTO)?

For example, if I had this code in a servlet (controller)

DTO.setFirstName(request.getParameter("firstName"));
DTO.setLastName(request.getParameter("lastName"));
DAO.save(DTO);

http://java.sun.com/blueprints/corej2eepatterns/Patterns/images09/figure09_02.jpg

解决方案

Almost. Between the controller, which handles presentation logic, and the DAO, which handles Data access logic, there should be a business layer, containing the business objects.

The main responsibilities of these business objects are

  • to provide business services to the controllers. They are a facade
  • to encapsulate the business logic of the application
  • to demarcate transacations
  • to use one or several DAOs to get, find and persist objects.

This layer is very important because you want to be able to perform several operations on your database within a single transaction. And it should not be the responsibility of the web controller to handle this. Moreover, the same business services could be used by other clients than the web controllers (Swing client, batch, etc.)

Business objects are typically implemented using session EJBs, or Spring services.

They're also useful to be able to

  • unit test the controller by mocking the business objects
  • unit test the business logic by mocking the DAOs

这篇关于DAO设计模式和Servlets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆