SpringBoot学习基础一优化分页查询

一  pagehelp引入原因

 该插件可以自动帮我们计算start以及SQL查询想要展示数据语句,例如:

@Select("select * from emp limit #{start},#{pageSize};")

这种查询语句,他都会帮我们自动的完成。

 1.在分页查询当中我们需要接收的参数有count(数据总数),result(查询结果,即需要展示的数据),并且在Controller当中我们也需要根据接收到的page(页面序号),pageSize(每个页面需要展示的数据条数),值得一说的是我们可以使用@RequestParam注解当中的defaultValue来设置属性的默认值

@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize,

然后根据起始索引计算公式start = (page-1)*pageSize,得出start,最后根据SQL语句当中的查询语句来得到结果。这种传统方式步骤更为固定,代码繁琐。

2.需要导入的pom.xml相关依赖:

        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.4.6</version>
        </dependency>

   

3.由此引入pagehelper插件来进行优化分页查询:

Controller层代码:

 @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {

        log.info("分页查询参数:{},{},{},{}", page, pageSize,begin,end);
        PageBean pageBean = empService.page(page, pageSize,name,  gender,  begin,  end);
        return Result.success(pageBean);
    }

Service层代码:

PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end);
 /**
     * 使用pagehelp插件来优化分页查询
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);

        //执行查询操作,并将结果转换为Page形式
        List<Emp> empList = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) empList;
        //封装为一个pageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

Mapper层代码:

 public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
 <select id="list" resultType="com.example.springbootitheima.pojo.Emp">
        select *
        from emp
        <where>
            <if test="name != null and name != ''">
                name like concat('%', #{name}, '%')
            </if>
            <if test="gender != null">
                and gender = #{gender}
            </if>
            <if test="begin != null and end != null">
                and entrydate between #{begin} and #{end}
            </if>
        </where>
            order by update_time desc

    </select>

   这种方式不仅简化了代码量,而且使得代码逻辑更加严谨,减少查询数据库操作


 

  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于Spring Boot和Spring Cloud的分页查询示例。 首先,你需要在pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> ``` 然后,你需要创建一个实体类来表示你要查询的数据。例如,如果你要查询一个“用户”实体,你可以创建一个名为“User”的类: ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String email; // getter and setter } ``` 接下来,你需要创建一个名为“UserRepository”的接口,该接口扩展了JpaRepository接口。这个接口将允许你执行分页查询,并将结果作为Page对象返回: ```java public interface UserRepository extends JpaRepository<User, Long> { Page<User> findAll(Pageable pageable); } ``` 在你的控制器类中,你可以注入UserRepository,并使用Pageable对象来执行分页查询: ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping public Page<User> getUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { Pageable pageable = PageRequest.of(page, size); return userRepository.findAll(pageable); } } ``` 现在,当你使用GET请求访问“/users”时,将返回一个包含用户列表的Page对象。你可以通过添加其他查询条件来进一步筛选和排序结果。 希望这个示例能够帮助你编写分页查询

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值