springboot对pageHelper的集成使用
pagehelper的作用:
就是在项目中省去了分页的步骤,也不用去查总条数。他会根据传入的start和limit返回当前要查的页的数据,以及总条数总页数等信息。
步骤:
一。导包
这里只增加导pagehelper的包,其他的连接数据库的包照常。
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
二。配置yml文件
pagehelper:
#指定数据库 可以不配置,插件会自动检测数据库的类型
helper-dialect: mysql
#分页合理化参数,默认值为false,当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
reasonable: true
#分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页
support-methods-arguments: true
#用于从对象中根据属性名取值 可以配置pageNum,pageSize,count,pageSizeZero,reasonable。不配置映射的用默认值。
params: count=countSql
其他配置连接mysql的配置,与以往一样:
spring:
datasource:
url: jdbc:mysql://192.168.61.147:3306/qunzhi?useSSL=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
username: root
password: 123456
mybatis:
mapperLocations: classpath:mapper/*/*.xml
三。控制层写法
第一:我使用了swagger,配置忽略,前文有介绍
第二:这里直接调用实现类,忽略了interface。
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinux.liaochao.myallprogramtest.sinux.batchoperation.entity.HonorEntity;
import com.sinux.liaochao.myallprogramtest.sinux.batchoperation.service.Impl.PageHelperServiceImpl;
import com.sinux.liaochao.myallprogramtest.sinux.batchoperation.service.PageHelperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/pagehelper")
@Api(value = "分页查询",tags = "分页查询")
public class PageHelperController {
@Autowired
private PageHelperServiceImpl pageHelperServiceImpl;
@PostMapping("/page")
@ApiOperation(value = "分页",notes = "分页")
public PageInfo<HonorEntity> lists(@RequestBody Map<String,Object> map){
int start = Integer.parseInt(String.valueOf(map.get("start")));
int limit = Integer.parseInt(String.valueOf(map.get("limit")));
PageHelper.startPage(start,limit);
PageInfo<HonorEntity> pageInfo = new PageInfo<>(pageHelperServiceImpl.pageHonor());
return pageInfo;
}
}
四.实现类的写法
注意:这里方法返回的是Page类型不是PageInfo,我刚开始就搞错了
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.sinux.liaochao.myallprogramtest.sinux.batchoperation.dao.PageHelperMapper;
import com.sinux.liaochao.myallprogramtest.sinux.batchoperation.entity.HonorEntity;
import com.sinux.liaochao.myallprogramtest.sinux.batchoperation.service.PageHelperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PageHelperServiceImpl {
@Autowired
private PageHelperMapper pageHelperMapper;
@Override
public Page<HonorEntity> pageHonor() {
return (Page<HonorEntity>)pageHelperMapper.selectHonor();
}
}
五.Mapper层的写法
@Mapper
public interface PageHelperMapper {
List<HonorEntity> selectHonor();
}
六.xml文件的写法
<select id="selectHonor" resultType="com.sinux.liaochao.myallprogramtest.sinux.batchoperation.entity.HonorEntity">
select * from sys_honorlist
</select>
这里提供一种不写xml的方法:
@Select("select * from sys_honorlist")
List<HonorEntity> selectHonor();
这样的话就一定要删掉xml,要不然不得行。