<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-314  評論-209  文章-0  trackbacks-0

    雖然對 Spring 不熟悉,又不懂 iBatis ,而且對模式的概念還沒有弄清楚,但也硬著頭皮去讀 Spring 包自帶的 Jpetstore 經典 J2EE 例子。

    可以肯定, Jpetstore 是按照 MVC 模式設計的。持久化層用 iBatis (這個我不懂,我希望是用 Hibernate ), web 層控制器的 servlet 有兩個選擇,一個是用 Struts ,另一個是 Spring MVC

    以下是自己的閱讀體會,也許分析不當或描述不清,但也算初步嘗試,所以記下來了。

    一,分層結構

    Jpetstore 使用了門面模式、單例模式, DAO 模式。

    ?

    1. 門面模式

    門面接口的實現類: PetStoreImpl

    public class PetStoreImpl implements PetStoreFacade, OrderService

    {

    ??? private AccountDao accountDao;

    ??? private CategoryDao categoryDao;

    ??? private ProductDao productDao;

    ??? private ItemDao itemDao;

    ??? private OrderDao orderDao;

    ?

    ??? // ----------------------------------------------------------------

    ??? // Setter methods for dependency injection

    ??? // ----------------------------------------------------------------

    ?

    ??? public void setAccountDao(AccountDao accountDao)

    ??? {

    ??????? this.accountDao = accountDao;

    ??? }

    ??? // 省略余下的四個setter

    ??? // -------------------------------------------------------------------------

    ??? // Operation methods, implementing the PetStoreFacade interface

    ??? // -------------------------------------------------------------------------

    ?

    ??? public Account getAccount(String username)

    ??? {

    ??????? return this.accountDao.getAccount(username);

    ??? }

    ??? public Account getAccount(String username, String password)

    ??? {

    ??????? return this.accountDao.getAccount(username, password);

    ??? }

    ??? public void insertAccount(Account account)

    ??? {

    ??????? this.accountDao.insertAccount(account);

    ??? }

    ??? public void updateAccount(Account account)

    ??? {

    ??????? this.accountDao.updateAccount(account);

    ??? }

    ??? // 省略其它的crud方法

    }

    ?

    暫時先不管 OrderService 這個接口。

    PetStoreImpl 的那些 setter 方法正是 spring 的注入方法。

    在配置文件中:

    < bean id ="petStore" class ="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">

    ??? < property name ="accountDao" ref ="accountDao" />

    ??? < property name ="categoryDao" ref ="categoryDao" />

    ??? < property name ="productDao" ref ="productDao" />

    ??? < property name ="itemDao" ref ="itemDao" />

    ??? < property name ="orderDao" ref ="orderDao" />

    </ bean >

    ?

    2. 單例模式

    單例模式中,我們一般把類的構造方法設置為 private ,提供靜態工廠方法給外界返回唯一的實例,但在這里,它不是這樣做的,因為有了 Spring 。有了 Spring BeanFactory 管理,可以輕易配置實現單例??纯醋髡叩淖⑨?。

    ?

    There is one instance of this class in the JPetStore application. In Spring terminology, it is a "singleton". This means a per-Application Context singleton. The factory creates a single instance; there is no need for a private constructor, static factory method etc as in the traditional implementation of the Singleton Design Pattern.

    ?

    單例的 PetStoreImpl

    Struts 當控制器時,它這樣做:為整個應用程序編寫一個繼承自 Action BaseAction 基礎類。

    ?

    public abstract class BaseAction extends Action

    {

    ??? private PetStoreFacade petStore;

    ?

    ??? public void setServlet(ActionServlet actionServlet)

    ??? {

    ??????? super.setServlet(actionServlet);

    ??????? if (actionServlet != null)

    ??????? {

    ??????????? ServletContext servletContext = actionServlet.getServletContext();

    ??????????? WebApplicationContext wac = WebApplicationContextUtils

    ??????????????? .getRequiredWebApplicationContext(servletContext);

    ??????????? this.petStore = (PetStoreFacade) wac.getBean("petStore");

    ??????? }

    ??? }

    ?

    ??? protected PetStoreFacade getPetStore()

    ??? {

    ??????? return petStore;

    ??? }

    }

    ?

    3.DAO 模式

    ORM 工具用 iBatis ,在領域模式層使用了粗粒度對象。下面是 AccountDao 的配置。

    ?

    < select id ="getAccountByUsername" resultMap ="result">

    ??? select

    ????????? signon.username as userid,

    ????????? account.email,

    ????????? account.firstname,

    ????????? account.lastname,

    ????????? account.status,

    ????????? account.addr1,

    ????????? account.addr2,

    ????????? account.city,

    ????????? account.state,

    ????????? account.zip,

    ????????? account.country,

    ????????? account.phone,

    ????????? profile.langpref,

    ????????? profile.favcategory,

    ????????? profile.mylistopt,

    ????????? profile.banneropt,

    ? ???????? bannerdata.bannername

    ??? from account, profile, signon, bannerdata

    ??? where account.userid = #value#

    ????? and signon.username = account.userid

    ????? and profile.userid = account.userid

    ????? and profile.favcategory = bannerdata.favcategory

    ? </ select >

    posted on 2006-11-02 09:41 xzc 閱讀(309) 評論(0)  編輯  收藏 所屬分類: Design
    主站蜘蛛池模板: 男女男精品网站免费观看| 国产成人人综合亚洲欧美丁香花| 黄色短视频免费看| 亚洲成aⅴ人片久青草影院| 狼人大香伊蕉国产WWW亚洲| 国产精品免费看久久久无码| 亚洲精品动漫免费二区| 日本大片在线看黄a∨免费| 亚洲а∨天堂久久精品9966| 免费无码A片一区二三区| 亚洲日韩亚洲另类激情文学| 国产精品麻豆免费版| 精品一区二区三区无码免费直播| 亚洲av麻豆aⅴ无码电影| 成人免费av一区二区三区| 亚洲成av人影院| 国产成人免费在线| 亚洲精品色播一区二区| 亚洲国产精品视频| 日韩电影免费在线观看网站 | 阿v视频免费在线观看| 亚洲另类激情专区小说图片| 国内精品99亚洲免费高清| 亚洲色图在线播放| 精品久久免费视频| 高清永久免费观看| 亚洲啪啪免费视频| 国产yw855.c免费视频| 中文在线日本免费永久18近| 亚洲精品自拍视频| 国产一级一片免费播放i| 十八禁在线观看视频播放免费| 亚洲国产精品人久久电影| 免费jjzz在线播放国产| a级日本高清免费看| 亚洲 暴爽 AV人人爽日日碰| 亚洲国产精品尤物yw在线| 最刺激黄a大片免费网站| 婷婷亚洲综合五月天小说在线| 久久精品国产精品亚洲下载| 亚洲免费网站在线观看|