SpringBoot实现分页查询

一.mybatis-plus实现

1.添加依赖

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
</dependency>

2.分页实现

public Result findPage(@RequestParam Integer pageNum,
                       @RequestParam Integer  pageSize,
                       @RequestParam String name){
    QueryWrapper<sys_menu> queryWrapper = new QueryWrapper<>();
    queryWrapper.orderByDesc("id");
    queryWrapper.like("name",name);
    return Result.success(menuService.page(new Page<>(pageNum,pageSize),queryWrapper));
}

二.mybatis实现

1.添加依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.3.0</version>
</dependency>

2.动态分页查询sql编写(在mapper.xml文件编写,通过方法名字绑定)

 <select id="page_users" resultType="com.example.pt_springboot.entity.sys_user">
          select * from sys_user
            <where>
                <if test="username!=null and username!=''">
                    username like concat('%',#{username},'%')
                </if>
                <if test="email!=null and email!=''">
                    and email like concat('%',#{email},'%')
                </if>
                <if test="address!=null and address!=''">
                    and address like concat('%',#{address},'%')
                </if>
            </where>
                  order by id desc  limit #{pageNum},#{pageSize}
      </select>
<!--分页数量查询-->
      <select id="usersNum" resultType="int">
          select count(*) from sys_user
           <where>
              <if test="username!=null and username!=''">
                  username like concat('%',#{username},'%')
              </if>
              <if test="email!=null and email!=''">
                  and email like concat('%',#{email},'%')
              </if>
              <if test="address!=null and address!=''">
                  and address like concat('%',#{address},'%')
              </if>
           </where>
      </select>
                            <!--!!!!!注意,可以在前面加and关键字,避免拼接错误!!!!!!!!!!!-->
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot 中,可以使用 Spring Data JPA 实现分页查询。下面是一个简单的例子: 1. 首先,在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> ``` 2. 创建一个实体类,例如 User: ```java @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; // getters and setters } ``` 3. 创建一个 UserRepository 接口,继承 JpaRepository 接口: ```java public interface UserRepository extends JpaRepository<User, Long> { Page<User> findAll(Pageable pageable); } ``` 4. 在 Controller 中引入 UserRepository,并使用 Pageable 对象实现分页查询: ```java @RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/users") public Page<User> getUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { Pageable pageable = PageRequest.of(page, size); return userRepository.findAll(pageable); } } ``` 在上面的例子中,我们通过调用 UserRepository 的 findAll 方法,传入 Pageable 对象实现分页查询。在 getUsers 方法中,我们使用 @RequestParam 注解将 page 和 size 参数传递给 PageRequest.of 方法,创建一个 Pageable 对象。 这就是使用 Spring BootSpring Data JPA 实现分页查询的基本步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值