参考文章链接:
****
使用MyBatis分页插件PageHelper遇到的问题
搭建layui+mybaits分页插件pagehelper实现漂亮的前端分页功能
使用pagehelper报错 java.lang.NoClassDefFoundError: net/sf/jsqlparser/expression/Expression
layui的数据表格+springmvc实现数据显示,分页功能
1. jar下载
PageHepler jar (5.0版本以上 跟4.0配置有小区别)
jsqlparser-0.9.5.jar(0.9版本以上)
导入jar包之后启动成功,jsql jar包地址: https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser/0.9.5
两个jar包资源群 群文件 QQ群:305663754
2.xml配置
mybatis的配置文件中配置这个插件
<!--分页插件配置-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
3.工具类准备
LayUI 数据表格接受格式:
{
code: 0,
msg: “”,
count: 数据总记录数,
data: []
}
ResultMap
/**
* 需要参数code(要为0,不然数据表格数据显示不出),
* msg(返回的消息),
* data(表格显示的数据),
* totals(查询到数据的总记录数),
所以用ResultMap返回数据
*/
public class ResultMap<T> {
private String msg;
private T data;
private int code;
private int count;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ResultMap(String msg, T data, int code, int count) {
this.msg = msg;
this.data = data;
this.code = code;
this.count = count;
}
public ResultMap() {
}
}
4.Ctroller层
@RequestMapping("/getUserData")
@ResponseBody
public ResultMap<List<User>> getUserData(User user,Integer page,@RequestParam("limit") Integer limit){
Map<String,Object> result = new HashMap<String,Object>();
PageInfo<User> userPageInfo =userService.getUserData(user,page,limit);
// 需要参数code(要为0,不然数据表格数据显示不出),
// * msg(返回的消息),
// * data(表格显示的数据),
// * totals(查询到数据的总记录数),q
List<User> userList = userPageInfo.getList();
Integer totals =Integer.parseInt(String.valueOf(userPageInfo.getTotal()));;
return new ResultMap<List<User>>("",userList,0,totals);
}
5.Service层
public PageInfo<User> getUserData(User user, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.getUserData(user);
PageInfo<User> userPageInfo = new PageInfo<User>(userList);
return userPageInfo;
}
6.Mapper.xml
<!--多条件查询用户-->
<select id="getUserData"
parameterType="User"
resultMap="UserMap">
SELECT <include refid="Base_Column_List"/> from sys_user
<where>
<if test="userCode != null and userCode != '' ">
AND USER_CODE LIKE concat ('%',#{userCode},'%')
</if>
.....
</if>
</where>
</select>
7.结果截图