SpringBoot Mybatis分页查询功能。

Mybatis是自带分页查询功能的哟。就不用自己再写一大堆算法实体类啥的,不多说直接上代码,并且我可愿意保证是100%能测试的哟。

1. 首先我们需要配置Mybatis的插件环境。

(1)在pom.xml里面拉Jar包:


		
			com.github.pagehelper
			pagehelper-spring-boot-starter
			${pagehelper-spring-boot.version}
		

(2)在application.properties配置里面设置:

pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

(3)下面直接开始写数据层与逻辑层,实体类如下:

package com.demo.model;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

//商品表实体类;
public class Commodity {
	   public int cid;             //商品id;
	   public BigDecimal cprice;   //商品价格;
	   public String ccode;        //商品编码;
	   public int ctype;           //商品类型;
	   public String cname;        //商品名称;
	   public ComType tid;
	   public ComType tname;
	   public int countx;          //商品数量;
   public Commodity(Commodity g, int i) {
		// TODO Auto-generated constructor stub
	}
   public Commodity(){}
/*public Commodity(Map g, int i) {
	// TODO Auto-generated constructor stub
}*/
public Commodity(Commodity g) {
	// TODO Auto-generated constructor stub
}
public Commodity(Map g, int i) {
	// TODO Auto-generated constructor stub
}
public ComType getTid() {
		return tid;
	}

	public void setTid(ComType tid) {
		this.tid = tid;
	}

	public ComType getTname() {
		return tname;
	}

	public void setTname(ComType tname) {
		this.tname = tname;
	}

	public int getCountx() {
		return countx;
	}

	public void setCountx(int countx) {
		this.countx = countx;
	}

@Override
public String toString() {
	return "Commodity [cid=" + cid + ", cprice=" + cprice + ", ccode=" + ccode + ", ctype=" + ctype + ", cname=" + cname
			+ ", tid=" + tid + ", tname=" + tname + ", countx=" + countx + "]";
}

   public int getCid() {
	return cid;
}
public void setCid(int cid) {
	this.cid = cid;
}
public String getCname() {
	return cname;
}
public void setCname(String cname) {
	this.cname = cname;
}
public BigDecimal getCprice() {
	return cprice;
}
public void setCprice(BigDecimal cprice) {
	this.cprice = cprice;
}
public String getCcode() {
	return ccode;
}
public void setCcode(String ccode) {
	this.ccode = ccode;
}
public int getCtype() {
	return ctype;
}
public void setCtype(int ctype) {
	this.ctype = ctype;
}
}

(4)Dao层如下:

package com.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.demo.model.Commodity;
import com.demo.utils.Page;

 @Mapper
public interface CommodityDao{
	 //主页展示商品查询;
   public List comSelect();
}

(5)Service层如下:

package com.demo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


import com.demo.mapper.CommodityDao;
import com.demo.model.Commodity;
import com.demo.model.Result;
import com.demo.utils.Page;
@Service
public class CommodityService {
	@Resource
	private CommodityDao comm;
	/**
	 * 商品页面展示查询;
	 * @return
	 * @throws Exception
	 */
    public List comServiceSelect()throws Exception{
    	List list=comm.comSelect();
    	return list;
    }

}

(6)controller控制层也就是Action层如下:

package com.demo.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.model.Commodity;
import com.demo.model.Login;
import com.demo.model.Order;
import com.demo.model.Result;
import com.demo.model.Sex;
import com.demo.service.CommodityService;
import com.demo.service.LoginService;
import com.demo.service.OrderService;
import com.demo.service.SexService;
import com.demo.utils.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/")
public class SpringBootUsersController {
	@Autowired
	private  CommodityService comm;

	
	@RequestMapping("/findCommodityAction_1")
	public String findCommodityAction_1(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,Integer pageSize,HttpServletRequest request,HttpServletResponse respoes)throws Exception{
		//分页插件;
        //这个里面是两个参数:pageNum(当前页数)与pageSize(每页要显示的数据数),这里直接给它们初始值为:pageNum=1,pageSize=4;
		PageHelper.startPage(pageNum,4);
		//获取数据;
		List list=comm.comServiceSelect();
        //然后把查询出来的数据放到插件自带的PageInfo对象里面;
		PageInfo pageInfo = new PageInfo(list);
		request.setAttribute("list",pageInfo);
		return "text";
	}

}

(7)sql.xml其实数据库sql语句跟平时一样没区别如下:



   
    
     
     
     
     
     
      
       
       
      
    
    
    
    
  

(8)视图层也就是页面如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'text.jsp' starting page
    
	
	
	    
	
	
	

    
      
    
    
    
    
    
    
    
    
    
    
    
    
  
  
  
    
商品分页展示
商品编号 商品名称 商品编码 商品类型 商品价格
${item.cid} ${item.cname} ${item.ccode} ${item.tname.tname} ${item.cprice}

当前第 ${list.pageNum} 页,总 ${list.pages} 页,共 ${list.total} 条记录

首页 上一页 下一页 尾页

(10)效果图如下:

SpringBoot Mybatis分页查询功能。_第1张图片

你可能感兴趣的:(java)