最近做了一个基于SpringBoot和mybatis的项目,和前一篇的Spark Streaming是一个完整的项目,系统后端通过读取mysql中的数据在前端页面显示,并进行处理。前端页面展示使用的是thymeleaf,在项目中遇到的其中一个小问题是数据显示的分页处理,最终使用了mybatis的pegeHelper分页工具。只是简单的查看了一下SpringBoot的API,发现其对于集成来说是非常好用的框架,而且学习成本比较低,当然我对它的使用也只是停留在基础阶段。
在启动类中配置分页插件pagehelper的设置:
import com.github.pagehelper.PageHelper;
import org.aopalliance.intercept.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.Properties;
@SpringBootApplication
@MapperScan("com.yuqing.alert.*")
public class AlertApplication {
public static void main(String[] args) {
SpringApplication.run(AlertApplication.class, args);
}
//配置mybatis的分页插件pageHelper
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
pageHelper.setProperties(properties);
return pageHelper;
}
}
数据分页实体:
package com.yuqing.alert.pojo;
/**
* 分页bean
*/
import java.util.List;
public class PageBeanEntity<T> {
// 当前页
private Integer currentPage ;
// 每页显示的总条数
private Integer pageSize ;
// 总条数
private Integer totalNum;
// 是否有下一页
private Integer isMore;
// 总页数
private Integer totalPage;
// 开始索引
private Integer startIndex;
// 分页结果
private List<T> items;
public PageBeanEntity() {
super();
}
public PageBeanEntity(Integer currentPage, Integer pageSize, Integer totalNum) {
super();
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalNum = totalNum;
this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
this.startIndex = (this.currentPage-1)*this.pageSize;
this.isMore = this.currentPage >= this.totalPage?0:1;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalNum() {
return totalNum;
}
public void setTotalNum(Integer totalNum) {
this.totalNum = totalNum;
}
public Integer getIsMore() {
return isMore;
}
public void setIsMore(Integer isMore) {
this.isMore = isMore;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public Integer getStartIndex() {
return startIndex;
}
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
}
以某个简单的方法进行举例。
Mapper层:
import com.yuqing.alert.pojo.*;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface AlertInfoMapper {
//查询车辆预警信息
@Select("select count(*) from alertCar")
public int selectCountAllAlertCar();
@Select("select * from alertCar order by alertTime desc")
public List<AlertCarEntity> selectAllAlertCar();
@Select("select * from alertCar where carNumber like CONCAT('%','${carNumber}','%') and alertTime BETWEEN #{selectTime1} AND #{selectTime2}")
public List<AlertCarEntity> selectAlertCar(String carNumber, String selectTime1, String selectTime2);
@Select("select count(*) from alertCar where carNumber like CONCAT('%','${carNumber}','%') and alertTime BETWEEN #{selectTime1} AND #{selectTime2}")
public int selectCountAlertCar(String carNumber,String selectTime1,String selectTime2);
}
Service层:
import com.github.pagehelper.PageHelper;
import com.yuqing.alert.Mapper.AlertInfoMapper;
import com.yuqing.alert.pojo.AlertCarEntity;
import com.yuqing.alert.pojo.PageBeanEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AlertCarService {
@Autowired
private AlertInfoMapper alertInfoMapper;
/**
* 查询所有的报警车辆记录数
* @return
*/
public int selectCountAllAlertCar(){
return alertInfoMapper.selectCountAllAlertCar();
}
/**
* 查询所有的报警车辆
* @param currentPage
* @param pageSize
* @return
*/
public List<AlertCarEntity> selectAllAlertCar(int currentPage, int pageSize){
PageHelper.startPage(currentPage, pageSize);
List<AlertCarEntity> allAlertCar = alertInfoMapper.selectAllAlertCar(); //全部
int countNums = alertInfoMapper.selectCountAllAlertCar(); //总记录数
PageBeanEntity<AlertCarEntity> pageData = new PageBeanEntity<>(currentPage, pageSize, countNums);
pageData.setItems(allAlertCar);
return pageData.getItems();
}
/**
* 查询符合条件的报警记录数
* @param carNumber
* @param selectTime1
* @param selectTime2
* @return
*/
public int selectCountAlertCar(String carNumber,String selectTime1,String selectTime2){
return alertInfoMapper.selectCountAlertCar(carNumber,selectTime1,selectTime2);
}
/**
* 符合条件的所有报警车辆
* @param currentPage
* @param pageSize
* @param carNumber
* @param selectTime1
* @param selectTime2
* @return
*/
public List<AlertCarEntity> selectAlertCar(int currentPage,int pageSize,String carNumber,String selectTime1,String selectTime2){
PageHelper.startPage(currentPage, pageSize);
List<AlertCarEntity> AlertCar = alertInfoMapper.selectAlertCar(carNumber,selectTime1,selectTime2);
int countNums = alertInfoMapper.selectCountAlertCar(carNumber,selectTime1,selectTime2); //记录数
PageBeanEntity<AlertCarEntity> pageData = new PageBeanEntity<>(currentPage, pageSize, countNums);
pageData.setItems(AlertCar);
return pageData.getItems();
}
}
controller层:
package com.yuqing.alert.controller;
import com.yuqing.alert.pojo.PageBeanEntity;
import com.yuqing.alert.service.AlertCarService;
import com.yuqing.alert.util.DateTimeHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping(value = "/alertCar")
public class AlertCarController {
@Autowired
AlertCarService alertCarService;
/**
* 查询所有的报警车辆信息
* @param hsr
* @param model
* @param currentPage
* @param pageSize
* @param startIndex
* @return
*/
@RequestMapping(value = "/selectAllAlertCar")
public ModelAndView selectAllAlertCar(HttpServletRequest hsr,Model model, Integer currentPage, Integer pageSize, Integer startIndex){
PageBeanEntity pageBean;
String date1;
String date2;
String date = hsr.getParameter("date1");
String carNumber = hsr.getParameter("carNumber");
if(date!=null){
date1 = hsr.getParameter("date1");
date2 = hsr.getParameter("date2");
}else {
date1 = DateTimeHelper.getPreDateTime();
date2 = DateTimeHelper.getDateTime();
}
if(hsr.getParameter("op")!=null){
String Number = hsr.getParameter("op");
if(Number.equals("首页")){
currentPage = 1;
pageBean = getPageBean(currentPage,pageSize,startIndex,alertCarService.selectCountAllAlertCar());
}else if(Number.equals("上一页")){
String current = hsr.getParameter("cur");
currentPage = (Integer.parseInt(current)-1);
}else if(Number.equals("下一页")){
String current = hsr.getParameter("cur");
currentPage = (Integer.parseInt(current)+1);
}else {
String current = hsr.getParameter("last");
currentPage = (Integer.parseInt(current));
}
pageBean = getPageBean(currentPage,pageSize,startIndex,alertCarService.selectCountAllAlertCar());
}
else{
pageBean = getPageBean(1,pageSize,startIndex,alertCarService.selectCountAllAlertCar());
}
model.addAttribute("date1",date1);
model.addAttribute("date2",date2);
model.addAttribute("carNumber",carNumber);
ModelAndView mav = new ModelAndView("alertInfoIndex");
model.addAttribute("pageBean", pageBean);
model.addAttribute("alertCar",alertCarService.selectAllAlertCar(pageBean.getCurrentPage(), pageBean.getPageSize()));
return mav;
}
/**
* 对报警车辆信息做搜索操作
* @param hsr
* @param model
* @param currentPage
* @param pageSize
* @param startIndex
* @return
*/
@RequestMapping(value = "selectAlertCar")
public ModelAndView selectAlertCar(HttpServletRequest hsr,Model model, Integer currentPage, Integer pageSize, Integer startIndex){
String carNumber = hsr.getParameter("carNumber");
String date1 = hsr.getParameter("date1");
String date2 = hsr.getParameter("date2");
PageBeanEntity pageBean = getPageBean(currentPage,pageSize,startIndex,alertCarService.selectCountAlertCar(carNumber,date1,date2));
ModelAndView mav = new ModelAndView("alertInfoIndex");
model.addAttribute("date1",date1);
model.addAttribute("date2",date2);
model.addAttribute("carNumber",carNumber);
model.addAttribute("pageBean",pageBean);
model.addAttribute("alertCar",alertCarService.selectAlertCar(pageBean.getCurrentPage(),pageBean.getPageSize(),carNumber,date1,date2));
return mav;
}
/**
* 分页工具方法
* @param currentPage
* @param pageSize
* @param startIndex
* @param num
* @return
*/
public PageBeanEntity getPageBean(Integer currentPage, Integer pageSize, Integer startIndex,int num){
PageBeanEntity pageBean = new PageBeanEntity();
if(currentPage==null||currentPage==0){
pageBean.setCurrentPage(1);
}else {
pageBean.setCurrentPage(currentPage);
}
if(pageSize==null||pageSize==0){
pageBean.setPageSize(6);
}else{
pageBean.setCurrentPage(pageSize);
}
if(startIndex==null||startIndex==0){
pageBean.setStartIndex((pageBean.getCurrentPage()-1)* pageBean.getPageSize());
}else {
pageBean.setStartIndex(startIndex);
}
pageBean.setTotalNum(num);
pageBean.setTotalPage((pageBean.getTotalNum()+ pageBean.getPageSize()-1)/ pageBean.getPageSize());
return pageBean;
}
}