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

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

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

    空間站

    北極心空

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

    DAO + 通用持久類 + 通用動(dòng)態(tài) formBean 類,實(shí)現(xiàn)數(shù)據(jù)增、刪、改、查

    newxy( 新坐標(biāo) ) 技術(shù)運(yùn)用之三

    ?

    DAO類”: net.newxy.dbm.BaseDAO或其子類;

    “通用持久類”: net.newxy.dbm.DynaDto;

    “通用動(dòng)態(tài)formBean類”: net.newxy.struts_faces.DynaFormBean

    ?

    DAO + 通用持久類,實(shí)現(xiàn)數(shù)據(jù)增、刪、改、查》 ( 又名《 不同于hibernate,利用通用持久類實(shí)現(xiàn)數(shù)據(jù)增、刪、改、查,可極大提高開發(fā)效率 ) 文章中介紹的數(shù)據(jù)增、刪、改、查方法比 hibernate 的方法已簡單很多,如果引入通用動(dòng)態(tài) formBean ,開發(fā)效率會(huì)有更大提高。

      ?

    net.newxy.struts_faces.DynaFormBean 類的類圖

    一、利用通用動(dòng)態(tài)formBean類,實(shí)現(xiàn)數(shù)據(jù)增、刪、改

    struts ActionServlet 將用戶 submit 來的數(shù)據(jù)綁定到 net.newxy.struts_faces.DynaFormBean 對象中,在后臺(tái),通過調(diào)用 net.newxy.struts_faces.DynaFormBean public Object getDto() public Object get(String name) 方法將上傳的數(shù)據(jù)提出, net.newxy.struts_faces.DynaFormBean 起作傳送數(shù)據(jù)的作用。

    1 )數(shù)據(jù)插入: Object update(Object dto) throws Exception;

    舉例:

    public ActionForward update(ActionMapping actionMapping, ActionForm actionForm,

    				
    						????????????????????????????? HttpServletRequest httpServletRequest,
    		
    				
    						??????????? HttpServletResponse httpServletResponse) throws Exception{
    		
    				
    						??????????? ......
    		
    				
    						??????????? net.newxy.struts_faces.DynaFormBean form=( net.newxy.struts_faces.DynaFormBean)actionForm;
    				
    						
    						
    				
    		

    try{

    net.newxy.dbm.DynaDto dynaDto=( net.newxy.dbm.DynaDto)form.getDto();

    net.newxy.dbm.BaseDAO dao=new net.newxy.dbm.BaseDAO();

    dynaDto.set_table("table1");//設(shè)置表名

    ??????? Object result=dao.update(dynaDto);

    ??? }catch(Exception e){

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

    }

    ......

    }

    jsp頁面不上傳主關(guān)鍵字段值,主關(guān)鍵字段值會(huì)自動(dòng)得到,是原有最大值加一。

    如果jsp頁面上傳了主關(guān)鍵字段值,如:表單中有<html:text property=”id” />用戶鍵入了id值。newxy(新坐標(biāo))會(huì)檢查賦給的主關(guān)鍵字值是否已存在,如果存在,作更新(update)操作,如果不存在,作插入(insert)操作。

    判斷是更新還是插入操作,可以根據(jù)返回的result值來判斷,如果result值為空,是作更新操作,如果result不空是作插入操作。

    Object result=dao.update(dynaDto);

    如果想需要知道自動(dòng)賦給的主關(guān)鍵字段值,可以用如下方法:

    Object result=dao.update(dynaDto);

    Object idValue=null;

    If(result!=null)

    idValue=((DynaDto)result).get(“id”);

    idValue 就是自動(dòng)賦給的主關(guān)鍵字段值。

    ?

    2)數(shù)據(jù)更新:

    舉例:

    ?

    public ActionForward update(ActionMapping actionMapping, ActionForm actionForm,

    ????????????????????????????? HttpServletRequest httpServletRequest,

    ??????????? HttpServletResponse httpServletResponse) throws Exception{

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

    ?? DynaFormBean form=(DynaFormBean)actionForm;

    try{

    DynaDto dynaDto=(DynaDto)form.getDto();

    net.newxy.dbm.BaseDAO dao=new net.newxy.dbm.BaseDAO();

    dynaDto.set_table("table1");

    ?????? Object result=dao.update(dynaDto);

    ?? }catch(Exception e){

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

    }

    ......

    }

    數(shù)據(jù)更新方法與數(shù)據(jù)插入方法相同,需注意:如果 dynaDto 中沒有主關(guān)鍵字段值,即 dynaDto.get(“id”)==null dynaDto.get(“id”) 所得主關(guān)鍵字段值在表中不存在 dao.update(dynaDto) 方法仍然作插入操作。

    在進(jìn)行 dao.update(dynaDto) 前,可以判斷數(shù)據(jù)是否已存在于表中:

    Object masterKey=dynaDto.get(“id”);

    Object record=null;

    if(masterKey!=null)

    record=dao.load(dynaDto);

    如果 masterKey==null record==null ,表中不存在該條記錄, dao.update(dynaDto) 作插入操作,否則該條記錄存在于表中,作更新操作。

    那么在 jsp 頁面上如何保證用戶錄入的是新插入數(shù)據(jù)或是編輯修改的數(shù)據(jù)呢?參看本文后面部分 jsp 頁面上新建、編輯數(shù)據(jù)

    3 )數(shù)據(jù)刪除

    舉例:

    public ActionForward remove(ActionMapping actionMapping, ActionForm actionForm,

    				
    						
    								????????????????????????????? HttpServletRequest httpServletRequest,
    				
    		
    				
    						
    								??????????? HttpServletResponse httpServletResponse) throws Exception{
    				
    		
    				
    						
    								??????????? ......
    				
    		
    				
    						
    								
    										??????????? DynaFormBean form=(DynaFormBean)actionForm;
    								
    										
    										
    								
    						
    				
    		

    try{

    ??? // 刪除 formBean 當(dāng)前數(shù)據(jù)對應(yīng)的記錄:

    Object record=form.getDto();

    net.newxy.dbm.BaseDAO dao=new net.newxy.dbm.BaseDAO();

    dynaDto.set_table("table1");// 設(shè)置表名,如果上傳數(shù)據(jù)的 jsp 頁面表單中沒有 <html:hidden name=”_table” value=”table1”/>

    ??????? dao.remove(dynaDto);

    ?

    ??????? // httpServletRequest.getParameter(“_index”) 值代表記錄號,刪除該條記錄

    ??????? String index= httpServletRequest.getParameter(“_index”);

    ??????? Object record=form.getDto(index);

    ??????? dao.remove(record);

    ??? }catch(Exception e){

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

    }

    ......

    }

    ?

    二、利用通用動(dòng)態(tài)formBean類,實(shí)現(xiàn)多項(xiàng)查詢

    ?? 1、后臺(tái)代碼

    ?????? public ActionForward find(ActionMapping actionMapping, ActionForm actionForm,

    ?????????????? HttpServletRequest httpServletRequest,

    ?????????????? HttpServletResponse httpServletResponse) throws Exception{

    ?????????? DynaFormBean form=(DynaFormBean)actionForm;

    ?????????? net.newxy.util.FormBeanUtils. createForm(form,httpServletRequest);

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

    ?????? }

    ??? net.newxy.util.FormBeanUtils. createForm(form,httpServletRequest)方法將查詢結(jié)果放在form的_coll屬性中。后臺(tái)代碼極其簡單,

    那么httpServletRequest中包含了什么數(shù)據(jù)?下面是jsp頁面表單對上傳數(shù)據(jù)的組織。

    ?? 2、前臺(tái)查詢條件的組織

    <html:form action="/myAction.do?method=find">

    ? <html:hidden property="_dao" value="dao.myDAO1"/>

    ? <html:hidden property="_sql" value="select a.industry,b.* from industry as a,enterprise as b where{a.code=b.industry_code}"/>

    ? <table border="1">

    ??? <tr>

    ????? <td>邏輯</td>

    ????? <td colspan="2">

    ??????? <html:radio property="_lg" value="1">并且</html:radio>

    ??????? <html:radio property="_lg" value="0">或者</html:radio>

    ????? </td>

    ??? </tr>

    ??? <tr>

    ????? <td>行業(yè)</td>

    ????? <td>等于</td>

    ????? <td>

    ??????? <nhtml:hidden property="_item1" value="a.code"/>

    ??????? <html:hidden property="_item1" value="="/>

    ??????? <html:select property="_item1">

    ????????? <html:option value=""></html:option>

    ????????? <html:option value="A">農(nóng)、林、牧、漁業(yè)</html:option>

    ????????? <html:option value="B">采礦業(yè)</html:option>

    ????????? <html:option value="C">制造業(yè)</html:option>

    ??????? </html:select>

    ????? </td>

    ??? </tr>

    ??? <tr>

    ????? <td>企業(yè)名稱</td>

    ????? <html:hidden property="_item2" value="b.name"/>

    ????? <td>

    ??????? <html:select property="_item2">

    ????????? <html:option value=""></html:option>

    ????????? <html:option value="=">等于</html:option>

    ????????? <html:option value="like">包含</html:option>

    ??????? </html:select>

    ????? </td>

    ????? <td>

    ??????? <html:text property="_item2" size="40">

    ??????? </html:text>

    ????? </td>

    ??? </tr>

    ??? <tr>

    ????? <td>企業(yè)地址</td>

    ????? <html:hidden property="_item3" value="b.address"/>

    ????? <td>

    ??????? <html:select property="_item3">

    ????????? <html:option value=""></html:option>

    ????????? <html:option value="=">等于</html:option>

    ????????? <html:option value="like">包含</html:option>

    ??????? </html:select>

    ????? </td>

    ????? <td>

    ??????? <html:text property="_item3" size="40">

    ??????? </html:text>

    ????? </td>

    ??? </tr>

    ??? <tr>

    ????? <td align="right" colspan="3"><html:submit value=" 查 詢 "/></td>

    ??? </tr>

    ? </table>

    </html:form>

    該例是對企業(yè)表、行業(yè)表的連表查詢,查詢的基本條件是industry.code=enterprise.industry_code。

    ????? industry:行業(yè)代碼表

    ????? industry.code:行業(yè)代碼

    ????? enterprise:企業(yè)表

    ????? enterprise.industry_code:企業(yè)的行業(yè)代碼

    下面是表單中包含的幾個(gè)屬性:

    (1)_dao ,是DynaFormBean的保留屬性,是DAO類的別名,告訴后臺(tái)由哪個(gè)DAO類來操作數(shù)據(jù)。別名在WEB-INF/classes下的newxy.properties文件中指定,假設(shè)有一DAO類如下:

    ????????? package common;

    ????????? public class MyDAO1 extends net.newxy.dbm.BaseDAO{

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

    }

    ????????? 在newxy.properties文件中定義DAO類別名如下:

    ?????? ????????? dao.mydao1=common.MyDAO1

    ???? 如果DAO類common.MyDAO1的別名是dao.default,則表單無需上傳屬性_dao。

    (2)_sql ,是DynaFormBean的保留屬性,是查詢的基本條件,其中where跟隨其后的是大括號{},是最終組成查詢條件存放的地方。用戶選擇的查詢條件不管是”and”連接的還是”or”連接的,最終與_sql屬性中的初始條件都以”and”關(guān)系連接。最后產(chǎn)生的sql語句可能是這樣的:

    ????????? ???select a.industry,b.* from industry as a,enterprise as b where((a.code=b.industry_code)and (name=’....公司’ or? ......))

    ???? 其中紅色是初始條件,藍(lán)色是用戶選擇的條件,二者以”and”相連。

    ?

    (3)_lg ,是DynaFormBean的保留屬性,是“邏輯”的意思,如果_lg==”1”,上傳的查詢條件以”and”關(guān)系組織,否則以”or”關(guān)系統(tǒng)組織。

    (4)_item開頭的屬性 ,這種屬性都以一個(gè)數(shù)字跟隨其后,這樣的屬性一上傳就有三個(gè)。如果在“企業(yè)名稱”的<select name=”_item2”>...</select>項(xiàng)選擇“等于”,在<input type=”text” name=”_item2”/>項(xiàng)填入“......公司”,上傳數(shù)據(jù)后可得到如下字符串?dāng)?shù)組:

    String[] itemValues=request.getParameterValues(“_item2”);

    itemValues是三個(gè)字符串構(gòu)成的數(shù)組,三個(gè)字符串分別是 “a.name” “=” “......公司”,拼這一起可構(gòu)成”a.name=’......公司’”,它構(gòu)成查詢條件的一部分。之所以這么解說,是增加對查詢條件構(gòu)成的理解。可參看 newxy(新坐標(biāo))網(wǎng)站 范例的“多項(xiàng)查詢”。

    三、 利用通用動(dòng)態(tài)formBean類, 向前臺(tái)展現(xiàn)查詢結(jié)果

    newxy(新坐標(biāo))查詢的記錄集保存在DynaFormBean類的_coll屬性中,DynaFormBean另有一個(gè)屬性_navigate,負(fù)責(zé)對記錄集的分頁計(jì)算。jsp頁面可以用<nlogic:navigate/>標(biāo)簽為formBean的記錄集分頁導(dǎo)航。

    舉例:

    如果struts的myAction的name=”enterprises”,查詢后,formBean以”enterprises”為名保存的會(huì)話或其它scope中。

    <div><nlogic:navigate formName="enterprise" length="20"/></div><!--導(dǎo)般標(biāo)簽-->

    <logic:notEmpty name="enterprises" property="_coll">

    ? <table>

    ? <logic:iterate id="rec" name="enterprises" property="_coll">

    ??? <tr>

    ????? <td><bean:write name="rec" property="industry"/></td> <!--行業(yè)-->

    ????? <td><bean:write name="rec" property="name"/></td> <!--企業(yè)名稱-->

    ??? </tr>

    ? </logic:iterate>

    ? </table>

    </logic:notEmpty>

    屬性_coll是通用動(dòng)態(tài)formBean類DynaFormBean的保留屬性,保存了查詢的記錄,類型是List,其元素是 org.apache.commons.beanutils.BasicDynaBean 類型。

    ???????

    四、通用動(dòng)態(tài)formBean類DynaFormBean及其父類的重要方法

    ???? net.newxy.struts_faces.DynaFormBean類繼承自net.newxy.struts_faces.FormBean,下面是net.newxy.struts.faces.DynaFormBean及其父類net.newxy.struts.faces.FormBean的幾個(gè)重要方法

    1、public void append(java.lang.Object?dto)

    在結(jié)果集_coll的最后追加一條記錄。同時(shí)將索引號指向最后一條。并用最后一條記錄數(shù)據(jù)填寫formBean。

    2、public java.lang.String setCollection(java.lang.Object?objs)

    設(shè)置結(jié)果集,與set_coll(Collection _coll)相比,該方法除了作set_coll(Collection _coll)操作外,還對與分頁瀏覽的相關(guān)屬性值進(jìn)行計(jì)算,將計(jì)算結(jié)果重新填入formBean的_navigate屬性中,并重新設(shè)置索引號。

    3、public void setDto(java.lang.String?no)

    formBean中的數(shù)據(jù)填寫到以no參數(shù)值為索引號的記錄中。

    4、public void setForm(java.lang.String?no)

    no參數(shù)值為索引號,查找該條記錄,如果存在,將該條記錄數(shù)據(jù)填入的formBean中,并以no值為索引號。

    5、public void setForm(java.lang.Object?dto)

    dto為包含數(shù)據(jù)的javaBean。將該Bean的值填入formBean中。這個(gè)javaBean類繼承自net.newxy.dbm.Dto,但在目前版本只介紹formBean類 net.newxy.struts.faces.FormBean 及持久類net.newxy.dbm.Dto,參數(shù)dto暫理解成通用持久類net.newxy.dbm.DynaDto。

    6、public java.lang.Object getDto()

    formBean得到持久類實(shí)例。

    7、public java.lang.Object getDto(java.lang.String?no)

    no為序號,返回這條記錄。如果不存在,返回空。

    8、public void remove(java.lang.String?no)

    從結(jié)果集中刪除以no參數(shù)值為序號的記錄。

    9、public java.lang.Object cloneDto(java.lang.String?no)

    no參數(shù)值為索引號,得到這條記錄的克隆

    10、public void empty()

    formBean數(shù)據(jù)清空,但保留屬性不會(huì)被清除,包括保存了記錄集的屬性_coll。

    11、public java.util.Map getValues()

    獲取保存了屬性值對的HashMap,頁面表單submit上傳的屬性保存在這個(gè)Map中。

    12、public java.lang.Object get(java.lang.String?name)

    獲取以name值為屬性名的屬性值

    13、public void set(java.lang.String?name, java.lang.Object?value)

    設(shè)置屬性值。如果name是保留屬性,將不進(jìn)行set操作。

    14、public void setValue(java.lang.String?name,java.lang.Object?value)

    保留屬性的set方法,通過此方法給以改變保留屬性的值。

    15、public void set_dao(java.lang.String?_dao)

    設(shè)置DAO類別名,_dao是保留屬性,也可用方法setValue(“_dao”,_dao)。

    16、public void set_index(java.lang.String?_index)

    設(shè)置記錄集的索引號,_index是保留屬性,也可用方法setValue(“_index”,_index)。

    ?

    五、在jsp頁面上新建、編輯數(shù)據(jù)

    1、后臺(tái)查詢數(shù)據(jù)

    ?????? public ActionForward find(ActionMapping actionMapping, ActionForm actionForm,

    ?????????????? HttpServletRequest httpServletRequest,

    ?????????????? HttpServletResponse httpServletResponse) throws Exception{

    ?????????? DynaFormBean form=(DynaFormBean)actionForm;

    ?????????? net.newxy.util.FormBeanUtils. createForm(form,httpServletRequest);

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

    ?????? }

    ? 2 、編輯更新記錄

    FormBeanUtils. createForm(form,httpServletRequest) 查詢的記錄集保存在DynaFormBean的_coll屬性中并且以第一條記錄填寫到formBean的屬性中。

    jsp頁面上有下列表單及其屬性:

    <html:form action="/myAction.do?method=update">

    ? <html:hidden property="_table" value="enterprise"/>

    ? <html:hidden property="industry_code" value="A"/>

    ? 注冊號:<html:text property="register_id"></html:text><br/>

    ? 負(fù)責(zé)人:<html:text property="principal"></html:text><br/>

    ? 企業(yè)名稱:<br />

    ? <html:text property="name" size="36"></html:text><br />

    ? 企業(yè)地址:<br />

    ? <html:text property="address" size="36"></html:text><br />

    ? 經(jīng)營范圍:<br/>

    ? <textarea name="dealIn" cols="36" rows="10">

    ? <bean:write name="enterprise" property="dealIn"/>

    ? </textarea><br/>

    ? <html:submit value=”提交”/>

    </html:form>

    頁面上顯示的是每一條記錄,用戶編輯修改數(shù)據(jù)提交到后臺(tái),綁定到formBean中,覆蓋原有數(shù)據(jù)。因?yàn)楸韱沃袥]有主關(guān)鍵字段屬性enterprise_id,數(shù)據(jù)提交后就不會(huì)改變原有的enterprise_id值, net.newxy.util.FormBeanUtils. createForm(form,httpServletRequest) 方法調(diào)用 DAO 類的 update(Object dto) 方法就會(huì)作更新操作。

    (1) 在頁面上為 formBean 選擇第 10 條記錄

    jsp 頁面 <html:form></html:form> 標(biāo)簽前加入 java 腳本 <%......%>

    <%

    ??? net.newxy.faces.DynaFormBean form=( net.newxy.faces.DynaFormBean)pageContext.getAtribute(“enterprises”,3);// session 中得到 formBean;

    ??? form.setForm(“9”);

    %>

    (2) 在后臺(tái)為 formBean 選擇第 10 條記錄

    public ActionForward? selectRecord(ActionMapping actionMapping, ActionForm actionForm,

    ????????????????????????????? HttpServletRequest httpServletRequest,

    ??????????? HttpServletResponse httpServletResponse) throws Exception{

    ??? ......

    DynaFormBean form=(DynaFormBean)actionForm

    form.set(“10”);

    ......

    }

    jsp頁面顯示的便是第10條記錄。用戶可以對其編輯上傳保存到數(shù)據(jù)庫。

    ?

    3、新建插入記錄

    本篇介紹的 數(shù)據(jù)插入: Object update(Object dto) throws Exception 方法表明,如果 dto 不包含主關(guān)鍵字段屬性, update(dto) 作插入操作。如果在表單提交數(shù)據(jù)前調(diào)用 net.newxy.faces.DynaFormBean 類的 empty() 方法會(huì)清空 formBean 中的數(shù)據(jù),但不清除保留屬性,包括保存了記錄集的屬性 _coll 。這樣用戶得到的頁面表單是空白的。而且提交數(shù)據(jù)后因?yàn)橐亚宄酥麝P(guān)鍵字段屬性, Object update(Object dto) throws Exception 方法會(huì)作數(shù)據(jù)插入操作。

    (1) jsp 頁面上清空 formBean

    jsp 頁面 <html:form></html:form> 標(biāo)簽前加入 java 腳本 <%......%>

    <%

    ??? net.newxy.faces.DynaFormBean form=( net.newxy.faces.DynaFormBean)pageContext.getAtribute(“enterprises”,3);// session 中得到 formBean;

    ??? form.empty();

    %>

    (2) 在后臺(tái)頁面上清空 formBean

    public ActionForward? selectRecord(ActionMapping actionMapping, ActionForm actionForm,

    ????????????????????????????? HttpServletRequest httpServletRequest,

    ??????????? HttpServletResponse httpServletResponse) throws Exception{

    ??? ......

    DynaFormBean form=(DynaFormBean)actionForm

    form.empty();

    ......

    }

    jsp 頁面顯示的是空白表單,用戶輸入數(shù)據(jù)提交后會(huì)作插入操作。

    ?

    ?

    newxy( 新坐標(biāo) ) 技術(shù)網(wǎng)站: http://www.newxy.net

    posted on 2006-12-25 12:27 蘆葦 閱讀(490) 評論(0)  編輯  收藏 所屬分類: Struts
    主站蜘蛛池模板: 免费观看亚洲人成网站| 人与禽交免费网站视频| 暖暖免费高清日本中文| 亚洲2022国产成人精品无码区| 国产精品成人亚洲| 国产精品爱啪在线线免费观看| 国产亚洲成人在线播放va| 亚洲熟妇无码一区二区三区 | 成人特级毛片69免费观看| 99无码人妻一区二区三区免费| 亚洲精品无码永久在线观看你懂的 | 99精品视频免费| 全部免费毛片在线| 亚洲久悠悠色悠在线播放| 99re6热视频精品免费观看| 在线观看亚洲成人| 羞羞视频网站免费入口| 成年轻人网站色免费看| 亚洲视频在线观看视频| 国产三级在线免费| 中文字幕第一页亚洲| 高潮内射免费看片| 波多野结衣久久高清免费| 国产精品亚洲精品| 97碰公开在线观看免费视频| 亚洲成色在线影院| a级大片免费观看| 亚洲综合色婷婷七月丁香| 国产亚洲精品第一综合| 色吊丝最新永久免费观看网站 | xxxx日本在线播放免费不卡| 免费国产成人高清在线观看麻豆| 亚洲一级特黄特黄的大片 | 亚洲视频免费在线播放| 日本免费中文字幕| 国产AV无码专区亚洲AVJULIA | 一级中文字幕乱码免费| 青青青国产色视频在线观看国产亚洲欧洲国产综合 | 亚洲毛片αv无线播放一区| jzzjzz免费观看大片免费| 亚洲成a人片在线播放|