Spring注解处理Ajax请求-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>