此文章为分页与模糊查询

分页:

创建一个工具类   PageUtils:

package com.xinyi.mydemo.com.xinyi.util;public class PageUtils {//当前页private int currentPage;//上一页private int prevPage;//下一页private int nextPage;//尾页private int lastPage;//数据总条数private int count;//每页条数private int  pageSize;//分页计入数private int pageRecord;//分页private String page;public PageUtils(String currentPage,int count,int pageSize) {init(currentPage, count, pageSize);initPrevPage();initLastPage();initNextPage();initPageRecord();initPage();}//初始化变量的值private void init(String currentPage,int count,int pageSize) {if(currentPage==null||currentPage.equals("")) {currentPage="1";}this.currentPage = Integer.parseInt(currentPage);this.count = count;this.pageSize = pageSize;}//计算上一页private void initPrevPage() {if(currentPage==1) {prevPage = 1;}else {prevPage = currentPage-1;}}//计算最后一页private void initLastPage() {if(count%pageSize==0) {lastPage=count/pageSize;}else {lastPage=count/pageSize+1;}}//计算下一页private void initNextPage() {if(currentPage==lastPage) {nextPage=lastPage;}else {nextPage=currentPage+1;}}//计算计入数private void initPageRecord() {pageRecord = (currentPage-1)*pageSize;}private void initPage() {page = "第"+currentPage+"/"+lastPage+"页,共"+count+"条数据。";page +="<input type='button' value='首页' onclick='page(1)' >";page +="<input type='button' value='上一页' onclick='page("+prevPage+")' >";page +="<input type='button' value='下一页' onclick='page("+nextPage+")' >";page +="<input type='button' value='尾页' onclick='page("+lastPage+")' >";}public int getCurrentPage() {return currentPage;}public int getPrevPage() {return prevPage;}public int getNextPage() {return nextPage;}public int getLastPage() {return lastPage;}public int getCount() {return count;}public int getPageSize() {return pageSize;}public int getPageRecord() {return pageRecord;}public String getPage() {return page;}}

修改controller/StudentController为

package com.xinyi.mydemo.com.xinyi.controller;import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinyi.mydemo.com.xinyi.entity.Student;
import com.xinyi.mydemo.com.xinyi.service.StudentService;
import com.xinyi.mydemo.com.xinyi.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;@RestController
@RequestMapping("student")
public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("getStudentAll")public ModelAndView getStudentAll(@RequestParam(defaultValue = "1") String currentPage){ModelAndView mv = new ModelAndView();PageHelper.startPage(Integer.parseInt(currentPage),2);Page<Student> students = studentService.selectStudentWithPage();PageInfo<Student> studentPageInfo = students.toPageInfo();PageUtils pageUtils = new PageUtils(studentPageInfo.getPageNum() + "", (int) studentPageInfo.getTotal(), studentPageInfo.getPageSize());mv.addObject("list",studentPageInfo.getList());mv.addObject("page",pageUtils.getPage());mv.setViewName("selectStudent");return mv;}}

在 selectStudent.ftl

开头页面添加 selectStudent.js

list循环下添加分页:

在static/js目录下创建js文件    selectStudent.js

function page(i) {location.href = "getStudentAll?currentPage="+i;
}

分页完成

模糊查询:

前端:

在 selectStudent.ftl 页面 加入input 标签

<tr><td colspan="5">学生名称:<input type="text" value="${sname}"  name="sname"/>老师名称:<input type="text" value="${tname}" name="tname"/><input type="button" class="btn btn-primary" value="查询" onclick="querys()"/></td></tr>

在selectStudent.js中加入querys点击事件

function querys() {var sname = $("[name='sname']").val();var tname = $("[name='tname']").val();location.href = "getStudentAll?sname="+sname+"&tname="+tname;
}

修改selectStudent.js中的page

function page(i) {location.href = "getStudentAll?sname="+$("[name='sname']").val()+"&tname="+$("[name='tname']").val()+"&currentPage="+i;
}

后端:

修改StudentController

package com.xinyi.mydemo.com.xinyi.controller;import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinyi.mydemo.com.xinyi.entity.Student;
import com.xinyi.mydemo.com.xinyi.service.StudentService;
import com.xinyi.mydemo.com.xinyi.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;@RestController
@RequestMapping("student")
public class StudentController {@Autowiredprivate StudentService studentService;@GetMapping("getStudentAll")public ModelAndView getStudentAll(@RequestParam(defaultValue = "") String sname,@RequestParam(defaultValue = "") String tname,@RequestParam(defaultValue = "1") String currentPage){ModelAndView mv = new ModelAndView();PageHelper.startPage(Integer.parseInt(currentPage),2);Page<Student> students = studentService.selectStudentWithPage(sname,tname);PageInfo<Student> studentPageInfo = students.toPageInfo();PageUtils pageUtils = new PageUtils(studentPageInfo.getPageNum() + "", (int) studentPageInfo.getTotal(), studentPageInfo.getPageSize());mv.addObject("list",studentPageInfo.getList());mv.addObject("page",pageUtils.getPage());mv.addObject("sname",sname);mv.addObject("tname",tname);mv.setViewName("selectStudent");return mv;}}

修改 StudentService 接口

package com.xinyi.mydemo.com.xinyi.service;import com.github.pagehelper.Page;
import com.xinyi.mydemo.com.xinyi.entity.Student;public interface StudentService {Page<Student> selectStudentWithPage(String sname,String tname);
}

修改 StudentImpl

package com.xinyi.mydemo.com.xinyi.service.imp;import com.github.pagehelper.Page;
import com.xinyi.mydemo.com.xinyi.entity.Student;
import com.xinyi.mydemo.com.xinyi.mapper.StudentMapper;
import com.xinyi.mydemo.com.xinyi.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class StudentImpl implements StudentService {@Autowiredprivate StudentMapper studentMapper;@Overridepublic Page<Student> selectStudentWithPage(String sname,String tname) {return studentMapper.selectStudentWithPage(sname,tname);}
}

修改 StudentMapper

package com.xinyi.mydemo.com.xinyi.mapper;import com.github.pagehelper.Page;
import com.xinyi.mydemo.com.xinyi.entity.Student;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;@Mapper   //加入Mapper注解
public interface StudentMapper {Page<Student> selectStudentWithPage(@Param("sname") String sname,@Param("tname") String tname);
}

修改 mapper/StudentMapper.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.xinyi.mydemo.com.xinyi.mapper.StudentMapper"><select id="selectStudentWithPage" resultType="com.xinyi.mydemo.com.xinyi.entity.Student">select s.*,GROUP_CONCAT(t.tname)  as tnamefrom j_student s ,j_teacher t,j_db d where  d.sid = s.sid and d.tid = t.tidGROUP BY s.sidHAVING s.sname like concat('%',#{sname},'%') and tname like concat('%',#{tname},'%')</select></mapper>

运行项目测试:http://localhost:8080/student/getStudentAll

分页与模糊查询完成

有点累了····需要再写

大白话、最简单——SpringBoot+Mybatis+freemarker整合(二)相关推荐

  1. springboot+mybatis+redis整合

    springBoot+mybatis+redis整合,这里搭建一个简单的框架是为了记录怎么使用redis做缓存. 一.构建一个springboot的maven项目,目录结构如下: 二.在pom.xml ...

  2. springboot mybatis easyui 整合的一个小demo

    springboot mybatis easyui 整合的一个小demo 这是最终完成界面 话不多说 开整! 这是项目结构 数据库 表结构和数据库 (有点乱 之前本来是个正经图书表的 = =.) /* ...

  3. 搭建springboot+mybatis+freemarker项目

    1. 创建springboot web项目 先创建一个项目 选择maven项目,先什么都不勾,直接点击next groupid和artifactid可以随便填,然后点击next 点击finish fi ...

  4. springboot+mybatis+dubbo整合

    PS:搭建框架过程在网上查询了比较多的资料,最后自己再综合得到,如有侵权,请联系删除! 最近整合了springboot+mybatis+dubbo的架构,分享给大家!首先Demo目录架构如下: 首先项 ...

  5. springboot + mybatis +easyUI整合案例

    概述 springboot推荐使用的是JPA,但是因为JPA比较复杂,如果业务场景复杂,例如企业应用中的统计等需求,使用JPA不如mybatis理想,原始sql调优会比较简单方便,所以我们的项目中还是 ...

  6. SpringBoot和FreeMarker整合

    目录 使用工具 环境搭建 代码示例 java目录 WebLogAspect.java DruidDBConfig.java UserMapper.java User.java UserServiceI ...

  7. MyEclipse中搭建spring-boot+mybatis+freemarker框架

    1.在MyEclipse里创建一个maven项目.File>New>Maven Project: 勾选图中红色部分,然后点击Next. 2.填写下图中红色部分然后点击Finish. 3.此 ...

  8. 超详细的SpringBoot+Mybatis+Vue整合笔记

    这是我的第一篇博客.请多多指教! 开发工具 前端:WebStorm.后台:Eclipse Vue环境搭建 vue是一个JavaMVVM库,是一套用于构建用户界面的渐进式框架,是初创项目的首选前端框架. ...

  9. SpringBoot+Mybatis+Vue整合

    创建springboot项目 添加分页依赖等 <?xml version="1.0" encoding="UTF-8"?> <project ...

最新文章

  1. jquery按钮禁用(全)
  2. 湖南工业大学java试卷_湖南工业大学数控加工技术试卷.doc
  3. 树状数组求区间和(区间均值)
  4. 电容式传感器位移性能试验报告_圆柱形电容式接近开关可以分3类?
  5. vue中用的swiper轮播图的用法及github的地址
  6. Spring Android 1.0.0.M3 发布
  7. linux 编译 expat,关于expat库的编译
  8. android 实现自动拍照,Android自定义相机实现定时拍照功能
  9. linux 权限分割,sudo使用之实现权限分配
  10. matlab两个数据放在一起,问个小问题,怎么把两个图像放在一起?
  11. XStream入门应用程序
  12. 实战案例:探索星巴克的世界分布
  13. linux 一次对一个用户限制存取
  14. UG NX 12 重复命令
  15. 干货|别找了,分享80个无版权、高清、免费图片素材网站给你!
  16. Spring AOP动态代理的两种实现方式
  17. 软件设计模式Java版
  18. Android WebView 因重定向无法正常goBack()的一种解决小方案
  19. 使python脚本在运行时可以附带参数的方法
  20. c语言程序设计专题实验bmp,C语言程序设计实验大纲.doc

热门文章

  1. Android View的事件分发机制和滑动冲突解决方案
  2. Ajax请求传递中文参数
  3. 使用Termux把Android手机变成SSH服务器
  4. ssh登陆之忽略known_hosts文件
  5. 6374. 【NOIP2019模拟2019.10.04】结界[生与死的境界]
  6. gpu浮点计算能力floaps_关于CPU的浮点运算能力计算
  7. 背包九讲之二:完全背包问题
  8. 这些“黑话”只有PCB设计制造内行人才懂!附PCB术语及英文对照
  9. 汉威危化品安全风险监测预警平台 助力企业摆脱新旧领域风险
  10. 2021北京大学暑期课程:区块链与隐私计算