SpringBoot重点详解--使用BootstrapTable进行分页展示

谁借莪1个温暖的怀抱¢ 2022-04-02 09:10 498阅读 0赞

目录

前端HTML页面

后台Java辅助代码

Mongodb分页实现

Mybatis分页实现

分页页面展示


前端HTML页面

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width" />
  5. <title>BootstrapTable分页</title>
  6. <link rel="stylesheet" href="/css/bootstrap.min.css" />
  7. <link rel="stylesheet" href="/css/bootstrap-table.min.css" />
  8. <script type="text/javascript" src="/js/jquery-1.11.1.min.js"></script>
  9. </head>
  10. <body>
  11. <div class="bootstrap-table">
  12. <table id="exampleTable" data-mobile-responsive="true">
  13. </table>
  14. </div>
  15. <script type="text/javascript" src="/js/bootstrap-table.min.js"></script>
  16. <script type="text/javascript" src="/js/bootstrap.min.js"></script>
  17. <script type="text/javascript" src="/js/demo.js"></script>
  18. </body>
  19. </html>

demo.js 内容如下:

  1. $(function() {
  2. load();
  3. });
  4. function load() {
  5. $('#exampleTable').bootstrapTable({
  6. url : "/demo/load/data", // 请求的后台URL(*)
  7. method : 'get', // 请求方式:get/post(*)
  8. showRefresh : false, // 是否显示刷新按钮
  9. showToggle : false, // 是否显示详细视图和列表视图的切换按钮
  10. showColumns : false, // 是否显示列操作按钮
  11. detailView : false, // 是否显示详细视图
  12. striped : true, // 设置为true会有隔行变色效果
  13. dataType : "json", // 服务器返回的数据类型
  14. pagination : true, // 设置为true会在底部显示分页条
  15. // queryParamsType : "limit",
  16. // 设置为limit则会发送符合RESTFull格式的参数
  17. singleSelect : true, // 设置为true将禁止多选
  18. clickToSelect : true, // 是否启用点击选中行
  19. // contentType : "application/x-www-form-urlencoded",
  20. // 发送到服务器的数据编码类型
  21. pageSize : 10, // 如果设置了分页,每页数据条数
  22. pageNumber : 1, // 如果设置了分布,首页页码
  23. search : false, // 是否显示搜索框
  24. sidePagination : "server", // 设置在哪里进行分页,可选值为"client" 或者 "server"
  25. queryParams : function(params) {
  26. return {
  27. // 说明:传入后台的参数包括offset开始索引,limit步长,sort排序列,order:desc或者,以及所有列的键值对
  28. limit : params.limit,
  29. pageSize : 10,
  30. offset : params.offset,
  31. search : params.search,
  32. sort : "age",
  33. order : "DESC",
  34. name : "user"
  35. };
  36. },
  37. // 请求服务器数据时,你可以通过重写参数的方式添加一些额外的参数,例如 toolbar 中的参数 如果
  38. // queryParamsType = 'limit' ,返回参数必须包含
  39. // limit, offset, search, sort, order 否则, 需要包含:
  40. // pageSize, pageNumber, searchText, sortName,
  41. // sortOrder.
  42. // 返回false将会终止请求
  43. columns : [ {
  44. title : '序号',
  45. field : 'id',
  46. align : 'left',
  47. valign : 'center',
  48. width : '10%',
  49. formatter : function(value, row, index) {
  50. return index + 1;
  51. }
  52. }, {
  53. title : '用户ID',
  54. field : 'id',
  55. align : 'left',
  56. valign : 'center',
  57. width : '20%'
  58. }, {
  59. title : '用户姓名',
  60. field : 'name',
  61. align : 'left',
  62. valign : 'center',
  63. width : '50%',
  64. cellStyle : function(value, row, index) {
  65. return {
  66. css : {
  67. "word-wrap" : "break-word",
  68. "word-break" : "normal"
  69. }
  70. };
  71. }
  72. }, {
  73. title : '用户年龄',
  74. field : 'age',
  75. align : 'left',
  76. valign : 'center',
  77. width : '20%'
  78. }]
  79. });
  80. }

后台Java辅助代码

在此我们分别对 MongoTemplate 和 Mybatis 的分页方法进行举例:

  1. import java.util.List;
  2. import java.util.Map;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. import com.pengjunlee.common.utils.PageUtils;
  10. import com.pengjunlee.common.utils.Query;
  11. import com.pengjunlee.dao.MongoDao;
  12. import com.pengjunlee.domain.UserEntity;
  13. import com.pengjunlee.service.MysqlService;
  14. @Controller
  15. @RequestMapping("/demo")
  16. public class DemoController {
  17. @Autowired
  18. MongoDao mongoDao;
  19. @Autowired
  20. MysqlService mysqlService;
  21. @GetMapping("/index")
  22. public String index() {
  23. return "index";
  24. }
  25. // 两个 /load/data 二选一
  26. @ResponseBody
  27. @GetMapping("/load/data")
  28. public PageUtils loadFromMongo(@RequestParam Map<String, Object> params) {
  29. for (Map.Entry<String, Object> entry : params.entrySet()) {
  30. System.out.println(entry.getKey() + " : " + entry.getValue());
  31. }
  32. PageUtils pageUtils = mongoDao.pageUserByCond(params);
  33. return pageUtils;
  34. }
  35. // 两个 /load/data 二选一
  36. @GetMapping("/load/data")
  37. @ResponseBody
  38. PageUtils loadFromMysql(@RequestParam Map<String, Object> params) {
  39. Query query = new Query(params);
  40. List<UserEntity> userList = mysqlService.list(query);
  41. int total = mysqlService.count(query);
  42. PageUtils pageUtil = new PageUtils(userList, total);
  43. return pageUtil;
  44. }
  45. }

PageUtils 是一个分页查询结果的封装类:

  1. import java.io.Serializable;
  2. import java.util.List;
  3. public class PageUtils implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. private int total;
  6. private List<?> rows;
  7. public PageUtils(List<?> list, int total) {
  8. this.rows = list;
  9. this.total = total;
  10. }
  11. public int getTotal() {
  12. return total;
  13. }
  14. public void setTotal(int total) {
  15. this.total = total;
  16. }
  17. public List<?> getRows() {
  18. return rows;
  19. }
  20. public void setRows(List<?> rows) {
  21. this.rows = rows;
  22. }
  23. }

Query 是一个分页查询条件的封装类:

  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. public class Query extends LinkedHashMap<String, Object> {
  4. private static final long serialVersionUID = 1L;
  5. // 偏移页数
  6. private int offset;
  7. // 每页条数
  8. private int limit;
  9. public Query(Map<String, Object> params) {
  10. this.putAll(params);
  11. // 分页参数
  12. this.offset = Integer.parseInt(params.get("offset").toString());
  13. this.limit = Integer.parseInt(params.get("limit").toString());
  14. this.put("offset", offset);
  15. this.put("page", offset / limit + 1);
  16. this.put("limit", limit);
  17. }
  18. public int getOffset() {
  19. return offset;
  20. }
  21. public void setOffset(int offset) {
  22. this.put("offset", offset);
  23. }
  24. public int getLimit() {
  25. return limit;
  26. }
  27. public void setLimit(int limit) {
  28. this.limit = limit;
  29. }
  30. }

定义的用户实体类 UserEntity 代码如下:

  1. import java.io.Serializable;
  2. public class UserEntity implements Serializable {
  3. private static final long serialVersionUID = 1L;
  4. private String id;
  5. private String name;
  6. private Integer age;
  7. public String getId() {
  8. return id;
  9. }
  10. public void setId(String id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public Integer getAge() {
  20. return age;
  21. }
  22. public void setAge(Integer age) {
  23. this.age = age;
  24. }
  25. }

Mongodb分页实现

  1. import java.util.Map;
  2. import com.pengjunlee.common.utils.PageUtils;
  3. import com.pengjunlee.domain.UserEntity;
  4. public interface MongoDao {
  5. void upsertUser(UserEntity user);
  6. PageUtils pageUserByCond(Map<String, Object> params);
  7. }
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.domain.Sort;
  14. import org.springframework.data.domain.Sort.Direction;
  15. import org.springframework.data.mongodb.core.MongoTemplate;
  16. import org.springframework.data.mongodb.core.query.Criteria;
  17. import org.springframework.data.mongodb.core.query.Query;
  18. import org.springframework.data.mongodb.core.query.Update;
  19. import org.springframework.stereotype.Component;
  20. import com.pengjunlee.common.utils.PageUtils;
  21. import com.pengjunlee.dao.MongoDao;
  22. import com.pengjunlee.domain.UserEntity;
  23. @Component
  24. public class MongoDaoImpl implements MongoDao {
  25. @Autowired
  26. private MongoTemplate mongoTemplate;
  27. @Override
  28. public void upsertUser(UserEntity user) {
  29. Query query = new Query(Criteria.where("id").is(user.getId()));
  30. Update update = Update.update("id", user.getId());
  31. if (StringUtils.isNotBlank(user.getName())) {
  32. update.set("name", user.getName());
  33. }
  34. if (null != user.getAge()) {
  35. update.set("age", user.getAge());
  36. }
  37. mongoTemplate.upsert(query, update, UserEntity.class);
  38. }
  39. @Override
  40. public PageUtils pageUserByCond(Map<String, Object> params) {
  41. int offset = 0;
  42. int limit = 10;
  43. if (null != params) {
  44. if (StringUtils.isNotBlank(params.get("offset").toString())) {
  45. offset = Integer.parseInt(params.get("offset").toString());
  46. }
  47. if (StringUtils.isNotBlank(params.get("limit").toString())) {
  48. limit = Integer.parseInt(params.get("limit").toString());
  49. }
  50. }
  51. Query query = null;
  52. if (StringUtils.isNotBlank(params.get("name").toString())) {
  53. query = new Query(Criteria.where("name").is(params.get("name").toString()));
  54. } else {
  55. query = new Query(new Criteria());
  56. }
  57. int count = mongoTemplate.find(query, UserEntity.class).size();
  58. if (StringUtils.isNotBlank(params.get("sort").toString())
  59. && StringUtils.isNotBlank(params.get("order").toString())) {
  60. query.with(new Sort(Direction.valueOf(params.get("order").toString().toUpperCase()),
  61. params.get("sort").toString()));
  62. }
  63. query.skip(offset);
  64. query.limit(limit);
  65. List<UserEntity> users = mongoTemplate.find(query, UserEntity.class);
  66. PageUtils pageUtils = new PageUtils(new ArrayList<>(), 0);
  67. pageUtils.setTotal(count);
  68. pageUtils.setRows(users);
  69. return pageUtils;
  70. }
  71. }

Mybatis分页实现

  1. import java.util.List;
  2. import java.util.Map;
  3. import com.pengjunlee.domain.UserEntity;
  4. public interface MysqlService {
  5. List<UserEntity> list(Map<String, Object> params);
  6. int count(Map<String, Object> params);
  7. }
  8. import java.util.List;
  9. import java.util.Map;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import com.pengjunlee.dao.MysqlDao;
  13. import com.pengjunlee.domain.UserEntity;
  14. import com.pengjunlee.service.MysqlService;
  15. @Service
  16. public class MysqlServiceImpl implements MysqlService {
  17. @Autowired
  18. private MysqlDao mysqlDao;
  19. @Override
  20. public List<UserEntity> list(Map<String, Object> params) {
  21. return mysqlDao.list(params);
  22. }
  23. @Override
  24. public int count(Map<String, Object> params) {
  25. return mysqlDao.count(params);
  26. }
  27. }
  28. import java.util.List;
  29. import java.util.Map;
  30. import org.apache.ibatis.annotations.Mapper;
  31. import com.pengjunlee.domain.UserEntity;
  32. @Mapper
  33. public interface MysqlDao {
  34. List<UserEntity> list(Map<String, Object> params);
  35. int count(Map<String, Object> params);
  36. }
  37. <?xml version="1.0" encoding="UTF-8"?>
  38. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  39. <mapper namespace="com.pengjunlee.dao.MysqlDao">
  40. <select id="list" resultType="com.pengjunlee.domain.UserEntity">
  41. select
  42. `id`,`name`,`age`
  43. from
  44. tbl_user
  45. <where>
  46. <if test="id != null"> and id = #{id} </if>
  47. <if test="name != null and name != ''"> and name = #{name} </if>
  48. <if test="age != null"> and age = #{age} </if>
  49. </where>
  50. <choose>
  51. <when test="sort != null and sort.trim() != ''">
  52. order by ${sort} ${order}
  53. </when>
  54. <otherwise>
  55. order by id desc
  56. </otherwise>
  57. </choose>
  58. <if test="offset != null and limit != null">
  59. limit #{offset}, #{limit}
  60. </if>
  61. </select>
  62. <select id="count" resultType="int">
  63. select count(*) from tbl_user
  64. <where>
  65. <if test="id != null"> and id = #{id} </if>
  66. <if test="name != null and name != ''"> and name = #{name} </if>
  67. <if test="age != null"> and age = #{age} </if>
  68. </where>
  69. </select>
  70. </mapper>

分页页面展示

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3BlbmdqdW5sZWU_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,498人围观)

还没有评论,来说两句吧...

相关阅读