当我们浏览商品列表的时候,下滑浏览到底部的时候会出现加载更多标志,下拉刷新之后出现了新的商品列表,以下是一个示例展示如何通过mybatis中的物理分页limit语法实现实现商品列表的分页查询并返回数据
1.为何要进行分页查询
分页查询是数据库交互最常用的几种操作之一,当我们浏览商品列表的时候,下滑浏览到底部的时候会出现加载更多标志,下拉刷新之后出现了新的商品列表,这是由于后端在向前端传输数据的时候,如果一次性将所有的商品数据全部发送,前端解析加载会花费更多的时间,这对页面的加载展示产生较大压力,在这样的情况下,分页查询应运而生。
2.分页的种类
分页可分为逻辑分页和物理分页
逻辑分页是一次性查询出所有结果,把全部数据查询加载进内存 ,返回部分结果,操作在内存中进行,所以也叫内存分页。逻辑分页减少了数据库查询次数,适用于频繁访问、数据量少的情况,但是当数据量大的时候,容易造成内存溢出
物理分页是利用sql语句中的limit语法在数据库中进行分页操作,这样每次可以查询出指定一页的数据。物理分页适合大数据量的操作,但是每次请求一页的数据会造成查询数据库的次数较为频繁,对服务器的性能提出了更高的要求
3.新建数据库表插入商品信息
4.创建实体类Goodist
@Data
@Entity
public class GoodList {
@Id
private int goodListId; //商品编号
private String goodListName; //商品名称
private double goodListPrice; //商品单价
private String goodListPicture;
private String goodListBriefIntroduction;
private Integer categoryId;
}
5.创建GoodMapper.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.software.PetShop.mapper.GoodMapper">
<resultMap id="goodResult" type="com.software.PetShop.entity.GoodList">
<id column="good_list_id" property="goodListId"/>
<id column="good_list_name" property="goodListName"/>
<id column="good_list_price" property="goodListPrice"/>
<id column="good_list_brief_introduction"
property="goodListBriefIntroduction"/>
<id column="good_list_picture" property="goodListPicture"/>
<id column="category_id" property="categoryId"/>
</resultMap>
</mapper>
6.配置mybatis-config.xml
在<mappers>下新加GoodMapper.xml映射文件
<mappers>
<mapper resource="mappers/GoodMapper.xml"></mapper>
</mappers>
7.创建GoodController,新建getGoodList()方法
这里根据前端传过来的商品种类id,当前页的索引和数据条数来进行分页查询,当然根据需求也可以在后端定义每一页的索引和数据条数大小,获取商品种类id后在sql语句中进行分页操作。
@RestController
@RequestMapping("/petShop")
public class GoodController {
/**
* 根据商品种类id,页的索引,页的数据条数获取商品列表
* **/
@PostMapping("/getGoodsList")
public String getGoodList(@RequestBody String json)
//获取前端传过来的商品种类id,获取当前页的索引,获取当前页的数据条数值
{ JSONObject jsonObject = new JSONObject(json);
int categoryId = jsonObject.getInt("categoryId");
int pageIndex = jsonObject.getInt("pageIndex");
int pageSize = jsonObject.getInt("pageSize");
SqlSession sqlSession = getSqlSession();
try {
GoodMapper goodMapper = sqlSession.getMapper(GoodMapper.class);
//计算分页
int offset = (pageIndex - 1) * pageSize;
List<GoodList> allGoodList =
goodMapper.findGoodListByCategoryId(categoryId,offset , pageSize);
//计算总条数
int totalCount = allGoodList.size();
//计算总页数
int totalPage = (int) Math.ceil(totalCount / (double) pageSize);
System.out.println("goodList数据:"+allGoodList);
JSONArray jsonArray = new JSONArray();
for (GoodList goodList : allGoodList) {
JSONObject clothJson = new JSONObject();
clothJson.put("goodId", goodList.getGoodListId());
clothJson.put("goodName", goodList.getGoodListName());
clothJson.put("goodBriefIntroduction",
goodList.getGoodListBriefIntroduction());
clothJson.put("goodPicture",goodList.getGoodListPicture());
clothJson.put("goodPrice",goodList.getGoodListPrice());
clothJson.put("goodCategoryId",goodList.getCategoryId());
System.out.println("goodList数据:"+goodList.getGoodListId() + ": " +
goodList.getGoodListName());
jsonArray.put(clothJson);
}
JSONObject result = new JSONObject();
if (jsonArray!=null){
result.append("data",jsonArray);
result.append("pageIndex", pageIndex);
result.append("pageSize", pageSize);
result.append("totalPage", totalPage);
result.append("status","ok");
}else{
result.append("status","no");
}
return result.toString();
} finally {
sqlSession.close();
}
}
}
8.创建GoodMapper接口,实现findGoodListByCategoryId()方法
public interface GoodMapper extends BaseMapper<GoodList> {
List<GoodList> findGoodListByCategoryId(@Param("categoryId")int
categoryId,@Param("offset") int offset, @Param("pageSize")int pageSize);
}
9.在GoodMapper.xml中实现sql分页查询语句
<!--通过标识符id分页查询-->
<select id="findGoodListByCategoryId" resultMap="goodResult" parameterType="map">
SELECT * FROM good_list WHERE category_id = #{categoryId} LIMIT #{offset},
#{pageSize}
</select>
10.测试
这里我们模拟前端传过来的categoryId=1,pageIndex=1(查询第一页的数据),pageSize=3(每页的数据条数为3条)
可以看出程序成功在数据库表中进行了分页查询操作,每一页的数据大小为3条,并成功返回了商品数据