PageHelper PageHelper 是一个用于简化 Java 分页查询的工具库,广泛应用于 MyBatis 和 Spring Data JPA 等框架中。它提供了简单易用的 API 来处理分页和排序操作。 PageHelper 通过拦截器的方式,在查询执行前自动计算分页参数,并在查询结果中返回分页信息。它支持多种数据库,并且可以与其他 ORM 框架配合使用。
PageHelper 的基本用法 添加依赖
1 2 3 4 5 <dependency > <groupId > com.github.pagehelper</groupId > <artifactId > pagehelper-spring-boot-starter</artifactId > <version > 1.4.0</version > </dependency >
在 Spring Boot 中配置 PageHelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public PageResult pageQuery (EmployeePageQueryDTO employeePageQueryDTO) { PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize()); Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO); long total = page.getTotal(); List<Employee> records = page.getResult(); return new PageResult (total, records); }
1 2 3 4 5 6 7 8 9 10 // EmployeeMapper.xml <select id ="pageQuery" resultType ="com.sky.entity.Employee" > select * from employee <where > <if test ="name != null and name != ''" > and name like concat('%', #{name}, '%') </if > </where > order by create_time desc </select >
PageHelper 能够进行动态sql 语句的拼接,将查询条件封装到 PageHelper 中,自动进行分页查询。
以上为实现了分页查询的代码示例
分页查询进一步 发现返回的 createTime 和 updateTime 没有按照年月日时分秒的格式返回
两种处理方法:
1 2 3 4 5 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime updateTime;
使用 WebMvcConfigurer 统一进行序列化和反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Override protected void extendMessageConverters (List<HttpMessageConverter<?>> converters) { log.info("扩展Spring MVC框架的消息转换器..." ); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter (); converter.setObjectMapper(new JacksonObjectMapper ()); converters.add(0 , converter); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class JacksonObjectMapper extends ObjectMapper { public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd" ; public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm" ; public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss" ; public JacksonObjectMapper () { super (); this .configure(FAIL_ON_UNKNOWN_PROPERTIES, false ); this .getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); SimpleModule simpleModule = new SimpleModule () .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))) .addDeserializer(LocalDate.class, new LocalDateDeserializer (DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))) .addDeserializer(LocalTime.class, new LocalTimeDeserializer (DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))) .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer (DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT))) .addSerializer(LocalDate.class, new LocalDateSerializer (DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT))) .addSerializer(LocalTime.class, new LocalTimeSerializer (DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT))); this .registerModule(simpleModule); } }
使用消息转换器和对象映射器的方式可以统一处理时间格式化问题,避免在每个实体类中都添加 @JsonFormat 注解。 这样可以提高代码的可维护性和可读性。 同时,使用 WebMvcConfigurer 还可以对其他请求和响应进行统一处理,例如添加全局异常处理、跨域配置等。