前端代碼
$('#kfMsgTable').bootstrapTable({
url:'../../../rzsj/selectByParams.action',
striped: true, //是否顯示行間隔色
cache: false, //是否使用緩存,默認爲true,所以一般情況下需要設置一下這個屬性(*)
pagination: true, //是否顯示分頁(*)
sortable: true, //是否啓用排序
sortOrder: "asc", //排序方式
sidePagination: "server", //分頁方式:client客戶端分頁,server服務端分頁(*)
pageNumber: 1, //初始化加載第一頁,默認第一頁,並記錄
pageSize: 10, //每頁的記錄行數(*)
pageList: [10, 25, 50, 100], //可供選擇的每頁的行數(*)
search: false, //是否顯示錶格搜索
strictSearch: true,
showColumns: true, //是否顯示所有的列(選擇顯示的列)
showRefresh: true, //是否顯示刷新按鈕
//得到查詢的參數
queryParams : function (params) {
//這裏的鍵的名字和控制器的變量名必須一直,這邊改動,控制器也需要改成一樣的
var temp = {
rows: params.limit, //頁面大小
page: (params.offset / params.limit) + 1, //頁碼
start:(params.offset / params.limit)*params.limit,//從start開始查詢
sort: params.sort, //排序列名
sortOrder: params.order, //排位命令(desc,asc)
name:$("#name").val(),
openid:$("#openid").val()
};
return temp;
},
columns:[
{field:'name',title:'姓名',align:'center',valign: 'middle'},
{field:'sfzhm',title:'身份證號碼',align:'center',valign: 'middle'},
{field:'tel',title:'聯繫電話',align:'center',valign: 'middle'},
{field:'openid',title:'用戶標識',align:'center',valign: 'middle'},
{field:'rzrq',title:'認證日期',align:'center',valign: 'middle',
formatter:function(value,row,index){
return UnixToDate(value,1);
}
},
{title:"操作",align:"center",valign:"middle",width:200,
formatter:function(value,row,index){
return "<button class='btn btn-primary' onclick='sendKfMsg("+JSON.stringify(row)+")'"+">發送客服消息</button>";
}
}
],
onLoadSuccess: function (data) {
console.log(data);
},
onLoadError: function () {
alert("數據加載失敗!")
},
});
springmvc後臺接收代碼
package cn.njhq.wechat.controller;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.fastjson.JSONObject;
import cn.njhq.wechat.pojo.TbRzsj;
import cn.njhq.wechat.service.RzsjService;
import cn.njhq.wechat.util.SendMessageUtil;
@RequestMapping("rzsj")
@Controller
public class RzsjController {
private Logger logger = Logger.getLogger(RzsjController.class);
@Autowired
private RzsjService rzsjService;
@RequestMapping("/selectByParams")
public void selectByParamse(HttpServletRequest request,HttpServletResponse response,@RequestParam Map<String, Object> params){
JSONObject jsonObject = new JSONObject();
if(params.size() == 0){
logger.info("傳遞的參數爲空!,無法執行下一步");
jsonObject.put("info", "傳遞的參數爲空!,無法執行下一步");
try {
SendMessageUtil.sendMessage(response, jsonObject);
} catch (IOException e) {
e.printStackTrace();
}
return;
}
logger.info(params.toString());
logger.info("********************************************************");
List<TbRzsj> rzsjs = rzsjService.selectByParamse(params);
jsonObject.put("success", true);
jsonObject.put("total", rzsjService.selectTotalByParams(params));
jsonObject.put("rows", rzsjs);
try {
SendMessageUtil.sendMessage(response, jsonObject);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我將前端傳遞過來的數據封裝成map集合,直接對集合操作,將map集合傳入到mapper.xml文件中
我們再看看mybatis中mapper.xml文件
<!--多參數查詢認證數據-->
<select id="selectByParams" parameterType="Map" resultMap="BaseResultMap">
select top ${rows} NAME as name,TEL as tel,OPENID as openid,RZRQ as rzrq,sfzhm from TB_RZSJ
where 1=1 and YHRZ_ID not in (select top ${start} YHRZ_ID from TB_RZSJ where 1=1
<if test="name != null and name != ''">
and dbo.fnGetPY(NAME) like '%'+#{name}+'%'
</if>
<if test="openid != null and openid != ''">
and OPENID=#{openid}
</if>)
<if test="name != null and name != ''">
and dbo.fnGetPY(NAME) like '%'+#{name}+'%'
</if>
<if test="openid != null and openid != ''">
and OPENID=#{openid}
</if>
</select>
<!--多參數查詢認證數據記錄數-->
<select id="selectTotalByParams" parameterType="Map" resultType="int">
select isnull(count(*),0) from TB_RZSJ
where 1=1
<if test="name != null and name != ''">
and dbo.fnGetPY(NAME) like '%'+#{name}+'%'
</if>
<if test="openid != null and openid != ''">
and OPENID=#{openid}
</if>
</select>
結束語
很簡單的,都在代碼上面了,如果有什麼不懂的請留言,如果有錯誤的地方,請大神指點。