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

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

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

    linansengling

     

    Spring JPetStore(二) JPetStore的分析

    ??Spring JPetStore總體架構(gòu)屬于三層結(jié)構(gòu). 分為業(yè)務(wù)服務(wù)層? 表現(xiàn)層? 數(shù)據(jù)訪問(wèn)層
    ??
    ?????? 業(yè)務(wù)服務(wù)層使用由POJO(java類)實(shí)現(xiàn), 它們運(yùn)行在輕量級(jí)的容器Spring中. 輕量級(jí)容器的功能在于,?一 管理業(yè)務(wù)對(duì)象的生命周期. 二 借助于Spring?的IOC(Inversion of Control, 控制反轉(zhuǎn))功能來(lái)完成對(duì)象之間的依賴關(guān)系, 而不用編程一顯示完成. 三 借助于Spring的AOP(Aspect-Oriented Programming, 面向方面的程序設(shè)計(jì))?為運(yùn)行在容器中的對(duì)象提供一些企業(yè)服務(wù), 比如聲明式的事務(wù)管理. 業(yè)務(wù)層放棄了EJB而使用運(yùn)行在輕量級(jí)容器的服務(wù)層, 會(huì)有以下好處:
    1.? 能夠在Servlet引擎中運(yùn)行. 不用EJB容器的服務(wù)器, tomcat就能搞定, 軟件的費(fèi)用低, 容易管理, 負(fù)載小.
    2?. 容易在不同的應(yīng)用服務(wù)器或serverlet引擎之間移植. 要想達(dá)到高可移植性, 對(duì)于EJB容器來(lái)說(shuō)要比web容器困難, 比如, 就需要確保不同的EJB容器在啟動(dòng)時(shí)都會(huì)運(yùn)行某一些代碼.
    3.??實(shí)現(xiàn)更為簡(jiǎn)單.
    4. 不需要那么累贅的部署文件.
    不好的地方:
    1.? 缺乏對(duì)遠(yuǎn)程調(diào)用的內(nèi)置支持.
    2.? 缺乏一個(gè)標(biāo)準(zhǔn)的環(huán)境, 用于容納, 管理業(yè)務(wù)對(duì)象.
    3.? 沒(méi)有清晰的業(yè)務(wù)層.
    4.? 在不同的應(yīng)用系統(tǒng)之間缺乏一致性, 第個(gè)系統(tǒng)都可能會(huì)有自己的一套做法: 怎么訪問(wèn)業(yè)務(wù)對(duì)象?怎么解決事務(wù)管理 怎么訪問(wèn)數(shù)據(jù)等.
    雖然如此但現(xiàn)在我們有了spring一切就有了解決之道了.

    ????? 表現(xiàn)層中, Spring JPetStore提供了兩種不同的web層實(shí)現(xiàn), 二者都要依靠同一個(gè)中間層, 一個(gè)是基于Structs的,另一個(gè)則是Spring的MVC框架. 二者都是基于JSTL的JSP視圖.

    ????? 數(shù)據(jù)訪問(wèn)層,使用了J2EE模式中的"數(shù)據(jù)訪問(wèn)對(duì)象"(Data Access Object, DAO),?他用一個(gè)DAO接口隱藏了持久化操作的細(xì)節(jié), 這樣使用這個(gè)模式的業(yè)務(wù)對(duì)象無(wú)需知道底層的持久化技術(shù)的細(xì)節(jié). Spring JPetStore中使用了iBATIS框架.

    下面讓我們來(lái)看一下應(yīng)用中的細(xì)節(jié)內(nèi)容吧, 先在Eclipse下把應(yīng)用加進(jìn)來(lái)方便調(diào)試, 運(yùn)行.

    Eclipse可到(http://www.eclipse.org/downloads/上下載)他是壓縮包解壓后就可用(可以再去下MyEclipe http://www.myeclipse.com是Eclipese的開(kāi)發(fā)插件方便開(kāi)發(fā), 不過(guò)要付費(fèi)可以下載它的破解文件http://jinxinxin.bokee.com/inc/myeclipse_keygens.rar?MyEclipse的安裝如果不會(huì)去Google一下吧). 好了開(kāi)發(fā)工具安裝后新建一工程jpetstore, 然后用..\spring-jpetstore\samples\jpetstore下的src目錄復(fù)蓋你的eclipse工作空間下的\jpetstore下的src目錄,然后再把..\spring-jpetstore\samples\jpetstore下的war目錄下的全部?jī)?nèi)容拷到你的eclipse工作空間下的\jpetstore下的WebRoot目錄下復(fù)蓋WEB-INF.回到eclipse下刷新工程你可看到如下:

    e.jpg



    先從數(shù)據(jù)訪問(wèn)層說(shuō)起吧, 他使用iBATIS框架來(lái)訪問(wèn)數(shù)據(jù)庫(kù),在..\spring-jpetstore\samples\jpetstore\db目錄下有它的各種數(shù)據(jù)庫(kù)的schema有hsql, mysql, oracle, postges你使用拿一種數(shù)據(jù)庫(kù)是通過(guò)..\spring-jpetstore\samples\jpetstore\war\WEB-INF下的jdbc.properties來(lái)配置的.默認(rèn)的是hsql數(shù)據(jù)庫(kù):
    # Properties file with JDBC-related settings.
    # Applied by PropertyPlaceholderConfigurer from "dataAccessContext-local.xml".
    # Targeted at system administrators, to avoid touching the context XML files.

    jdbc.driverClassName=org.hsqldb.jdbcDriver
    jdbc.url=jdbc:hsqldb:hsql://localhost:9002
    jdbc.username=sa
    jdbc.password=
    要改為用mysql只要改為:
    #jdbc.driverClassName=org.hsqldb.jdbcDriver
    #jdbc.url=jdbc:hsqldb:hsql://localhost:9002
    #jdbc.username=sa
    #jdbc.password=

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/jpetstore
    jdbc.username=root
    jdbc.password=

    即可.業(yè)務(wù)層能過(guò)...jpetstore.dao包下的接口來(lái)訪問(wèn)....jpetstore.dao.ibatis包從而通過(guò)iBATIS框架訪問(wèn)數(shù)據(jù)庫(kù)中的數(shù)據(jù)的部份代碼如下:

    package org.springframework.samples.jpetstore.dao;
    public interface AccountDao {

    ? Account getAccount(String username, String password) throws DataAccessException;

    ? ...........

    }

    package org.springframework.samples.jpetstore.dao.ibatis;
    public class SqlMapAccountDao extends SqlMapDaoSupport implements AccountDao {

    ??? public Account getAccount(String username, String password) throws DataAccessException {
    ??? Account account = new Account();
    ??? account.setUsername(username);
    ??? account.setPassword(password);
    ??? return (Account) getSqlMapTemplate().executeQueryForObject("getAccountByUsernameAndPassword", account);
    ? }
    ..............
    }

    Account.xml

    <mapped-statement name="getAccountByUsernameAndPassword" result-map="result">
    ??? select
    ????? SIGNON.USERNAME as USERID,
    ????? ACCOUNT.EMAIL,
    ????? ACCOUNT.FIRSTNAME,
    ????? ACCOUNT.LASTNAME,
    ????? ACCOUNT.STATUS,
    ????? ACCOUNT.ADDR1,
    ????? ACCOUNT.ADDR2,
    ????? ACCOUNT.CITY,
    ??????..???
    ??? from ACCOUNT, PROFILE, SIGNON, BANNERDATA
    ??? where ACCOUNT.USERID = #username#

    ????? and SIGNON.PASSWORD = #password#
    ????? and SIGNON.USERNAME = ACCOUNT.USERID
    ????? and PROFILE.USERID = ACCOUNT.USERID
    ????? and PROFILE.FAVCATEGORY = BANNERDATA.FAVCATEGORY
    ? </mapped-statement>

    然后到了業(yè)務(wù)層, 業(yè)務(wù)層為表達(dá)層提供服務(wù),操縱數(shù)據(jù)層來(lái)完成業(yè)務(wù)邏輯比如從數(shù)據(jù)庫(kù)中讀出客啟信息傳給表達(dá)層 向數(shù)據(jù)庫(kù)中插入訂單等.業(yè)務(wù)層還要完成對(duì)數(shù)據(jù)庫(kù)操作的完整性,正確性即事務(wù)管理. 此應(yīng)用是通過(guò)Spring的AOP來(lái)完成無(wú)需編程實(shí)現(xiàn)如下:

    dataAccessContex-local.xml

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    ??<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
    ??<property name="url"><value>${jdbc.url}</value></property>
    ??<property name="username"><value>${jdbc.username}</value></property>
    ??<property name="password"><value>${jdbc.password}</value></property>
    ?</bean>

    ?<!-- Transaction manager for a single JDBC DataSource -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    ? <property name="dataSource">
    ?? <ref local="dataSource" />
    ? </property>
    ? <property name="mappingResources">
    ?? <list>
    ??? <value>org/springframework/samples/jpetstore/Hibernate/Account.hbm.xml</value>
    ?? </list>
    ? </property>
    ? <property name="hibernateProperties">
    ?? <props>
    ??? <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    ??? <prop key="hibernate.show_sql">true</prop>
    ?? </props>
    ? </property>
    ?</bean>
    ?
    ?<!-- (see dataAccessContext-jta.xml for an alternative) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    ? <property name="sessionFactory">
    ?? <ref local="sessionFactory" />
    ? </property>
    ?</bean>

    ?<!-- SqlMap setup for iBATIS Database Layer -->
    ?<bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapFactoryBean">
    ??<property name="configLocation"><value>WEB-INF/sql-map-config.xml</value></property>
    ?</bean>


    ?<!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->

    ?<bean id="accountDao" class="org.springframework.samples.jpetstore.dao.hibdaoimp.AccountDaoImp">
    ? <property name="sessionFactory">
    ?? <ref local="sessionFactory" />
    ? </property>
    ? ?
    ?</bean>
    ................

    </beans>

    applicationContex.xml

    <bean id="petStoreTarget" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
    ??<property name="accountDao"><ref bean="accountDao"/></property>
    ??<property name="categoryDao"><ref bean="categoryDao"/></property>
    ??<property name="productDao"><ref bean="productDao"/></property>
    ??<property name="itemDao"><ref bean="itemDao"/></property>
    ??<property name="orderDao"><ref bean="orderDao"/></property>
    ?</bean>

    ?<!-- Transactional proxy for the JPetStore primary business object -->
    ?<bean id="petStore" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    ??<property name="transactionManager"><ref bean="transactionManager"/></property>
    ??<property name="target"><ref local="petStoreTarget"/></property>
    ??<property name="transactionAttributes">
    ???<props>
    ????<prop key="insert*">PROPAGATION_REQUIRED</prop>
    ????<prop key="update*">PROPAGATION_REQUIRED</prop>
    ????<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    ???</props>
    ??</property>
    ?
    從上面的配置代碼我們可看到通過(guò)Spring的IOC完成了org.springframework.samples.jpetstore.domain.logic.PetStoreImpl等類的注入. 通過(guò)AOP用org.springframework.transaction.interceptor.TransactionProxyFactoryBean來(lái)完成事務(wù)代理
    業(yè)務(wù)層通過(guò)一個(gè)門(mén)面(facada)PetStoreFacade.java接口來(lái)為表達(dá)層提供服務(wù):

    package org.springframework.samples.jpetstore.domain.logic;
    public interface PetStoreFacade {

    ?Account getAccount(String username);

    ?Account getAccount(String username, String password);

    ?void insertAccount(Account account);

    ?void updateAccount(Account account);

    ..........
    }
    用PetStoreImp.java來(lái)實(shí)現(xiàn):

    package org.springframework.samples.jpetstore.domain.logic;

    public class PetStoreImpl implements PetStoreFacade, OrderService {

    ? private AccountDao accountDao;

    ?.........

    ?public void setAccountDao(AccountDao accountDao) {
    ??this.accountDao = accountDao;
    ?}


    ?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);
    ? }

    ? .......
    }

    表達(dá)層這里介紹Structs,Structs中通過(guò)PetStoreFacade接口來(lái)訪問(wèn)業(yè)務(wù)層:

    package org.springframework.samples.jpetstore.web.struts;

    public abstract class BaseAction extends Action {

    ? private PetStoreFacade petStore;

    ?public void setServlet(ActionServlet actionServlet) {
    ??super.setServlet(actionServlet);
    ??ServletContext servletContext = actionServlet.getServletContext();
    ??WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    ??this.petStore = (PetStoreFacade) wac.getBean("petStore");
    ?}

    ?protected PetStoreFacade getPetStore() {
    ??return petStore;
    ?}

    }


    public class SignonAction extends BaseAction {

    ? public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    ???????AccountActionForm acctForm = (AccountActionForm) form;
    ????? String username = acctForm.getUsername();
    ????? String password = acctForm.getPassword();
    ????? Account account = getPetStore().getAccount(username, password);
    .....
    ??????????????? return mapping.findForward("success");
    ????}
    Structs框架通過(guò)struct-config.xml文來(lái)控制相關(guān)映射轉(zhuǎn)發(fā)的:????

    struct-config.xml???????

    <action path="/signon" type="org.springframework.samples.jpetstore.web.struts.SignonAction"
    ???name="accountForm" scope="request"
    ???validate="false">
    ???<forward name="success" path="/index.jsp"/>
    ??</action>

    關(guān)于jsp就不在多說(shuō)了下面再來(lái)看一下它的web.xml文件:

    ??<context-param>
    ??<param-name>contextConfigLocation</param-name>
    ??<param-value>
    ???/WEB-INF/dataAccessContext-local.xml? /WEB-INF/applicationContext.xml
    ??</param-value>
    ???</context-param>

    <servlet>
    ??<servlet-name>context</servlet-name>
    ??<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    ??<load-on-startup>1</load-on-startup>
    ?</servlet>

    ?<servlet>
    ??<servlet-name>action</servlet-name>
    ??<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    ??<load-on-startup>3</load-on-startup>
    ?</servlet>

    ?? ??<servlet-mapping>
    ???<servlet-name>action</servlet-name>
    ??
    ??<url-pattern>*.do</url-pattern>
    ?</servlet-mapping>

    ? 還有關(guān)于遠(yuǎn)程機(jī)制有Caucho的Hessian(一個(gè)借助HTTP的二進(jìn)制協(xié)議) Burlap(一個(gè)基于XML的借助HTTP的傳輸協(xié)議), Apache Axis提供的JAX-RPC(基于SOAP的借助HTTP傳輸?shù)膚eb serverice), 還有基于RMI的遠(yuǎn)程調(diào)用.
    想了解更多還是自已細(xì)看里面的代碼吧!
    請(qǐng)繼續(xù)關(guān)注Spring JPetStore(三) 在其上實(shí)現(xiàn)自已的Sunlight Netstore

    posted on 2006-09-17 12:16 fds 閱讀(4273) 評(píng)論(6)  編輯  收藏 所屬分類: JAVA

    評(píng)論

    # re: Spring JPetStore(二) JPetStore的分析 2006-09-19 23:24 Supergoal

    對(duì)于JPetStore,我倒也研究過(guò)一點(diǎn)。總感覺(jué)它的業(yè)務(wù)層設(shè)計(jì)的不是很合理,僅僅通過(guò)一個(gè)Facade來(lái)對(duì)Dao進(jìn)行了封裝,稍簡(jiǎn)陋了一些。還有就是,數(shù)據(jù)分頁(yè)的功能都是在表示層里完成了,對(duì)于一個(gè)數(shù)據(jù)量較小的系統(tǒng)來(lái)說(shuō)還能接受,如果數(shù)據(jù)量大的話,像它這種將所有結(jié)果集都保存在HttpSession中的做法實(shí)現(xiàn)是不敢恭維。  回復(fù)  更多評(píng)論   

    # re: Spring JPetStore(二) JPetStore的分析 2006-09-20 08:46 逆流的魚(yú)

    你好,我希望要一份代碼,謝謝。我的msn:ywg_2008@hotmail.com
    希望共同學(xué)習(xí)(java開(kāi)發(fā)一年,主要用spring+hibernate(ibatis)+struts)。
    郵箱是 ywg2008@sohu.com  回復(fù)  更多評(píng)論   

    # re: Spring JPetStore(二) JPetStore的分析 2006-09-20 23:20 周先有

    @逆流的魚(yú)
    需要源碼和發(fā)布幫助請(qǐng)看:
    http://m.tkk7.com/linansengling/archive/2006/09/16/70041.html  回復(fù)  更多評(píng)論   

    # re: Spring JPetStore(二) JPetStore的分析 2006-09-21 00:48 周先有

    @Supergoal
    我也有同感, 你的分析很對(duì)當(dāng)數(shù)據(jù)量大, 或是用戶很多時(shí)JPetstore存在這樣的問(wèn)題, 我的Java分頁(yè)技術(shù)的應(yīng)用實(shí)現(xiàn), 應(yīng)該能解決這種問(wèn)題
    http://m.tkk7.com/linansengling/archive/2006/09/21/70960.html
    要不就用jdbc來(lái)控制有點(diǎn)煩不知大家是怎么搞定的  回復(fù)  更多評(píng)論   

    # re: Spring JPetStore(二) JPetStore的分析 2007-01-14 14:51 qiuguang

    我想要份源程序
    qiugaungmail@yahoo.com.cn
    謝謝!  回復(fù)  更多評(píng)論   

    # re: Spring JPetStore(二) JPetStore的分析[未登錄](méi) 2009-11-04 10:40 leon

    好啊報(bào)錯(cuò)  回復(fù)  更多評(píng)論   

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(3)

    隨筆分類(12)

    隨筆檔案(13)

    文章分類(1)

    文章檔案(2)

    相冊(cè)

    收藏夾(3)

    my like

    最新隨筆

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 国产精品手机在线亚洲| 亚洲美女高清一区二区三区| 国产成人免费在线| 91精品免费不卡在线观看| 免费人妻无码不卡中文字幕系| 国产一级a毛一级a看免费视频| a级片免费观看视频| 久久九九久精品国产免费直播| 亚洲高清免费视频| 中国极品美軳免费观看| 两个人看的www高清免费视频| 女同免费毛片在线播放| 日韩视频在线观看免费| 香港a毛片免费观看 | 亚洲熟女综合一区二区三区| 亚洲xxxx视频| 在线亚洲v日韩v| fc2免费人成为视频| a毛看片免费观看视频| 久久国产精品成人片免费| 最近免费2019中文字幕大全| 在线看片v免费观看视频777| 思思99re66在线精品免费观看| 麻豆精品国产免费观看| 日韩亚洲精品福利| 亚洲精品白浆高清久久久久久| 亚洲国产美国国产综合一区二区 | 久久亚洲中文字幕精品一区| 亚洲日韩精品一区二区三区无码| 久久久久久亚洲精品| 亚洲国产精品成人综合色在线婷婷 | 免费A级毛片无码免费视| 在线免费观看色片| 精品国产人成亚洲区| 亚洲国产精品自在线一区二区| 亚洲av产在线精品亚洲第一站| 亚洲AV电影天堂男人的天堂| 久久久久久久久久免免费精品| 精品熟女少妇a∨免费久久| 成人免费无毒在线观看网站| 久久亚洲精品无码观看不卡|