spring boot+layui分页实战

项目用了layui,做了个简单的图书搜索页,分享出来。

喜欢的朋友给点个赞!!!

 

实现效果

 

开发步骤

1.前端页面和JS


   
   
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>搜索图书 </title>
  8. <link rel="stylesheet" href="/static/layui/css/layui.css" th:href="@{/static/layui/css/layui.css}">
  9. <style type="text/css">
  10. 省略...
  11. </style>
  12. </head>
  13. <body>
  14. <div class="main">
  15. <h1 class="site-h1"> <i class="layui-icon"> &#xe656; </i>Bookman图书检索 </h1>
  16. <form class="layui-form">
  17. <div class="layui-form-item">
  18. <div class="input-opt">
  19. <select name="condition" lay-verify="required">
  20. <option value="isbn">ISBN号 </option>
  21. <option value="author">作者 </option>
  22. <option value="name">名称 </option>
  23. </select>
  24. </div>
  25. <div class="input-text">
  26. <input type="text" name="keyword" required lay-verify="required" placeholder="请输入" autocomplete="off"
  27. class= "layui-input">
  28. </div>
  29. </div>
  30. <div class="layui-form-item">
  31. <button class="layui-btn layui-btn-submit" lay-submit="" lay-filter="formDemo">搜索 </button>
  32. </div>
  33. </form>
  34. <!-- 列表 -->
  35. <div class="booklist">
  36. <table class="items">
  37. </table>
  38. <!--分页-->
  39. <div id="pager"> </div>
  40. </div>
  41. </div>
  42. <script src="/static/js/jquery-1.11.3.min.js" th:src="@{/static/js/jquery-1.11.3.min.js}"> </script>
  43. <script src="/static/layui/layui.js" th:src="@{/static/layui/layui.js}"> </script>
  44. <!--ctx-->
  45. <script th:replace="~{fragment::ctx}"/>
  46. <script>
  47. var form, laypage;
  48. // 请求参数
  49. var param = {};
  50. layui. use([ 'form', 'laypage', 'jquery'], function ( ) {
  51. form = layui. form;
  52. laypage = layui. laypage, $ = layui. $;
  53. //监听提交
  54. form. on( 'submit(formDemo)', function ( data) {
  55. param. name = "";
  56. param. isbn = "";
  57. param. author = "";
  58. if (data. field. condition == "isbn") {
  59. param. isbn = data. field. keyword;
  60. }
  61. if (data. field. condition == "name") {
  62. param. name = data. field. keyword;
  63. }
  64. if (data. field. condition == "author") {
  65. param. author = data. field. keyword;
  66. }
  67. showRecord( 1, 5,param);
  68. return false;
  69. });
  70. });
  71. function showRecord( pageNo, pageSize, param) {
  72. $. get(ctx+ "api/book/list",
  73. {
  74. author: param. author,
  75. name: param. name,
  76. isbn: param. isbn,
  77. page: pageNo,
  78. limit: pageSize
  79. },
  80. function ( result) {
  81. // 展示数据
  82. fillPage(pageNo, pageSize, result. data);
  83. // 渲染分页
  84. laypage. render({
  85. elem: $( '#pager')
  86. , count: result. count //数据总数,从服务端得到
  87. , limit: 5 //每页显示条数
  88. , limits: [ 5, 10, 15]
  89. , curr: 1 //起始页
  90. , groups: 5 //连续页码个数
  91. , prev: '上一页' //上一页文本
  92. , netx: '下一页' //下一页文本
  93. , first: 1 //首页文本
  94. , last: 100 //尾页文本
  95. , layout: [ 'prev', 'page', 'next', 'limit', 'skip']
  96. //跳转页码时调用
  97. , jump: function ( obj, first) { //obj为当前页的属性和方法,第一次加载first为true
  98. //非首次加载 do something
  99. if (!first) {
  100. //调用加载函数加载数据
  101. getPage(obj. curr,obj. limit,param);
  102. }
  103. }
  104. });
  105. }
  106. );
  107. }
  108. function getPage( pageNo, pageSize, param) {
  109. var result1;
  110. $. get(ctx+ "api/book/list",
  111. {
  112. author: param. author,
  113. name: param. name,
  114. isbn: param. isbn,
  115. page: pageNo,
  116. limit: pageSize
  117. },
  118. function ( result) {
  119. fillPage(pageNo, pageSize, result. data);
  120. }
  121. );
  122. }
  123. function fillPage( pageNo, pageSize, data) {
  124. var start=pageNo== 1? 1:(pageNo- 1)*pageSize+ 1;
  125. $( '.items'). empty();
  126. //加载后台返回的List集合数据
  127. var href= "";
  128. for ( var i = 0; i < data. length; i++) {
  129. href=ctx+ "bookDetail/"+data[i]. id;
  130. var td = $( "<td class='col1'></td>"). text(start+i);
  131. var td2 = $( "<td class='cover'><a href='"+href+ "' target='_blank'><img src='static/img/nopic.png'></a></td>");
  132. var td3 = $( "<td class='col2'></td>");
  133. var div = $( "<div class='itemtitle'><a href='"+href+ "' target='_blank'>"+data[i]. name+ "</a></div>");
  134. var tb = $( "<table><tr><td class='label1'>作者:</td><td class='content'>"+data[i]. author+ "</td>" +
  135. "<td class='label1'>ISBN:</td><td class='content'>"+data[i]. isbn+ "</td></tr></table>")
  136. td3. append(div,tb);
  137. var tr = $( "<tr class='item'></tr>"). append(td, td2, td3);
  138. $( '.items'). append(tr);
  139. }
  140. }
  141. </script>
  142. </body>
  143. </html>

 

2.数据层


   
   
  1. <select id= "count" parameterType= "Map" resultType= "java.lang.Integer">
  2. select count (*) from tb_book
  3. <where>
  4. < if test= "isbn!=null and isbn!=''">
  5. and isbn = #{isbn}
  6. </ if>
  7. < if test= "name!=null and name!=''">
  8. and name like concat ('%',#{name},'%')
  9. </ if>
  10. < if test= "author!=null and author!=''">
  11. and author like concat ('%',#{author},'%')
  12. </ if>
  13. </where>
  14. </select>

   
   
  1. <select id= "selectPageResult" parameterType= "map" resultType= "com.laoxu.java.bookman.model.Book">
  2. select *
  3. from tb_book
  4. <where>
  5. < if test= "isbn!=null and isbn!=''">
  6. and isbn = #{isbn}
  7. </ if>
  8. < if test= "name!=null and name!=''">
  9. and name like concat ('%',#{name},'%')
  10. </ if>
  11. < if test= "author!=null and author!=''">
  12. and author like concat ('%',#{author},'%')
  13. </ if>
  14. </where>
  15. limit #{offset}, #{rows}
  16. </select>

 

3.服务层


   
   
  1. package com.laoxu.java.bookman.service;
  2. import com.laoxu.java.bookman.framework.AbstractService;
  3. import com.laoxu.java.bookman.model.Book;
  4. import org.springframework.stereotype.Service;
  5. import java.util.List;
  6. import java.util.Map;
  7. /**
  8. * @Description: 图书服务
  9. * @Author laoxu
  10. * @Date 2019/5/2 9:26
  11. **/
  12. @Service
  13. public class BookService extends AbstractService {
  14. public void add (Book entity) {
  15. //String username = SecurityUtil.getLoginUser();
  16. insert( "bookMapper.insert",entity);
  17. }
  18. public void modify (Book entity) {
  19. update( "bookMapper.update",entity);
  20. }
  21. public void remove (Long id) {
  22. delete( "bookMapper.delete",id);
  23. }
  24. public void removes (Long[] ids) {
  25. delete( "bookMapper.deletes",ids);
  26. }
  27. public Book get (Long id) {
  28. return selectOne( "bookMapper.select",id);
  29. }
  30. public Book getByIsbn (String isbn) {
  31. return selectOne( "bookMapper.selectByIsbn",isbn);
  32. }
  33. public List<Book> getParentList (Long id) {
  34. return selectList( "bookMapper.selectParentList",id);
  35. }
  36. public int count (Map<String, Object> param) {
  37. return selectOne( "bookMapper.count",param);
  38. }
  39. public List<Book> getList (Map<String, Object> param) {
  40. return selectList( "bookMapper.selectList",param);
  41. }
  42. public List<Book> getPageResult (Map<String, Object> param) {
  43. return selectList( "bookMapper.selectPageResult",param);
  44. }
  45. public int checkCode (Book entity){
  46. return selectOne( "bookMapper.countCode",entity);
  47. }
  48. }

 

4.控制层


   
   
  1. @GetMapping("/list")
  2. public ResultBean<List<Book>> getPageResult (
  3. @RequestParam(required = false) String name,
  4. @RequestParam(required = false) String isbn,
  5. @RequestParam(required = false) String author,
  6. @RequestParam(defaultValue = "1") Integer page,
  7. @RequestParam(defaultValue = "10") Integer limit) {
  8. Map<String, Object> param = new HashMap<>();
  9. // 计算起始行号
  10. int offset = (page - 1) * limit;
  11. int rows = limit;
  12. param.put( "name",name);
  13. param.put( "isbn",isbn);
  14. param.put( "author",author);
  15. param.put( "offset", offset);
  16. param.put( "rows", rows);
  17. // 统计记录数
  18. int totalRows = bookService.count(param);
  19. // 获取当前页结果集
  20. List<Book> entities = bookService.getPageResult(param);
  21. ResultBean result = new ResultBean( 0, "查询成功", totalRows, entities);
  22. return result;
  23. }

 

5.数据格式参考

{
    "code": 0,
    "msg": "查询成功",
    "count": 1,
    "data": [{
        "id": 10,
        "createTime": "2020-01-12T11:22:49.000+0000",
        "creater": null,
        "updateTime": "2020-02-04T15:31:28.000+0000",
        "updater": null,
        "name": "大秦帝国",
        "isbn": "111",
        "categoryCode": "10",
        "categoryName": "K 历史、地理",
        "author": "孙皓晖",
        "publisherCode": "7-302",
        "publisherName": "清华大学出版社-北京",
        "price": 588,
        "edition": 1,
        "translator": "",
        "languageCode": "CH",
        "languageName": "汉语",
        "pages": 1200,
        "words": 5000000,
        "locationCode": "一号架",
        "locationName": "一号架",
        "totalNumber": 122,
        "leftNumber": 4,
    }]
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值