网页的前台展示和查询分页相关代码(后台分页)

  • datagrid获取数据的后台代码

js映射的地址controller的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Controller
@RequestMapping("userPlat")
public class UserPlatformController {
@RequestMapping("selectmeteridandimei")
@ResponseBody
public String selectmeteridandimei(HttpServletRequest request,int rows,int page,String producemeteridorimei)
throws Exception {
//获取查到数据的数量
int total = userPlatService.selectmeteridandimeiNumber(producemeteridorimei);
//根据当前的页数和行数,获取一定数量的数据
String result = userPlatService.selectmeteridandimei(rows,page,producemeteridorimei);
//配置前台需要的数据格式
JSONObject resultobject = new JSONObject();
resultobject.put("total",total);
//把string转换成json数组
resultobject.put("rows",JSONArray.parseArray(result));
return resultobject.toString();
}

@RequestMapping("/selectproduceListBy")
@ResponseBody
public String selectproduceListBy(HttpServletRequest request,String producetongxuntypeselect,String producetypeselect,
String produceordernumber,String produceusername,String producemeter,int rows,int page)
throws Exception {
//获取查到数据的数量
int total = userPlatService.selectproduceListByNumber(producetongxuntypeselect,producetypeselect,produceordernumber,produceusername,producemeter);
//根据当前的页数和行数,获取一定数量的数据
String result = userPlatService.selectproduceListBy(producetongxuntypeselect,producetypeselect,produceordernumber,produceusername,producemeter,rows,page);
//配置前台需要的数据格式
JSONObject resultobject = new JSONObject();
resultobject.put("total",total);
//把string转换成json数组
resultobject.put("rows",JSONArray.parseArray(result));
return resultobject.toString();
}
}

先写service接口,然后在写实现类的代码,代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 	@Service
public class UserPlatformServiceImpl implements UserPlatformService{
@Override
public int selectmeteridandimeiNumber(String producemeteridorimei) {
//去掉字符串首尾空格
producemeteridorimei = producemeteridorimei.trim();
int total = userPlatMapper.selectmeteridandimeiNumber(producemeteridorimei);
return total;
}

@Override
public String selectmeteridandimei(int rows,int page,String producemeteridorimei) {
//通过起始页数和一页的行数获取数据的起始位置
int offset = page*rows;
//去掉字符串首尾空格
producemeteridorimei = producemeteridorimei.trim();
List<T_productmsg> productmsg = userPlatMapper.selectmeteridandimei(rows,offset,producemeteridorimei);
//list集合转换json数组
com.alibaba.fastjson.JSONArray array= com.alibaba.fastjson.JSONArray.parseArray(JSON.toJSONString(productmsg));
String data = array.toString();
return data;
}

@Override
public int selectproduceListByNumber(String producetongxuntypeselect,String producetypeselect,String produceordernumber,String produceusername,String producemeter) {
//去掉字符串首尾空格
producetongxuntypeselect = producetongxuntypeselect.trim();
producetypeselect = producetypeselect.trim();
produceordernumber = produceordernumber.trim();
produceusername = produceusername.trim();
producemeter = producemeter.trim();
//获取数量
int total = userPlatMapper.selectproduceListByNumber(producetongxuntypeselect,producetypeselect,produceordernumber,produceusername,producemeter);
return total;
}


@Override
public String selectproduceListBy(String producetongxuntypeselect,String producetypeselect,String produceordernumber,String produceusername,String producemeter,int rows,int page) {
//通过起始页数和一页的行数获取数据的起始位置
int offset = page*rows;
//去掉字符串首尾空格
producetongxuntypeselect = producetongxuntypeselect.trim();
producetypeselect = producetypeselect.trim();
produceordernumber = produceordernumber.trim();
produceusername = produceusername.trim();
producemeter = producemeter.trim();
List<T_productmsg> productmsg = userPlatMapper.selectproduceListBy(producetongxuntypeselect,producetypeselect,produceordernumber,produceusername,producemeter,rows,offset);
//list集合转换json数组
com.alibaba.fastjson.JSONArray array= com.alibaba.fastjson.JSONArray.parseArray(JSON.toJSONString(productmsg));
String data = array.toString();
return data;
}
}

先写数据库Mapper接口,然后在写mapper.xml,代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 	<select id="selectmeteridandimeiNumber" resultType="int">
select count(*) from t_productmsg where imei =#{producemeteridorimei} or meterid=#{producemeteridorimei}
</select>

<select id="selectmeteridandimei" resultType="T_productmsg">
select * from t_productmsg where imei =#{producemeteridorimei} or meterid=#{producemeteridorimei} LIMIT #{page}, #{rows}
</select>

//
<select id="selectproduceListByNumber" resultType="int">
select count(*) from t_productmsg where 1=1
<if test="producetongxuntypeselect != '' and producetongxuntypeselect != '全部'">
and comunicateMode = #{producetongxuntypeselect}
</if>
<if test="producetypeselect != '' and producetypeselect != '全部'">
and productType like '%${producetypeselect}%'
</if>
<if test="produceordernumber != ''">
and code = #{produceordernumber}
</if>
<if test="produceusername != ''">
and customerName like '%${produceusername}%'
</if>
<if test="producemeter != '' and producemeter != '全部'">
and meterSize like '%${producemeter}%'
</if>
</select>

<select id="selectproduceListBy" resultType="T_productmsg">
select * from t_productmsg where 1=1
<if test="producetongxuntypeselect != '' and producetongxuntypeselect != '全部'">
and comunicateMode = #{producetongxuntypeselect}
</if>
<if test="producetypeselect != '' and producetypeselect != '全部'">
and productType like '%${producetypeselect}%'
</if>
<if test="produceordernumber != ''">
and code = #{produceordernumber}
</if>
<if test="produceusername != ''">
and customerName like '%${produceusername}%'
</if>
<if test="producemeter != '' and producemeter != '全部'">
and meterSize like '%${producemeter}%'
</if>
LIMIT #{page}, #{rows}
</select>

查询分页(前台分页,此方法不推荐)

js的相关代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   function pagerFilter(data){
if (typeof data.length == 'number' && typeof data.splice == 'function'){ // 判断数据是否是数组
data = {
total: data.length,
rows: data
}
}
var dg = $(this);
var opts = dg.datagrid('options');
var pager = dg.datagrid('getPager');
pager.pagination({
onSelectPage:function(pageNum, pageSize){
opts.pageNumber = pageNum;
opts.pageSize = pageSize;
pager.pagination('refresh',{
pageNumber:pageNum,
pageSize:pageSize
});
dg.datagrid('loadData',data);
}
});
if (!data.originalRows){
data.originalRows = (data.rows);
}
var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
var end = start + parseInt(opts.pageSize);
data.rows = (data.originalRows.slice(start, end));
return data;
}

//res为数组
$('#deviceList').datagrid({loadFilter:pagerFilter}).datagrid('loadData', res);