<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 經(jīng)典 J2EE 例子。

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

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

    一,分層結(jié)構(gòu)

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

    ?

    1. 門面模式

    門面接口的實現(xiàn)類: 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. 單例模式

    單例模式中,我們一般把類的構(gòu)造方法設(shè)置為 private ,提供靜態(tài)工廠方法給外界返回唯一的實例,但在這里,它不是這樣做的,因為有了 Spring 。有了 Spring BeanFactory 管理,可以輕易配置實現(xiàn)單例。看看作者的注釋。

    ?

    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 當(dāng)控制器時,它這樣做:為整個應(yīng)用程序編寫一個繼承自 Action BaseAction 基礎(chǔ)類。

    ?

    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 ,在領(lǐng)域模式層使用了粗粒度對象。下面是 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 閱讀(317) 評論(0)  編輯  收藏 所屬分類: Design
    主站蜘蛛池模板: 女性自慰aⅴ片高清免费| 国产成人免费在线| 免费观看国产精品| 亚洲最大av资源站无码av网址| 91精品免费国产高清在线| 亚洲精品中文字幕无码AV| 国产又大又粗又长免费视频 | 日韩免费一区二区三区| 亚洲国产成人综合精品| 性做久久久久免费看| 色噜噜的亚洲男人的天堂| 亚洲国产成人a精品不卡在线| 国产免费MV大全视频网站| 亚洲爆乳精品无码一区二区三区| 国精产品一区一区三区免费视频| 日韩va亚洲va欧洲va国产| 99在线免费观看视频| 亚洲 日韩 色 图网站| 日韩精品视频免费网址| 一级免费黄色毛片| 亚洲AV综合色一区二区三区| 91精品免费久久久久久久久| 亚洲欧美国产国产综合一区 | 成人奭片免费观看| 羞羞网站免费观看| 亚洲高清国产AV拍精品青青草原| 91香蕉国产线观看免费全集| 亚洲情A成黄在线观看动漫软件 | 亚洲自偷自偷图片| 57pao一国产成视频永久免费| 亚洲中文字幕乱码熟女在线| 大胆亚洲人体视频| 99视频在线免费看| 国产成人综合久久精品亚洲| 亚洲av午夜成人片精品网站| 女性无套免费网站在线看| a级毛片在线免费| 亚洲熟妇无码一区二区三区| 亚洲色精品88色婷婷七月丁香| 99久久99久久精品免费看蜜桃| 羞羞的视频在线免费观看|