1. Understand the interaction between the front and back end of the layui, and the list of parameters passed and received
There are two parameters for the foreground to send the background:
One is page, that is, the page you defined, and the second is limit, that is, how many pieces of data are displayed in one page
Example:
@RequestMapping(value="/listUser") @ResponseBody //page number, limit, number of queries at a time 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);//Total number of records return new ResultMap<List<UserCustom>>("",userList,0,totals); }
Format required for background sending foreground:
The format of background to send foreground must be the following format. In addition, if the delivery is successful, the code of the first three fields must be 0, and the code judged by layui is 0, that is, the delivery is successful and the data is displayed. And the background should correspond to the foreground fields one by one
{ "code": 0, "msg": "", "count": 15, "data": [ { "userId": "1", "userName": "admin", "userEmail": "123@qq.com", ... },{ "usersId": "2", "userName": "root", "userEmail": "123@qq.com", ... }, ...... ] }
So we need to customize two classes
First class:
Class Page
package blog.Utils; /** * paging */ import java.io.Serializable; public class Page implements Serializable { private static final long serialVersionUID = -3198048449643774660L; private int pageNow = 1; // Current page number private int pageSize; // Number of records per page private int totalCount; // Total number of records private int totalPageCount; // Total page number private String keyType; //Query keyword type private String keyWord; //Query keywords private String userId; //User id @SuppressWarnings("unused") private int startPos; // Start position, starting from 0 @SuppressWarnings("unused") private boolean hasFirst;// Whether there is a homepage @SuppressWarnings("unused") private boolean hasPre;// Is there a previous page @SuppressWarnings("unused") private boolean hasNext;// Is there a next page @SuppressWarnings("unused") private boolean hasLast;// Is there a last page public Page() { } /** * Total records and current page passed in through constructor * @param totalCount * @param pageNow */ public Page(int totalCount, int pageNow, int pageSize) { this.totalCount = totalCount; this.pageNow = pageNow; this.pageSize = pageSize; } /** * Get total pages, total pages = total records / total pages * @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; } /** * Get the initial location of the selected record * @return */ public int getStartPos() { return (pageNow - 1) * pageSize; } public void setStartPos(int startPos) { this.startPos = startPos; } /** * Is it the first page * @return */ public boolean isHasFirst() { return (pageNow == 1) ? false : true; } public void setHasFirst(boolean hasFirst) { this.hasFirst = hasFirst; } /** * Whether there is a homepage * @return */ public boolean isHasPre() { // If there is a home page, there is a previous page, because there is a home page is not the first page return isHasFirst() ? true : false; } public void setHasPre(boolean hasPre) { this.hasPre = hasPre; } /** * Is there a next page * @return */ public boolean isHasNext() { // If there is a tail page, there will be the next page, because the tail page indicates that it is not the last page return isHasLast() ? true : false; } public void setHasNext(boolean hasNext) { this.hasNext = hasNext; } /** * Is there a back page * @return */ public boolean isHasLast() { // If it's not the last page, it's the last page 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; } }
The second class resultmap < T > uses generics here. Obviously, there must be four member variables, 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; } }
After completing the above two steps
The next step is to complete mapper, service and controller
1.Mapper
Because I query the user, the corresponding query statement is:
//UserMapper.java //Paging query user public List<UserCustom> listUserPage(Page page) throws Exception; //Total number of page query users 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>
Next is the service layer
I just query it and have no other operation, so I just call two methods in usermapper.java
//Display users by page according to query criteria public List<UserCustom> getUserListByPage(Page page) throws Exception; //Display the number of users by page according to the query criteria 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 layer
@RequestMapping(value="/listUser") @ResponseBody //page number, limit, number of queries at a time 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);//Total number of records return new ResultMap<List<UserCustom>>("",userList,0,totals); }
The effect is as follows:
(ps: due to the lack of front-end knowledge, the styles here are all layui)