获取表中字段最大值,并且保存在前台页面中
//获取Userid function getUserId(){ $.getJSON('<%=basePath %>user/getUserId.do', function(data){ alert(eval(data).userId); document.getElementById("userId").value=data.userId; }); }
<input id="userId" name="userId" value="" type="text" readonly="readonly"/><input value="获取最大的Userid" onclick="javascript:getUserId();" type="button" style="width:100px"/>
UserSlaveController 获取参数
@ResponseBody //@ResponseBody 返回的是数据,不加 @ResponseBody 返回的是页面。 @RequestMapping(value="/user/getUserId.do")//method=RequestMethod.POST public JSONObject getUserId(HttpServletRequest request, HttpServletResponse response)throws Exception { response.setContentType("text/html"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); JSONObject jsonObject = new JSONObject(); Integer userId = 0; try { userId = userSlaveService.getUserId(); } catch (Exception e) { Loger.logtxt("user", "获取id异常:" + e.toString()); } jsonObject.accumulate("userId", userId); System.out.println(jsonObject.toString()); //输出: {"userId":182888} System.out.println(jsonObject.values()); //输出: [182888] return jsonObject; //返回json,在上面的jsp页面中接收,并且保存值到input中。 }
UserSlaveService.java 接口声明方法 : public Integer getUserId()throws Exception;
UserSlaveServiceImpl.java 实现UserSlaveService接口:
@Override
public Integer getUserId() throws Exception {
return userDAO.getUserId();
}
IUserDAO.java 接口声明方法: public Integer getUserId()throws Exception;
user.xml 中的查询:
<select id="getUserId" resultType="java.lang.Integer">
<![CDATA[
SELECT (MAX(userid)+1) AS userid FROM userinfo
]]>