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

此文章为分页与模糊查询

 

分页:

创建一个工具类   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 {


    @Autowired
    private 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 {


    @Autowired
    private 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 {

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public 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 mapper
        PUBLIC "-//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 tname
         from j_student s ,j_teacher t,j_db d where  d.sid = s.sid and d.tid = t.tid
          GROUP BY s.sid
          HAVING s.sname like concat('%',#{sname},'%') and tname like concat('%',#{tname},'%')
    </select>

</mapper>

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

 

分页与模糊查询完成

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

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值