关于SSM个人博客项目中,使用layui完成用户分页操作过程

 1.了解layui前后端交互,所传递与接收到的参数列表

前台发送后台的参数有两个:

一个是page,即你定义的page页,第二个为limit,即一个页显示多少条数据.

例子:

    @RequestMapping(value="/listUser")
    @ResponseBody           //page页码, limit,一次查询的条数
    public ResultMap<List<UserCustom>> listUser(Page page, @RequestParam("limit") int limit) throws Exception {
        page.setPageSize(limit);
        System.out.println("page:"+page.toString());
        List<UserCustom> userList = userService.getUserListByPage(page);
        Integer totals = userService.getUserCountByPage(page);
        page.setTotalCount(totals);//总记录数
        return new ResultMap<List<UserCustom>>("",userList,0,totals);
    }

后台发送前台需要的格式:

后台要发送前台的格式必须是如下的格式。而且要传递成功的话,前面三个字段的code必须为0,layui判断code为0,即传递成功,显示数据。而且后台对于前台的字段要一一对应.

{
  "code": 0,
  "msg": "",
  "count": 15,
  "data": [
    {
      "userId": "1",
      "userName": "admin",
      "userEmail": "123@qq.com",
      ...
    },{
      "usersId": "2",
      "userName": "root",
      "userEmail": "123@qq.com",
      ...
    },
    ......
  ]
}

所以这里需要自定义两个类

第一个类:

Page类

package blog.Utils;

/**
 * 分页
 */
import java.io.Serializable;

public class Page implements Serializable {
	
	private static final long serialVersionUID = -3198048449643774660L;
	
	private int pageNow = 1; // 当前页数
	
	private int pageSize; // 每页显示记录的条数
	
	private int totalCount; // 总的记录条数
	
	private int totalPageCount; // 总的页数

	private String keyType; //查询关键词类型

	private String keyWord; //查询关键词

	private String userId; //用户id
	
	@SuppressWarnings("unused")
	private int startPos; // 开始位置,从0开始
	
	@SuppressWarnings("unused")
	private boolean hasFirst;// 是否有首页
	
	@SuppressWarnings("unused")
	private boolean hasPre;// 是否有前一页
	
	@SuppressWarnings("unused")
	private boolean hasNext;// 是否有下一页
	
	@SuppressWarnings("unused")
	private boolean hasLast;// 是否有最后一页

	public Page() {
	}

	/**
	 * 通过构造函数 传入  总记录数  和  当前页
	 * @param totalCount
	 * @param pageNow
	 */
	public Page(int totalCount, int pageNow, int pageSize) {
		this.totalCount = totalCount;
		this.pageNow = pageNow;
		this.pageSize = pageSize;
	}
	
	/**
	 * 取得总页数,总页数=总记录数/总页数
	 * @return
	 */
	public int getTotalPageCount() {
		totalPageCount = getTotalCount() / getPageSize();
		return (totalCount % pageSize == 0) ? totalPageCount
			: totalPageCount + 1;
	}
	
	public void setTotalPageCount(int totalPageCount) {
		this.totalPageCount = totalPageCount;
	}
	
	public int getPageNow() {
		return pageNow;
	}
	
	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public int getTotalCount() {
		return totalCount;
	}
	
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	/**
	 * 取得选择记录的初始位置
	 * @return
	 */
	public int getStartPos() {
		return (pageNow - 1) * pageSize;
	}
	
	public void setStartPos(int startPos) {
		this.startPos = startPos;
	}
	
	/**
	 * 是否是第一页
	 * @return
	 */
	public boolean isHasFirst() {
		return (pageNow == 1) ? false : true;
	}
	
	public void setHasFirst(boolean hasFirst) {
		this.hasFirst = hasFirst;
	}
	/**
	 * 是否有首页
	 * @return
	 */
	public boolean isHasPre() {
		// 如果有首页就有前一页,因为有首页就不是第一页
		return isHasFirst() ? true : false;
	}
	
	public void setHasPre(boolean hasPre) {
		this.hasPre = hasPre;
	}
	/**
	 * 是否有下一页
	 * @return
	 */
	public boolean isHasNext() {
		// 如果有尾页就有下一页,因为有尾页表明不是最后一页
		return isHasLast() ? true : false;
	}
	
	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}
	/**
	 * 是否有尾页
	 * @return
	 */
	public boolean isHasLast() {
		// 如果不是最后一页就有尾页
		return (pageNow == getTotalCount()) ? false : true;
	}
	
	public void setHasLast(boolean hasLast) {
		this.hasLast = hasLast;
	}

	public String getKeyType() {
		return keyType;
	}

	public void setKeyType(String keyType) {
		this.keyType = keyType;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
}

第二个类ResultMap<T> 这里用泛型。 很明显,里面必须有四个成员变量,code,msg,data,count

package blog.pojo.custom;




public class ResultMap<T> {
    private String msg;
    private T data;
    private int code;
    private int count;

    public ResultMap() {
    }

    public ResultMap(String msg, T data, int code, int count) {
        this.msg = msg;
        this.data = data;
        this.code = code;
        this.count = 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;
    }
}

完成了上面两步之后

接下来就是完成mapper、service、controller

1.Mapper

我这里由于是对user进行查询所以对应的查询语句为:

//UserMapper.java
	//分页查询用户
	public List<UserCustom> listUserPage(Page page) throws Exception;

	//分页查询用户的总条数
	public Integer listUserCount(Page page) throws Exception;

//UserMapper.xml

    <select id="listUserPage" parameterType="blog.Utils.Page" resultMap="BaseResultMap">
        select
        <include refid="user_table_all_columns"/>
        from
        `user`
        <where>
            <if test="userId != null and userId !=''">AND user_id = #{userId}</if>
            <if test="keyWord!= '' and keyType == 'userId'">AND user_id like '%' #{keyWord} '%'</if>
            <if test="keyWord!= '' and keyType =='userNickname'">AND user_id like '%' #{keyWord} '%'</if>
            <if test="keyWord!= '' and keyType =='userEmail'">AND user_id like '%' #{keyWord} '%'</if>
        </where>
        order by user_id DESC
        limit #{startPos},#{pageSize}
    </select>

    <select id="listUserCount" parameterType="blog.Utils.Page" resultType="Integer">
        select count(*) from
        `user`
        <where>
            <if test="userId != null and userId !=''">AND user_id = #{userId}</if>
            <if test="keyWord!= '' and keyType == 'userId'">AND user_id like '%' #{keyWord} '%'</if>
            <if test="keyWord!= '' and keyType =='userNickname'">AND user_id like '%' #{keyWord} '%'</if>
            <if test="keyWord!= '' and keyType =='userEmail'">AND user_id like '%' #{keyWord} '%'</if>
        </where>
    </select>

 接下来就是service层了

我由于只是查询了一下,并没有什么别的操作所以就直接调用usermapper.java里面的两个方法就可以了


	//根据查询条件分页显示用户
	public List<UserCustom> getUserListByPage(Page page) throws Exception;

	//根据查询条件分页显示用户数
	public Integer getUserCountByPage(Page page) throws Exception;


//UserServiceImpl

	@Override
	public List<UserCustom> getUserListByPage(Page page) throws Exception {
		List<UserCustom> customList = userMapperCustom.listUserPage(page);
		return customList;
	}

	@Override
	public Integer getUserCountByPage(Page page) throws Exception {
		Integer count = userMapperCustom.listUserCount(page);
		return count;
	}

 

Controller层

    @RequestMapping(value="/listUser")
    @ResponseBody           //page页码, limit,一次查询的条数
    public ResultMap<List<UserCustom>> listUser(Page page, @RequestParam("limit") int limit) throws Exception {
        page.setPageSize(limit);
        System.out.println("page:"+page.toString());
        List<UserCustom> userList = userService.getUserListByPage(page);
        Integer totals = userService.getUserCountByPage(page);
        page.setTotalCount(totals);//总记录数
        return new ResultMap<List<UserCustom>>("",userList,0,totals);
    }

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值