標籤:lin 系統 err 相同 str method list size 架構
這是自己曾寫的一個系統(養老保險管理)中的一個小片段,今天突然想到把請求資料改成json,該如何處理。(Spring+SpringMVC+MyBatis+MySql)如下
1.前端jsp頁面
<div class="tab_tip"> 請輸入[社會安全號碼或姓名] <input type="text" class="tab_getText" id="tab1_getText"> <input type="button" class="tab_selectButton" id="tab1_selectButton" value="查詢"></div><!-- 省略代碼 --><table class="table" id="table1" cellspacing="0" cellpadding="0"> <tr> <th>個人編號</th> <th>社會安全號碼</th> <th>姓名</th> <th>性別</th> <th>民族</th> <th>出生年月</th> <th>參加工作時間</th> <th>繳費基數</th> <th>單位編號</th> <th>單位簡稱</th> <th>人員狀態</th> </tr></table>
2. JavaScript處理代碼
$(function(){ $("#tab1_selectButton").unbind(‘click‘).click(function(){ var tab1_getText = $.trim(document.getElementById("tab1_getText").value); if(tab1_getText != ""){ $.ajax({ type:"POST", url:"getStaffAllSelect/"+tab1_getText+"/0", async:false, dataType:"json", success:function(data){ $(".staffallinfotr").remove(); for(i=0;i<data.length;i++){ $("#table1").append(‘<tr class="staffallinfotr"><td>‘+data[i].sid+‘</td><td>‘+data[i].sino+‘</td><td>‘+data[i].sname+‘</td><td>‘+data[i].ssex+‘</td><td>‘+data[i].snation+‘</td><td>‘+data[i].sbirth+‘</td><td>‘+data[i].sdaj+‘</td><td>‘+data[i].pbase+‘</td><td>‘+data[i].cid+‘</td><td>‘+data[i].csn+‘</td><td>‘+data[i].sstate+‘</td></tr>‘); } }, error:function(){ alert("error"); }, complete : function(XMLHttpRequest,status){ if(status==‘timeout‘){ ajaxTimeoutTest.abort(); alert("逾時"); } } }); }else{ alert("請輸入個人編號或姓名!"); } }); });
3.Controller類中方法(註:StaffAllSelectDTO:和前端jsp頁面中的table欄位相同,因為代碼過長,就不再貼出)
@RequestMapping("/getStaffAllSelect/{sname}/{start}") public @ResponseBody List<StaffAllSelectDTO> getStaffAllSelect(@PathVariable String sname, @PathVariable int start){ List<StaffAllSelectDTO> staffAllSelectDTOList = staffServices.getStaffAllSelectByName(sname, start, 10); for(int i=0;i<staffAllSelectDTOList.size();i++){ System.out.println(staffAllSelectDTOList.get(i)); } return staffAllSelectDTOList; }
4. Services類中方法
@Override public List<StaffAllSelectDTO> getStaffAllSelectByName(String sname, int start, int limit) { // TODO Auto-generated method stub List<Staff> staffList = staffDAO.getStaffsByName(sname, start, limit); List<StaffAllSelectDTO> staffAllSelectDTOList = staffFactory.staffAndStaffPaymentToStaffAllSelectDTO(staffList); return staffAllSelectDTOList; }
5. DAO類中方法
@Override public List<Staff> getStaffsByName(String sname, int start, int limit) { // TODO Auto-generated method stub Map<String, Object> map = new HashMap<String, Object>(); map.put("sname", sname); map.put("start", start); map.put("limit", limit); return getSqlSession().selectList("com.staff.entity.StaffMapper.getStaffsByName", map); }
6. DTO: StaffAllSelectDTO:和前端jsp頁面中的table欄位相同,因為代碼過長,就不再貼出)
7. Mybatis
<select id="getStaffsByName" parameterType="Map" resultMap="StaffResult"> select s.sid,s.sino,s.sname,s.ssex,s.snation,s.sbirth,s.sdaj,s.sstate,s.spbase,s.cid,c.csn,c.cname from staffinfo s, companyinfo c where s.cid = c.cid <if test="sname != null and !"".equals(sname)">and s.sname like CONCAT(‘%‘,#{sname,jdbcType=VARCHAR},‘%‘)</if> <if test="start!=null and limit!=null"> limit #{start},#{limit} </if> </select>
Spring註解處理Ajax請求-JSON格式[系統架構:Spring+SpringMVC+MyBatis+MySql]