分页,数组入参mybatis写法实例
数组入参mybatis查找,模糊查找
mapper.xml:
<select id="pageSearchList" resultType="com.example.domain.stockModel">
select
user_name as username,
mobile as mobile
from stockin_order
<where>
<if test="query.spaceIds!=null and query.spaceIds.size()>0">
and s.space_id in
<foreach collection="query.spaceIds" separator="," index="index" item="spaceId" open="("
close=")">
#{spaceId}
</foreach>
</if>
<if test="query.sku != null and query.sku !=''">
AND i.sku like concat('%',#{query.sku},'%')
</if>
</where>
</select>
Mapper接口:
public interface StockMapper extends BaseMapper<StockEntity> {
IPage<StockModel> pageSearchList(IPage page, @Param("query") StockListRequest request);
}
service:
public PageResponse<StockResponse> getStockListByRequest(StockListRequest request) {
PageResponse<StockResponse> pageResponse = new PageResponse<>();
IPage<StockModel> pageResult = stockMapper.pageSearchList(new Page<>(request.getPageIndex(), request.getPageSize()), request);
List<StockModel> resultList = pageResult.getRecords();
List<StockResponse> responseList = resultList.stream().map(item -> {
StockResponse response = new StockResponse();
BeanUtils.copyProperties(item, response);
return response;
}).collect(Collectors.toList());
pageResponse.setTotalCount(pageResult.getTotal());
pageResponse.setContent(responseList);
return pageResponse;
}
request:
@Data
public class StockListRequest extends PageRequest {
@ApiModelProperty(value = "用户名", name = "username")
private String username;
@ApiModelProperty(value = "手机号", name = "mobile")
private String mobile;
}
request基类:
@Data
public class PageRequest {
@NotNull
private Integer pageSize = 20;
@NotNull
private Integer pageIndex = 1;
private String sortField;
private String sortOrder;
}