记载一下前几天的一个需求,mybatis
分页插件的使用,
前端传进来pageNum
, pageSize
后端根据这两个参数进行分页
引入插件
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
service层代码
public PageResponse<ParkSpaceBO> getCarInRecords(ParkSpaceDTO param) {
// 是一些对象,param的参数,代码详见附录
ParkSpacePO pso = new ParkSpacePO();
BeanConvertUtils.copy(param, pso);
// new一个Page对象,(不用自己手动创建,插件自带的)
Page page = new Page();
// 设置当前页数
page.setCurrent(param.getPageNo());
// 设置一页的pageSize
page.setSize(param.getPageSize());
// 获取POMapper层返回值
// 注意传进去的参数,和返回值
IPage<ParkSpacePO> tempCarInRecords = ParkSpacePOMapper.getCarInRecords(page, pso);
// 获取返回的list
List<ParkSpacePO> records = tempCarInRecords.getRecords();
// 利用复制的方式改变泛型类型,构造返回值的类型
List<ParkSpaceBO> ParkSpaceBOS = BeanConvertUtils.copyList(records, ParkSpaceBO.class);
// 给返回值对象传参
return new PageResponse<>(ParkSpaceBOS, param.getPageNo(), tempCarInRecords.getTotal(), param.getPageSize());
}
POMapper接口层
// 注意参数和返回值,参数第一个必须传Page对象,第二个传带有参数的对象,@Param("item")必须要传,item和xml里对应
IPage<ParkSpacePO> getCarInRecords(Page<?> page, @Param("item") ParkSpacePO record);
POMapper.xml层
注意 item
要和xml
这里的对应,另外注意resultMap
对不对
<select id="getCarInRecords" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from parking_space
<where>
<if test="item.spaceNo!=null">
and space_no = #{item.spaceNo}
</if>
<if test="item.plateNo!=null">
and plate_no = #{item.plateNo}
</if>
<if test="item.parkFlag!=null">
and park_flag = #{item.parkFlag}
</if>
<if test="item.personName!=null">
and person_name = #{item.personName}
</if>
<if test="item.personId!=null">
and person_id = #{item.personId}
</if>
<if test="item.state!=null">
and state = #{item.state}
</if>
</where>
</select>
附录
下面还有一些`get` `set`
public class ParkSpacePO {
private Integer id;
private String spaceNo;
private String plateNo;
private Integer parkFlag;
private String personName;
private String personId;
private Integer state;
private String parkTime;
private Date inTime;
private Date createTm;
private Date updateTm;
public Integer getId() {
return id;
}
@Data
public class ParkSpaceBO {
private String spaceNo;
private String plateNo;
private Integer parkFlag;
private String personName;
private String personId;
private Integer state;
private String parkTime;
private Date inTime;
}
下面是一些get set
public class PageResponse<T> implements Serializable {
private static final long serialVersionUID = -1L;
private List<T> list;
private Long pageNo = 1L;
private Long total = 0L;
private Long pageSize = 10L;
public PageResponse(List<T> list, Long pageNo, Long total, Long pageSize) {
this.list = list;
this.pageNo = pageNo;
this.total = total;
this.pageSize = pageSize;
}
}