1.html
名称
名称证号
2.javaScript
script>
$(function () {
var oTable145=$("#search145").DataTable({
"ajax":{
"url":"search/search145",
"type":"POST"
},
"autoWidth":true,//自动计算列宽
"scrollX": true,
"paging": true,//分页,默认true
"bSort": false,//能否对列进行排序操作
"lengthChange": false,//每页显示数据条数能否修改,默认true
"pageLength": 15,//每页显示数据个数
"searching": false,//本地搜索默认true
"serverSide": true,
"columns":[
{"data":"name"},
{"data":"nameCode"}
],
language: {//代替表下方的英文页码说明
"sProcessing": "处理中...",
"sLengthMenu": "每页 _MENU_ 项",
"sZeroRecords": "没有匹配结果",
"sInfo": "当前显示第 _START_ 至 _END_ 项,共 _TOTAL_ 项。",
"sInfoEmpty": "当前显示第 0 至 0 项,共 0 项",
"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
"sInfoPostFix": "",
"sSearch": "搜索:",
"sUrl": "",
"sEmptyTable": "表中数据为空",
"sLoadingRecords": "载入中...",
"sInfoThousands": ",",
"oPaginate": {
"sFirst": "首页",
"sPrevious": "上页",
"sNext": "下页",
"sLast": "末页",
"sJump": "跳转"
}
}
});
function search(){
var name=$("#nameSearch").val();
var param={
"name":name//(参数名,参数值)在参数处理中,参数名和Controller中一样就可以了,做对象处理参数,参数名必须为实体类的属性名
};
oTable.settings()[0].ajax.data=param;
oTable.ajax.reload();
}
$("#serchbtn").click(
function () {
search();
}
);
});
后台有两种接收方式:
一,,直接接收传递过来的参数
Controller控制器阶段
@RequestMapping(value = "queryTest",method = RequestMethod.POST)
public Mapquery(@RequestParam(required=false)String name){
Mapparam=new HashMap ();
Listlist= SearchService.search(name);
int i=list.size();
System.out.println(i);
param.put("draw",1);
param.put("data",list);
param.put("recordsTotal",i);
param.put("recordsFiltered",i);
return param;
}
service阶段
public static ListqueryByHql(String name) {
Listlist=new ArrayList ();
String hql="from User where 1=1 [WHERE]";
String where="";
if(!(StringUtil.isEmpty(name)){
where+=" and name = :name1";
param.put("name1",name);
}
if(where.length()>0){
hql=hql.replace("[WHERE]",where);
}else{
hql=hql.replace("[WHERE]",where);
}
Session session = null;
try
{
session = getSession();
Query query = session.createQuery(hql);
Iterator iterator = param.keySet().iterator();
while(iterator.hasNext()) {
String key = iterator.next().toString();
Object value = param.get(key);
//String[]
if(value != null && "[Ljava.lang.String;".equals(value.getClass().getName())) {
query.setParameterList(key, (String[])value);
}
else {
query.setParameter(key, value);
}
}
list=query.list();
}
catch (Exception ex) {
logger.error(ex.toString());
logger.error("hql: {}", hql);
ex.printStackTrace();
}
finally {
closeSession(session);
}
return list;
}
这是参数处理的第一种方式,将参数直接传递到后台后台也直接处理参数,但是缺点是当参数过多时传递的参数多造成代码的雍余,这时引入第二种接受参数的方式,以一个对象接收参数
第二种处理参数的方式
Controller控制器阶段
@RequestMapping(value = "queryTest",method = RequestMethod.POST)
public Mapquery(User user){
Mapparam=new HashMap ();
Listlist= SearchService.search(user);
int i=list.size();
System.out.println(i);
param.put("draw",1);
param.put("data",list);
param.put("recordsTotal",i);
param.put("recordsFiltered",i);
return param;
}
service阶段
public static ListqueryByHql(User user) {
Listlist=new ArrayList ();
String hql="from User where 1=1 [WHERE]";
String where="";
if(!(StringUtil.isEmpty(user.getName())){
where+=" and name = :name";
param.put("name",user.getName);
}
if(where.length()>0){
hql=hql.replace("[WHERE]",where);
}else{
hql=hql.replace("[WHERE]",where);
}
Session session = null;
try
{
session = getSession();
Query query = session.createQuery(hql);
Iterator iterator = param.keySet().iterator();
while(iterator.hasNext()) {
String key = iterator.next().toString();
Object value = param.get(key);
//String[]
if(value != null && "[Ljava.lang.String;".equals(value.getClass().getName())) {
query.setParameterList(key, (String[])value);
}
else {
query.setParameter(key, value);
}
}
list=query.list();
}
catch (Exception ex) {
logger.error(ex.toString());
logger.error("hql: {}", hql);
ex.printStackTrace();
}
finally {
closeSession(session);
}
return list;
}