<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 閱讀(318) 評論(0)  編輯  收藏 所屬分類: Design
    主站蜘蛛池模板: 亚洲日韩在线中文字幕综合| 亚洲精品国产啊女成拍色拍| 噜噜综合亚洲AV中文无码| 中文字幕影片免费在线观看| 亚洲一卡二卡三卡| 青青草a免费线观a| 亚洲中文无码亚洲人成影院| 永久在线毛片免费观看| 福利片免费一区二区三区| 亚洲AV日韩精品一区二区三区| 免费人成再在线观看网站| 亚洲伊人久久综合影院| 久久久WWW成人免费精品| 亚洲AV无码乱码国产麻豆| 日本免费一区二区久久人人澡| 亚洲综合无码一区二区三区| 国产成人午夜精品免费视频| 亚洲av无码专区国产不乱码| 免费二级毛片免费完整视频| 中文字幕在线视频免费| 亚洲好看的理论片电影| 日本h在线精品免费观看| 中文字幕精品三区无码亚洲| mm1313亚洲精品无码又大又粗| 好猛好深好爽好硬免费视频| 亚洲黄色在线观看视频| 四虎成人免费网站在线| 成人精品综合免费视频| 日韩亚洲Av人人夜夜澡人人爽| 97视频热人人精品免费| 一级毛片一级毛片免费毛片| 亚洲人成网址在线观看| 日韩a在线观看免费观看| 国产精品玖玖美女张开腿让男人桶爽免费看| 亚洲精品成人网站在线观看| 91在线视频免费看| 中国内地毛片免费高清| ass亚洲**毛茸茸pics| 久久99亚洲综合精品首页 | 国内精品免费视频自在线| 午夜在线免费视频|