日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Jdon框架开发指南

發(fā)布時(shí)間:2025/7/25 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Jdon框架开发指南 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Jdon框架快速開發(fā)指南

開發(fā)主要步驟如下:

  • JdonFramework6.0以上兩步開發(fā)見(jiàn)這里。
  • 快速配置指南
  • 新增/查詢/修改/刪除(CRUD);
  • 批量查詢和分頁(yè)顯示
  • 本文Step By Step詳細(xì)講解如何使用Jdon框架基于領(lǐng)域模型快速開發(fā)這兩個(gè)功能,通過(guò)Jdon框架的可以快速完成系統(tǒng)原型(ArcheType),使得開發(fā)者將真正精力集中在每個(gè)項(xiàng)目系統(tǒng)的特殊業(yè)務(wù)處理。

    本案例源碼下載

    按這里查看更詳細(xì)全面文檔

    快速配置指南

    Jdon框架有一個(gè)配置文件叫jdonframework.xml,其中配置的是我們編寫的Java類,格式如下:

    <pojoService name="給自己類取的名稱" class="完整類的名稱"/>

    配置有兩個(gè)基本項(xiàng):name和class,class中寫全POJO的全名;name是供代碼中調(diào)用這個(gè)服務(wù)的名稱。

    或者使用Annotation注解@Service或@Component,就無(wú)需上面這個(gè)配置。

    假如我們編寫了一個(gè)類TestServicePOJOImp,代碼簡(jiǎn)要如下:

    //@Service("testService")
    public class TestServicePOJOImp implements TestService{

    ???private JdbcDAO jdbcDao;

    ???public TestServicePOJOImp(JdbcDAO jdbcDao) {
    ?????????this.jdbcDao = jdbcDao;
    ???}

    ???public void createUser(EventModel em) {
    ??????....
    ???}
    ?}

    接口TestService代碼:

    public interface TestService {

    ???void createUser(EventModel em);

    }




    上面TestServicePOJOImp代碼創(chuàng)建完成后,下面有兩個(gè)并行步驟:

    1. 如果在TestServicePOJOImp類前加上@Service注解,就可以了,無(wú)需再XML配置。

    2.如果你為了更加松耦合,在未來(lái)更換類時(shí),不再重新編譯源碼,那么可以采取XML配置這個(gè)步驟,我們?cè)谠创a目錄需要?jiǎng)?chuàng)建一個(gè)叫jdonframework.xml配置文件,內(nèi)容如下:


    <?xml?version="1.0" encoding="UTF-8"?>
    <!DOCTYPE app PUBLIC "-//JDON//DTD Framework 2005 1.0 //EN" "http://www.jdon.com/jdonframework.dtd">
    <app>
    ???<services>
    ??????<pojoService name="testService" ???????????????????????????class="com.jdon.framework.test.service.TestServicePOJOImp"/>
    ???</services>
    </app>


    這樣,在servlet或jsp或struts的action等客戶端代碼中,我們可以使用如下代碼調(diào)用TestServicePOJOImp,注意:以下代碼沒(méi)有具體TestServicePOJOImp類:


    TestService testService = (TestService) WebAppUtil.getService("testService?", request);
    testService.createUser(em);

    ?

    以上步驟,只是簡(jiǎn)單展示框架的一個(gè)簡(jiǎn)要步驟,你可能沒(méi)有覺(jué)得多了一個(gè)jdonframework.xml以后,好像比平常代碼沒(méi)什么不同,關(guān)鍵是:如果我們需要使用AnotherTestServicePOJOImp更換原來(lái)的TestServicePOJOImp類,只需要更改jdonframework.xml文件,而無(wú)須更改客戶端代碼,也無(wú)須重新編譯項(xiàng)目了。

    ?

    ?

    當(dāng)然,還有另外一個(gè)優(yōu)點(diǎn),就是Ioc/DI依賴注射,細(xì)心的人已經(jīng)注意到TestServicePOJOImp有一個(gè)構(gòu)造參數(shù)如下:
    ???public TestServicePOJOImp(JdbcDAO jdbcDao) {
    ?????????this.jdbcDao = jdbcDao;
    ???}
    如果不傳入JdbcDAO實(shí)例,我們?nèi)绾文茉诳蛻舳舜a中直接創(chuàng)建TestServicePOJOImp實(shí)例呢?原來(lái)只要我們?cè)趈donframework.xml中再配置一個(gè)JdbcDAO類,概時(shí)框架就會(huì)自動(dòng)幫我們創(chuàng)建JdbcDAO實(shí)例,并且傳入TestServicePOJOImp實(shí)例中。
    新的jdonframework.xml內(nèi)容如下:


    <?xml?version="1.0" encoding="UTF-8"?>
    <!DOCTYPE app PUBLIC "-//JDON//DTD Framework 2005 1.0 //EN" "http://www.jdon.com/jdonframework.dtd">
    <app>
    ???<services>
    ??????<pojoService name="testService" ???????????????????????????class="com.jdon.framework.test.service.TestServicePOJOImp"/>

    ??????<!-- 新增加的配置:jdbcDAO是被TestServiceImp調(diào)用的,是為其服務(wù)的。 -->
    ??????<component name="jdbcDAO" class="com.jdon.framework.test.dao.JdbcDAO"/>
    ???</services>
    </app>


    再進(jìn)一步,如果我們經(jīng)常遇到一些類中需要一些常量或參數(shù)定義,那么可以使用如下配置:


    <component name="jdbcDAO" class="com.jdon.framework.test.dao.JdbcDAO">
    ?????<constructor value="java:/TestDS"/>
    </ component >?

    這時(shí),要求JdbcDAO構(gòu)造參數(shù)有一個(gè)字符串和參數(shù),這樣constructor的值java:/TestDS就在JdbcDAO被創(chuàng)建時(shí)注射到它的實(shí)例中。 JdbcDAO代碼如下:

    public class JdbcDAO{

    ......?
    ???public JdbcDAO(String jndiName){
    ?????????System.out.println("jndiname" = jndiName);?
    ?????????......

    ???}

    ......?
    }

    原來(lái)圖如下(圖中UserReposotoryInMEN相當(dāng)于JdbcDAO HellpServiceImp相當(dāng)于TestServicePOJOImp):

    啟動(dòng)Jdon框架

    有了jdonframework.xml,我們需要在項(xiàng)目中啟動(dòng)它,有兩種啟動(dòng)方式:一個(gè)是結(jié)合struts的struts-config.xml配置方式;另外一個(gè)是不結(jié)合struts的web.xml配置方式。


    第一:web.xml配置方式:
    如果你不使用Struts,可以通過(guò)web.xml下列配置來(lái)啟動(dòng)Jdon框架。


    <context-param>
    ???<param-name>?modelmapping-config </param-name>
    ???<param-value> jdonframework.xml?</param-value>
    </context-param>
    ……
    <listener>
    ???<listener-class>com.jdon.container.startup.ServletContainerListener</listener-class>
    </listener>

    上述param-value可以配置多個(gè)配置,中間以逗號(hào)隔開,如下:



    <context-param>
    ???<param-name>modelmapping-config </param-name>
    ???<param-value>com.jdon.framework.test.model.models.xml,
    ???????????????????????????com.jdon.framework.test.service.services.xml,
    ???????????????????????????com.jdon.framework.test.dao.dao.xml</param-value>??
    </context-param>?

    第二:結(jié)合struts配置方式(需要struts基礎(chǔ)知識(shí)):

    struts-config.xml中配置Plugin實(shí)現(xiàn)子類:


    <plug-in className="com.jdon.strutsutil.InitPlugIn">
    ???<set-property property="modelmapping-config" value="jdonframework.xml" />
    </plug-in>

    按這里查看更詳細(xì)全面文檔

    ?

    增刪改查(CRUD)和批量分頁(yè)查詢是每個(gè)系統(tǒng)的基本功能,下面分這兩部分描述。

    CRUD開發(fā)步驟

    說(shuō)明:每個(gè)應(yīng)用系統(tǒng)中存在大量重復(fù)的CRUD開發(fā)流程,通過(guò)本框架可快速完成這些基本基礎(chǔ)工作量,將精力集中在特殊功能設(shè)計(jì)上。 CRUD快速開發(fā)主要簡(jiǎn)化了表現(xiàn)層的流程,將其固化,或者是模板化,以配置替代代碼編制,靈活而快速。每個(gè)Model一套固化CRUD流程。

    CRUD開發(fā)步驟分兩個(gè)部分:代碼編寫 、配置。

    CRUD代碼:

    代碼只需要三步:?
    1、域建模:建立UserTest類如下:

    //@Model
    public class UserTest extends?Model?{
    ??? private String userId;
    ??? private String name;

    ???public String getName() { return name; }
    ???public void setName(String name) { this.name = name; }

    ???.......
    }

    注意點(diǎn):

    • 模型必須以@Model標(biāo)注
    • 或者繼承框架的com.jdon.controller.model.Model,或者實(shí)現(xiàn)com.jdon.controller.model.ModelIF接口。
    • 該模型類必須有一個(gè)能夠標(biāo)識(shí)其對(duì)象唯一性的主鍵,如userId,這個(gè)主鍵相當(dāng)于數(shù)據(jù)表的主鍵,這個(gè)規(guī)定符合Evans DDD規(guī)定。

    為了激活 Domain Model的對(duì)象實(shí)例駐留內(nèi)存緩存中,在持久層模型類創(chuàng)建時(shí),加入下面標(biāo)注:

    @Component()
    @Introduce("modelCache")
    public class UserDAOJdbc implements UserRepository{

    ???@Around
    ???public UserTest getUser(String Id) {

    ????????...... //fetch from key-value stores or relation DB

    ???}

    }

    ?? DCI:數(shù)據(jù)Data, 場(chǎng)景Context, 交互Interactions是由MVC發(fā)明者Trygve Reenskaug發(fā)明的。 見(jiàn)?DCI架構(gòu)是什么??DCI讓我們的核心模型更加簡(jiǎn)單,只有數(shù)據(jù)和基本行為。業(yè)務(wù)邏輯等交互行為在角色模型中 在運(yùn)行時(shí)的場(chǎng)景,將角色的交互行為注射到數(shù)據(jù)中。

    ? ?? JdonFramework的Domain Events是DCI的交互行為,在實(shí)現(xiàn)領(lǐng)域事件的同時(shí)也實(shí)現(xiàn)了DCI。

    ? ?? 為更清楚說(shuō)明DCI,下面以JdonFramework案例說(shuō)明。

    ? ?? 領(lǐng)域模型是DCI的Data,只有數(shù)據(jù)和基本行為,更簡(jiǎn)單,但注意不是只有setter/getter的貧血模型。如下:

    ?

    @Model
    public class UserModel {

    ? ?? private String userId;
    ? ?? private String name;

    ? ?? @Inject
    ? ?? private ComputerRole computerRole;

    ?

    ?

    ? ?? Domain Events事件或消息的生產(chǎn)者也就是DCI中的角色Role,比如我們有一個(gè)專門進(jìn)行計(jì)數(shù)計(jì)算的角色,實(shí)際上真正計(jì)算核心因?yàn)橐褂藐P(guān)系數(shù)據(jù)庫(kù)等底層技術(shù)架構(gòu),并不真正在此實(shí)現(xiàn),而是依托消息消費(fèi)者@Consumer實(shí)現(xiàn),那么消息生產(chǎn)者可以看出是一個(gè)接口,消費(fèi)者看成是接口的實(shí)現(xiàn):

    ?

    @Introduce("message")
    public class ComputerRole {

    ? ?? @Send("computeCount")
    ? ?? public DomainMessage computeCount(UserModel user) {
    ? ?? ? ?? return new DomainMessage(user);
    ? ?? }

    ? ?? @Send("saveUser")
    ? ?? public DomainMessage save(UserModel user) {
    ? ?? ? ?? return new DomainMessage(user);
    ? ?? }

    }

    ?

    DCI第三個(gè)元素是場(chǎng)景Context,在這個(gè)場(chǎng)景下,ComputeRole將被注入到模型UserModel中,實(shí)現(xiàn)計(jì)數(shù)計(jì)算的業(yè)務(wù)功能:

    public class ComputeContext {

    ? ??private DomainMessage ageAsyncResult;

    ? ?? public void preloadData(UserModel user) {
    ? ?? ? ?? if (ageAsyncResult == null)
    ? ?? ? ?? ? ?? ageAsyncResult = user.getUserDomainEvents().computeCount(user);
    ? ?? }

    ? ?? public int loadCountNow(UserModel user) {
    ? ?? ? ?? preloadData(user);
    ? ?? ? ?? return (Integer) ageAsyncResult.getEventResult();
    ? ?? }

    ? ?? public int loadCountByAsync(UserModel user) {
    ? ?? ? ?? if (ageAsyncResult == null)
    ? ?? ? ?? ? ?? ageAsyncResult = user.getUserDomainEvents().computeCount(user);
    ? ?? ? ?? else if (ageAsyncResult != null)
    ? ?? ? ?? ? ?? return (Integer) ageAsyncResult.getEventResult();
    ? ?? ? ?? return -1;

    ? ?? }

    }

    ?


    2、建立Model組件服務(wù):首先建立模型的服務(wù)接口TestService:

    public interface TestService {
    ???void createUser(EventModel em);?
    ???void updateUser(EventModel em);
    ?? void deleteUser(EventModel em);
    ?? UserTest getUser(String userId);
    }

    至于TestService的具體實(shí)現(xiàn)子類可以在現(xiàn)在或者以后建立

    3、建立Model的表現(xiàn)層邊界模型UserActionForm,必須繼承框架的ModelForm,如下:

    public class UserActionForm extends?ModelForm?{

    ???private String userId;
    ???private String name;

    ???public String getName() { return name; }
    ???public void setName(String name) { this.name = name; }

    ....
    }

    表現(xiàn)層UserActionForm內(nèi)容基本上是從業(yè)務(wù)層模型UserTest類中拷貝過(guò)來(lái)的,屬于界面對(duì)象,用來(lái)顯示或錄入數(shù)據(jù)。

    ?

    一個(gè)模型的CRUD實(shí)現(xiàn)的代碼工作到此結(jié)束,如果有其他模型,完全按照上述三個(gè)步驟再做一次,是不是不太費(fèi)腦筋?有點(diǎn)模板化開發(fā)味道?下面談?wù)凜RUD實(shí)現(xiàn)第二組成部分

    CRUD配置:

    兩個(gè)配置文件分別是:

    • 將前面三步編寫的類建立關(guān)系:jdonframework.xml
    • 配置界面流程:struts-config.xml

    一、Jdon框架配置文件

    首先我們將前面三步編寫的三個(gè)類:模型UserTest、服務(wù)TestService和界面模型UserActionForm建立起聯(lián)系,也就是告訴Jdon框架這三者是解決一個(gè)模型增刪改查CRUD功能實(shí)現(xiàn)的。 由于這個(gè)配置文件是告訴Jdon框架的,因此,我們?nèi)∶麨閖donframework.xml,當(dāng)然你也可以取其他名稱,無(wú)論取什么名稱,都要告訴Jdon框架,在struts-config.xml中配置:

    <plug-in className="com.jdon.strutsutil.InitPlugIn">
    ???<set-property property="modelmapping-config" ???????????????????????????value="jdonframework.xml" />
    </plug-in>

    jdonframework.xml配置內(nèi)容如下:


    <models>
    ???<model key="userId"?
    ?????????class ="com.jdon.framework.test.model.UserTest">
    ??????<!-- configuration about UI Form: UserActionForm -->
    ??????<actionForm name="userActionForm"/>
    ?????????<handler>
    ????????????<!-- configuration about the Model service : TestService -->
    ????????????<service ref="testService">?
    ??????????????????<getMethod name="getUser" />
    ??????????????????<createMethod name="createUser" />
    ??????????????????<updateMethod name="updateUser" />
    ??????????????????<deleteMethod name="deleteUser" />
    ?????????</service>
    ???????</handler>
    ???</model>

    ???......
    </models>
    <services>
    ???<!-- the Ioc configuration about TestService -->
    ???<pojoService name="testService"?
    ?????????class="com.jdon.framework.test.service.TestServicePOJOImp"/>

    ???......
    </services>

    以上配置是配置模型UserTest、模型服務(wù)TestService和界面模型UserActionForm三者關(guān)系的,下面詳細(xì)說(shuō)明三個(gè)部分的配置:

    1、模型UserTest的配置:
    這是通過(guò)第一行中的class值來(lái)指定當(dāng)前Model是com.jdon.framework.test.model.UserTest:

    <model?key="userId" class ="com.jdon.framework.test.model.UserTest">

    其中,UserTest模型的主鍵是userId,這個(gè)userId必須是UserTest類的一個(gè)字段;同時(shí)是用來(lái)唯一標(biāo)識(shí)唯一的UserTest模型對(duì)象,也就是Object ID,或者可以認(rèn)為是模型UserTest對(duì)應(yīng)的數(shù)據(jù)表的主鍵。

    2、界面模型UserActionForm配置:

    <actionForm name="userActionForm"/>

    可能你已經(jīng)注意到:這里并沒(méi)有寫界面模型完整類:com.jdon.framework.test.web.UserActionForm, 那么配置中userActionForm名稱是從哪里來(lái)的呢?是struts-config.xml中ActionForm定義名稱,如下:

    <struts-config>
    ???<form-beans>???
    ??????<form-bean name="userActionForm" ???????????????????????????type="com.jdon.framework.test.web.UserActionForm" />
    ??????……?
    ???</form-beans>
    …..?
    </struts-config>

    可見(jiàn)我們的界面模型完整類com.jdon.framework.test.web.UserActionForm是在struts-config.xml中form-beans中配置,并且命名為userActionForm,而這個(gè)userActionForm就是jdonframework.xml中的userActionForm。

    3、模型服務(wù)TestService配置:
    在jdonframework.xml中首先申明TestService完整實(shí)現(xiàn)是類com.jdon.framework.test.service.TestServicePOJOImp,并且取名為testService:
    <pojoService name="testService" class="com.jdon.framework.test.service.TestServicePOJOImp"/>
    這樣,我們就可以詳細(xì)將我們自己編寫的testService的CRUD方法名告訴Jdon框架了:


    <handler>
    ???<!-- this will refer to service: testService-->???
    ???<service ref="testService">?

    ?????????<!--getUser is the method name of testService -->
    ?????????<getMethod name="getUser" />

    ?????????<!--createUser is the method name of testService -->
    ?????????<createMethod name="createUser" />

    ?????????<!--updateUser is the method name of testService -->
    ?????????<updateMethod name="updateUser" />

    ????????? <!--deleteUser is the method name of testService -->
    ?????????<deleteMethod name="deleteUser" />

    ????? </service>
    </handler>

    黑體字部分正是testService所指的接口TestService四個(gè)方法,可見(jiàn)前面代碼步驟第二步。

    二、界面流程配置

    界面流程主要是配置CRUD界面流程,Jdon框架CRUD流程主要分兩個(gè)部分:第一是推出供用戶新增修改刪除的頁(yè)面;第二是接受用戶提交新增修改過(guò)的數(shù)據(jù),以便遞交到業(yè)務(wù)層保存。
    這部分配置主要是配置struts-config.xml:
    1、配置推出CRUD頁(yè)面流程:


    <action name="userActionForm" path="/userAction" ?????????type="com.jdon.strutsutil.ModelViewAction"
    ?????????scope="request" validate="false">
    ??????????????????<forward name="create" path="/user.jsp" />
    ??????????????????<forward name="edit" path="/user.jsp" />
    </action>

    其中com.jdon.strutsutil.ModelViewAction是Jdon框架類。只要客戶端瀏覽器調(diào)用http://localhost:8080/userAction.do,通過(guò)上述配置將激活forward的name=”create”流程,就能得到一個(gè)空白表單的頁(yè)面user.jsp;如果客戶端瀏覽器調(diào)用http://localhost:8080/userAction.do?action=edit&userId=18,通過(guò)上述配置將激活forward name=”edit”流程,得到一個(gè)填滿數(shù)據(jù)的表單頁(yè)面,供用戶修改。

    2、配置:接受用戶提交新增修改過(guò)的數(shù)據(jù),以便遞交到業(yè)務(wù)層保存:


    <html:form action="/userSaveAction.do" method="POST" >

    <html:hidden property="action"/> <!-- this is a rule -->

    userId:<html:text property="userId"/>
    <br>Name:<html:text property="name"/>
    <br><html:submit property="submit" value="Submit"/>
    </html:form>

    其實(shí)在上一步的user.jsp中已經(jīng)使用到這一步的配置,在user.jsp的表單action值就是本步配置的path值:/userSaveAction.do:


    <action name="userForm" path="/userSaveAction" ????????type="com.jdon.strutsutil.ModelSaveAction"
    ????????scope="request" validate="true" input="/user.jsp">
    ????????????????<forward name="success" path="/result.jsp" />
    ????????????????<forward name="failure" path="/result.jsp" />
    </action>

    在上面user.jsp中一定要有<html:hidden property="action"/>一行。至此,模型UserTest的CRUD功能開發(fā)完畢。


    批量分頁(yè)查詢實(shí)現(xiàn)

    批量分頁(yè)查詢開發(fā)步驟也分兩個(gè)部分:代碼編寫 、配置。

    批量查詢代碼實(shí)現(xiàn):

    代碼也分三步實(shí)現(xiàn)。
    1、表現(xiàn)層編寫一個(gè)查詢Action,繼承Jdon框架的com.jdon.strutsutil.ModelListAction,該類名稱為com.jdon.framework.test.web.UserListAction,完成getPageIterator和findModelByKey兩個(gè)方法。
    其中g(shù)etPageIterator方法內(nèi)容是業(yè)務(wù)層TestService的調(diào)用:


    TestService testService = (TestService) ?????????????????????????????????WebAppUtil.getService("testService",request);
    return testService.getAllUsers(start, count);

    所以TestService接口中必須有g(shù)etAllUsers這個(gè)方法,主要功能是返回PageIterator對(duì)象
    findModelByKey方法內(nèi)容也是業(yè)務(wù)層TestService的調(diào)用:


    TestService testService = (TestService) ???????????????????????????????WebAppUtil.getService("testService", request);
    return testService.getUser((String)key);

    TestService接口中必須有getUser方法。

    2、業(yè)務(wù)層實(shí)現(xiàn)TestService接口方法getAllUsers內(nèi)容,一般是直接調(diào)用持久層JdbcDao方法。

    3、持久層實(shí)現(xiàn)返回PageIterator對(duì)象:


    public PageIterator getUsers(int start, int count) throws Exception {
    ????????String GET_ALL_ITEMS_ALLCOUNT =
    ??????????????? "select count(1) from usertest ";//usertest是數(shù)據(jù)表名
    ????????String GET_ALL_ITEMS =?
    ???????????????"select userId from usertest ";//usertest是數(shù)據(jù)表名
    ????????return pageIteratorSolver. getPageIterator (GET_ALL_ITEMS_ALLCOUNT,?GET_ALL_ITEMS, "",start, count);
    }

    如果有參數(shù),可以如下查詢:


    public PageIterator getUsers(Long categoryId, int start, int count) {
    ???????String GET_ALL_ITEMS_ALLCOUNT =
    ??????????????????"select count(1) from usertest where categoryId = ? ";
    ???????String GET_ALL_ITEMS =?
    ???????????????????"select userId from usertest where categoryId = ? ";
    ???????Collection params = new ArrayList(1);
    ???????params.add(categoryId);//paramters will be put into Collection
    ???????return pageIteratorSolver.getPageIterator(GET_ALL_ITEMS_ALLCOUNT, ?????????????????????????????????????????????????GET_ALL_ITEMS, params, start, count);
    }

    批量查詢配置

    一、Jdon框架配置文件

    本步驟主要是需要告訴jdonframework.xml我們的TestService實(shí)現(xiàn)子類是什么,以及調(diào)用的JdbcDao等組件,jdonframework.xml如下:


    <services>
    ???????<pojoService name="testService" ??????????????????????????????class="com.jdon.framework.test.service.TestServicePOJOImp"/>
    ???????<component name="jdbcDAO" ??????????????????????????????class="com.jdon.framework.test.dao.JdbcDAO"/>?
    ???????<component name="constants" class="com.jdon.framework.test.Constants">
    ??????????????<constructor value="java:/TestDS"/>
    ???????</component>?
    </services>

    因?yàn)門estServicePOJOImp類中調(diào)用了JdbcDAO,JdbcDAO中又涉及JNDI名稱,所以它們之間依賴關(guān)系靠Jdon框架的IOC容器實(shí)現(xiàn)。TestServicePOJOImp必須有構(gòu)造器如下:


    public class TestServicePOJOImp implementsTestService{

    ???????private JdbcDAO jdbcDAO;

    ???????public TestServicePOJOImp(JdbcDAO jdbcDAO){

    ??????????????this.jdbcDAO = jdbcDAO;

    ???????}

    }

    二、界面流程配置

    這一步主要是struts-config.xml配置,和通常struts的ActionForm和Action配置類似:


    <form-beans>
    ……
    <form-bean name="listForm" type="com.jdon.strutsutil.ModelListForm" />
    </form-beans>

    其中com.jdon.strutsutil.ModelListForm是框架批量查詢特別使用的類。


    <action name="listForm" path="/userListAction"?
    ???????type="com.jdon.framework.test.web.UserListAction"?
    ???????scope="request">
    ??????????????<forward name="success" path="/userList.jsp" />
    </action>

    其中UserListAction是我們前面代碼編寫部分編寫的代碼。這樣,客戶端瀏覽器通過(guò)http://localhost:8080/userListAction.do就可以實(shí)現(xiàn)所有UserTest批量分頁(yè)查詢顯示。
    注意,userList.jsp中編碼和通常Struts的Jsp編碼是一樣的,需要使用logic:iterator從ActionForm為listForm的list字段中獲取單個(gè)的UserTest對(duì)象,然后顯示這些單個(gè)UserTest對(duì)象,,如下:


    <logic:iterate indexId="i" id="user" name="listForm" property="list" >

    ???????<bean:write name="user" property="name" />

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

    </logic:iterate

    在userList.jsp中加入下面標(biāo)簽庫(kù)可以自動(dòng)顯示多頁(yè),缺省一個(gè)頁(yè)面顯示30個(gè)條目。


    <MultiPages:pager actionFormName="listForm" ??????page="/userListAction.do">

    ???????<MultiPages:prev name="[Prev ]" />
    ???????<MultiPages:index displayCount="1" />
    ???????<MultiPages:next name="[Next ]" />
    </MultiPages:pager>

    模型UserTest的批量查詢功能已經(jīng)全部完成。

    以上是介紹基于開源Jdon框架開發(fā)軟件系統(tǒng)中的CRUD和批量查詢功能步驟,遵循模板化開發(fā),開發(fā)人員使用起來(lái)輕松而不容易出錯(cuò),適合軟件生產(chǎn)和嚴(yán)格的項(xiàng)目管理。

    本案例源碼下載

    按這里查看更詳細(xì)全面文檔

    附件:本案例代碼結(jié)構(gòu)圖:

    本案例全部代碼(struts+jdon+jpa/hibernate架構(gòu)):

    ?

    業(yè)務(wù)模型類代碼

    @Entity
    public class UserTest extends Model {

    ??????private String userId;
    ??????private String name;
    ??????@Id
    ??????public String getUserId() {
    ????????????return userId;
    ??????}

    ??????public String getName() {
    ????????????return name;
    ??????}

    ??????public void setUserId(String userId) {
    ??????this.userId = userId;
    ??????}

    ??????public void setName(String name) {
    ??????this.name = name;
    ??????} ??????
    }

    持久層代碼

    public class JdbcDAO extends DaoTemplate {

    private final static Logger logger = Logger.getLogger(JdbcDAO.class);

    ??????public JdbcDAO(CacheManager cacheManager, DaoCRUD daoCRUD) {
    ????????????super(cacheManager, daoCRUD);
    ??????}

    ??????public PageIterator getModels(int start, int count) throws Exception{
    ????????????String GET_ALL_ITEMS_ALLCOUNT = "select count(1) from usertest ";
    ????????????String GET_ALL_ITEMS = "select userId from usertest ";
    ????????????return pageIteratorSolver.getDatas("", GET_ALL_ITEMS_ALLCOUNT, ??????????????????GET_ALL_ITEMS, start, count);
    ??????}

    }

    業(yè)務(wù)層服務(wù)代碼
    基本是委托持久層操作的簡(jiǎn)單代碼

    public class TestServicePOJOImp implements TestService, Poolable{
    ??????private final static Logger logger = Logger
    ????????????.getLogger(TestServicePOJOImp.class);
    ??????private final static String USER_SAVE_ERROR = "USER.SAVE.ERROR";

    ??????private JdbcDAO jdbcDao;

    ??????public TestServicePOJOImp(JdbcDAO jdbcDao) {
    ????????????this.jdbcDao = jdbcDao;
    ??????}

    ??????public void createUser(EventModel em) {
    ????????????UserTest user = (UserTest) em.getModel();
    ????????????try {??????
    ??????????????????jdbcDao.insert(user);
    ????????????} catch (Exception ex) {
    ??????????????????logger.error(ex);
    ??????????????????em.setErrors(USER_SAVE_ERROR);
    ????????????}

    ??????}

    ??????public void updateUser(EventModel em) {
    ????????????UserTest user = (UserTest) em.getModel();
    ????????????try {
    ??????????????????jdbcDao.update(user);
    ????????????} catch (Exception ex) {
    ??????????????????logger.error(ex);
    ??????????????????em.setErrors(USER_SAVE_ERROR);
    ????????????}

    ??????}

    ??????public void deleteUser(EventModel em) {
    ????????????UserTest user = (UserTest) em.getModel();
    ????????????try {
    ??????????????????jdbcDao.delete(user);
    ????????????} catch (Exception ex) {
    ??????????????????logger.error(ex);
    ??????????????????em.setErrors(USER_SAVE_ERROR);
    ????????????}

    ??????}??????

    ??????public UserTest getUser(String userId) {
    ????????????logger.debug(" get User from DAO + JDBC " + userId);
    ????????????return (UserTest)jdbcDao.loadModelById(UserTest.class, userId);

    ??????}

    ??????public PageIterator getAllUsers(int start, int count) {
    ????????????PageIterator pageIterator = null;
    ????????????try {
    ??????????????????pageIterator = jdbcDao.getModels(start, count);
    ????????????} catch (Exception ex) {
    ??????????????????logger.error(ex);
    ????????????}
    ????????????return pageIterator;
    ????????????}

    ??????}

    表現(xiàn)層代碼

    public class UserListAction extends ModelListAction {
    ??????private final static Logger logger = Logger.getLogger(UserListAction.class);

    ??????public Model findModelByKey(HttpServletRequest request, Object key) {
    ????????????Model model = null;
    ????????????try {
    ??????????????????logger.debug("get the?model?for primary key=" + key + " type:"+ ????????????????????????key.getClass().getName());
    ??????????????????TestService testService = (TestService) ????????????????????????WebAppUtil.getService("testService", request);
    ??????????????????model = testService.getUser((String)key);
    ????????????} catch (Exception ex) {
    ??????????????????logger.debug("get the model for primary key=" + key + " type:"+ ??????????????????????????????key.getClass().getName());
    ??????????????????logger.error(" error: " + ex);
    ????????????}
    ????????????return?model;

    ??????}

    ??????public PageIterator getPageIterator(HttpServletRequest request, int start,
    ??????int count) {
    ????????????PageIterator pageIterator = null;
    ????????????try {
    ??????????????????TestService testService = (TestService) ????????????????????????WebAppUtil.getService("testService", request);
    ??????????????????pageIterator = testService.getAllUsers(start, count);
    ????????????} catch (Exception ex) {
    ??????????????????logger.error(ex);
    ????????????}
    ????????????return pageIterator;
    ????????????}
    ??????}

    轉(zhuǎn)載于:https://www.cnblogs.com/barrywxx/p/4526660.html

    總結(jié)

    以上是生活随笔為你收集整理的Jdon框架开发指南的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。