背景:

mybatis 常用数据查询的方法都是先建实体类,再建Mapper,最后写Service,如果只是单纯的去查询数据显示,这样操作太麻烦。本文就以mybatis +layui创建通用分页类,简化这一操作,提升开发效率(没考虑注入情况读者自行修改)

有需要的同学可以下载 示例代码

先看效果

1.添加静态文件资源

把UI静态文件拷入resources/static文件夹中

2.添加分页dao

@Mapper
@Repository
public interface PageDao {@Select("select count(1) from ${table}")int findCountAll(@Param("table") String table);@Select("select * from ${table} limit #{pagestart}, #{pagesize}")List<HashMap<String, Object>> findMapPageAll(@Param("table") String table, @Param("pagestart") int pagestart, @Param("pagesize") int pagesize);@Select("select count(1) from ${table} where ${strWhere}")int findCount(@Param("table") String table, @Param("strWhere") String strWhere);@Select("select ${fileds} from ${table} where ${strWhere} ${order} limit #{pagestart}, #{pagesize}")List<HashMap<String, Object>> findMapPage(@Param("table") String table,@Param("fileds") String fileds,@Param("order") String order,@Param("strWhere") String strWhere,@Param("pagestart") int pagestart,@Param("pagesize") int pagesize);@Select("select ${fileds} from ${table} where ${strWhere} ${order}")List<HashMap<String, Object>> findMapAll(@Param("table") String table,@Param("fileds") String fileds,@Param("order") String order,@Param("strWhere") String strWhere);}

3.添加PageService

@Service
public class PageService {@AutowiredPageDao pageDao;public int findCount(String table){return  pageDao.findCountAll(table);}public List<HashMap<String, Object>> findMapPage(String table, int pagestart, int pagesize){return  pageDao.findMapPageAll(table,pagestart, pagesize);}public int findCount(String table, String strWhere){return  pageDao.findCount(table,strWhere);}public List<HashMap<String, Object>> findMapPage(String table,String fileds,String order,String strWhere,int pagestart,int pagesize){return  pageDao.findMapPage(table,fileds,order,strWhere,pagestart,pagesize);}public List<HashMap<String, Object>> findMapAll(String table,String fileds,String order,String strWhere){return  pageDao.findMapAll(table,fileds,order,strWhere);}}

4.建立Controller

 @AutowiredPageService pageService;@RequestMapping(value = "/pagelist", method = {RequestMethod.GET, RequestMethod.POST})public String pagelist() {return "pagelist";}@RequestMapping("/pagelsitquery")@ResponseBodypublic String pagelsitquery(HttpServletResponse response, ModelMap modelMap, PageSearch pageConf) throws IOException {int currentPage = pageConf.getCurrentPage();int pageSize = pageConf.getPageSize();int pagestart=(currentPage-1)*pageSize;String tables = "groupidinfo t   ";String files = "t.*";String order = "order by t.id desc ";String where = "1=1";if(!StringUtils.isBlank(pageConf.getKey())){String key=pageConf.getKey();where=where+" and  groupId like '%"+key+"%' ";}int total=pageService.findCount(tables,where);List<HashMap<String, Object>> map= pageService.findMapPage(tables,files,order,where,pagestart,pageSize);Map<String, Object> obj=new HashMap<String,Object>();obj.put("total",total);obj.put("list",map);ObjectMapper objectMapper = new ObjectMapper();String jsonString=objectMapper.writeValueAsString(obj);return  jsonString;}

5.创建HTML,关键JS

/*** 初始化layui分页*/function initLayPage(pageConf) {if(!pageConf){pageConf ={};pageConf.pageSize = 20;pageConf.currentPage = 1;}key=$('#key').val();pageConf.key=key;console.log(pageConf);$.post("pagelsitquery", pageConf, function (data) {layui.use(['laypage', 'layer'], function () {var page = layui.laypage;page.render({elem: 'layui',count: data.total,curr: pageConf.currentPage,limit: pageConf.pageSize,first:"首页",last:"尾页",layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],jump: function (obj, first) {if (!first) {pageConf.currentPage = obj.curr;pageConf.pageSize = obj.limit;initLayPage(pageConf);}}});fillTable(data.list,(pageConf.currentPage - 1) * pageConf.pageSize); //页面填充
                })},"json");}//填充表格数据
        function fillTable(data,num) {var info = '';$.each(data, function (index, obj) {info += '<tr>'+'<td>' + getValue(obj.id) + '</td>'+'<td>' + getValue(obj.groupId) + '</td>'+'<td>' + getValue(obj.version) + '</td>'+'<td>' + getValue(obj.cluster) + '</td>'+'<td>' + getValue(obj.timeout) + '</td>'+'<td>' + getValue(obj.retries) + '</td>'+'</tr>';});//alert(info);$("#tab_list").html(info);}function getValue(v) {if(v==undefined)return "";return v;}

转载于:https://www.cnblogs.com/Guroer/p/10201120.html

Spring Boot mybatis HashMap +layui 通用分页相关推荐

  1. spring boot + mybatis + layui + shiro后台权限管理系统

    后台管理系统 版本更新 后续版本更新内容 链接入口: springboot + shiro之登录人数限制.登录判断重定向.session时间设置:https://blog.51cto.com/wyai ...

  2. 第七章、Spring Boot MyBatis升级篇

    课时二十七.Spring Boot MyBatis升级篇-注解 缘起:在一节视频中,有这么一段留言:"会不会推出SpringBoot整合Mybaits配置文件sqlMapConfig.xml ...

  3. 前端Vue+ElementUI的Pagination分页组件实现分页展示 后端Spring Boot +Mybatis Plus实现分页接口

    前端Vue+ElementUI的Pagination分页组件实现分页展示 & 后端Spring Boot +Mybatis Plus实现分页接口 很久没有更新博客了,主要原因是博主一直在补充自 ...

  4. 【Spring boot】IDEA + Maven + Spring Boot + Mybatis + Druid + PageHelper

    在第一篇Spring Boot 框架搭建博客中,底层用的是jpa,这篇博客底层选择的mybatis,之前搭建过Spring Boot + Mybatis的框架,用的是Spring Boot 1.5.9 ...

  5. 商城项目(一)使用Spring boot + Mybatis搭建

    Spring boot + Mybatis基础架构 环境搭建 mysql 8 mysql客户端连接工具 Valentina Studio springboot 版本:2.1.3.RELEASE Myb ...

  6. spring boot+mybatis整合

    LZ今天自己搭建了下Spring boot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多.其实只用Spring boot也可以开发,但是对于多表多条件分页查询,Sp ...

  7. Spring Boot + Mybatis 实现动态数据源

    动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...

  8. 从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建

    从零搭建一个 Spring Boot 开发环境!Spring Boot+Mybatis+Swagger2 环境搭建 本文简介 为什么使用Spring Boot 搭建怎样一个环境 开发环境 导入快速启动 ...

  9. Spring boot Mybatis 整合(完整版)

    Spring boot Mybatis 整合(完整版) 更多干货 SpringBoot系列目录 正题 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 springboot: ...

最新文章

  1. 根据之前发的那SQL语句查询表结构的语句做了个MSSQL实体类生成器!
  2. rnn词性标注算法_Python预测算法哪家强?权游龙妈是生还是凉凉?
  3. 博客园——记录我的开始
  4. N皇后问题的两个最高效的算法
  5. 【linux所有命令——复习】
  6. 利用python开发微信JS-JDK(基于python3.6)
  7. 用MATLAB计算光的等厚干涉实验中的不确定度
  8. vcpkg安装和使用--学习入门
  9. labelimg安装教程
  10. java 16进制转字符串 乱码_Java中16进制与字符串之间的相互转换
  11. Mac 如何免费支持NTFS 格式移动硬盘读写
  12. 超详细解析:Python输出水仙花数
  13. luogu P4234 最小差值生成树
  14. php 表情,php emoji表情处理
  15. Struts2文件的下载
  16. Dremel made simple with Parquet(CN)
  17. 关于php中的print EOF
  18. C++串口同步和异步的读取与串口设备编程
  19. python合并单元格居中_Python基于xlrd模块处理合并单元格
  20. lankecms企业网站漏洞导致被入侵篡改跳转

热门文章

  1. apache java cache-control,Tomcat: Cache-Control
  2. html 导航栏跟着动_“跟着导航来,现在不敢动!”浙江深山男游客吓坏,4岁儿子冻得直跺脚...
  3. 大数据要学javaweb吗_大数据是私有财产吗?
  4. linux+vi+注掉代码,VI编辑器之删除操作(示例代码)
  5. 使用计算机报点系统时填记,子案例库接发列车工作-企业生产实际教学案例库...
  6. Mysql控制流语句
  7. 跟着开源项目学因果推断——whynot(十四)
  8. lr中错误解决方法(收集)
  9. PDF Expert 坚果云 强强联合 优惠来袭
  10. linux查看磁盘io的几种方法