展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

pageHelper

  • pom.xml
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>2.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.11</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.27</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.18</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
  • yml
server:
  port: 8080

spring:
  application:
    name: demo12
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
  main:
    allow-circular-references: true
  devtools:
    restart:
      enabled: true

mybatis:
  configuration:
    map-underscore-to-camel-case: true
logging:
  level:
    com:
      ychen:
        mybatis: debug
  • 启动类
@SpringBootApplication
@MapperScan(basePackages = "com.ychen.mybatis.mapper")
public class Demo12Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo12Application.class, args);
    }

}
  • 配置类
@Configuration
public class MybatisPlusConfig {

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }

}

@Configuration
public class MybatisPlusPageConfig {

    /**
     * pagehelper的分页插件
     */
    @Bean
    public PageInterceptor pageInterceptor() {
        return new PageInterceptor();
    }

}
  • 实体类
@Data
public class User {

    private Long id;

    private String name;
}
  • 持久层接口
@Mapper
public interface UserMapper extends BaseMapper<User> {

}
  • 控制层
@RestController
public class TestController {

    @Autowired
    private UserMapper mapper;

    /**
     * 普通方式查询id为1的数据
     * @return
     */
    @RequestMapping("/test1")
    @ResponseBody
    public String test1(){
        Page<User> mpPage = mapper.selectPage(
                new Page<>(1, 2), Wrappers.<User>query().eq("id", 1)
        );
        List<User> records = mpPage.getRecords();
        System.out.println(records);
        return "success";
    }

    /**
     * 使用pageHelper查询id为1的数据
     * @return
     */
    @RequestMapping("/test2")
    @ResponseBody
    public String test2(){
        PageInfo<User> info = PageHelper.startPage(
                1, 2).doSelectPageInfo(() -> mapper.selectById(1)
        );
        List<User> list = info.getList();
        System.out.println(list);
        return "success";
    }

    @RequestMapping("/test3")
    @ResponseBody
    public String test3(){
        List<Long> ids = Arrays.asList(1L, 2L);
        PageInfo<User> info = PageHelper.startPage(1, 5)
                .doSelectPageInfo(() -> mapper.selectList(Wrappers.<User>query().in("id", ids)));
        List<User> list = info.getList();
        System.out.println(list);
        return "success";
    }

}
posted @ 2022-07-19 14:11  DogLeftover  阅读(121)  评论(0编辑  收藏  举报