Springboot使用PageHelper(需要解决依赖冲突)
1、导入相关依赖 (PageHelper自带了mybatis、mybatis-spring,不排除会报错)
<!-- pagehelper分页插件依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </exclusion> </exclusions> </dependency>
2、在application.yml 配置文件中配置指定数据库
#pagehelper配置
pagehelper:
helper-dialect: mysql
其他相关配置:https://www.cnblogs.com/mengw/p/11673637.html
或者使用配置类
3、使用Pagehelper多条件分页
条件类:
@Data @AllArgsConstructor @NoArgsConstructor public class HouseQueryInfo implements Serializable{ private static final long serialVersionUID = 1034989291292817460L; //private int pageIndex;//当前页码(页面传递) //private int pageSize;//页容量(后台设置) private String title; private String price_on; private String price_down; private String street_id; private String type_id; private String floorage_on; private String floorage_down; }
service层接口:
public interface IHouseService extends IService<House>{ //根据条件查询所有房屋信息,分页显示 List<House> findAllHouse(HouseQueryInfo houseInfo) throws Exception; //使用pagehelper分页查询 public PageInfo<House> findBookPage(Integer pageIndex,Integer pageSize,HouseQueryInfo houseInfo) throws Exception; }
serviceimpl实现类:
@Service public class HouseServiceImpl extends ServiceImpl<IHouseMapper, House> implements IHouseService { @Autowired private IHouseMapper houseMapper; //根据条件查询所有房屋信息,分页显示 @Override public List<House> findAllHouse(HouseQueryInfo houseInfo) throws Exception { return houseMapper.findAllHouse(houseInfo); } //根据所有信息进行分页 @Override public PageInfo<House> findBookPage(Integer pageIndex,Integer pageSize,HouseQueryInfo houseInfo) throws Exception { PageHelper.startPage(pageIndex,pageSize); PageInfo<House> pageinfo = new PageInfo<>(houseMapper.findAllHouse(houseInfo)); return pageinfo; }
controller层:
PageInfo<House> pageInfo = houseService.findBookPage(houseInfoVo.getPageIndex(),3,houseInfo); // 将租房信息存入model中 model.addAttribute("pageInfo", pageInfo);
页面数据的展示:
<TR th:each="house:${pageInfo.getList()}"> <TD class=house-thumb><span> <img th:src="${house.photopath != null} ? |/img/${house.photopath}| :'../images/thumb_house.gif' " width="100" height="75"</span></TD> <TD> <DL> <DT> <A href="#" th:text="${house.title}"></A> </DT> <DD> <span th:text="${house.streets.name}"/> <span th:text="${house.streets.district.name}"/> <span th:text="${house.floorage}"/> 平米<BR>联系方式:<span th:text="${house.contact}"/> </DD> </DL> </TD> <TD class=house-type><span th:text="${house.typess.name}"/></TD> <TD class=house-price><SPAN th:text="${house.price}"></SPAN>元/月</TD> </TR>