SpringBoot集成MybatisPlus实现分页
注意:如果数据库选择oracle,时间类型字段设置为timestamp而不是date类型时,这时用分页插件查询会报错,还没搞明白咋回事。
Pom依赖:
<!-- mybatis-plus依赖--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency>
配置分页插件:
@Configuration public class MybatisPlusConfig { /** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
Controller层:
@RequestMapping("/getProjectList")
@ResponseBody public Page getProjectList(HttpServletRequest request) { int offset = Integer.parseInt(request.getParameter("offset")); int limit = Integer.parseInt(request.getParameter("limit")); String yearStr = request.getParameter("year"); Integer year = null; if(yearStr!=null){ year = Integer.parseInt(request.getParameter("yearStr")); } Page page = new Page<>(offset / limit + 1, limit); Page projectWinInfoList = (Page) electricityDao.getAll(page,year); return projectWinInfoList; }
Dao层:
@Mapper public interface ElectricityDao extends BaseMapper<EneElectricity> { IPage getAll (Page page,@Param("year")Integer year); }
Mapper.xml:注意resultType为HashMap时,返回的数据完全是查询的数据库字段,而不是实体类属性,二者有一些大小写,或者命名的差别。
<select id="getAll" resultType="java.util.HashMap"> select * from ene_electricity <if test="year!=null and year!=''"> where year = #{year} </if> </select>