效果
mybatis-plus分页插件 config
@Configuration
@EnableTransactionManagement
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//分页
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
实体类
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = false)
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 分类编号
*/
@TableId(value = "cat_id", type = IdType.AUTO)
private Integer catId;
private String catName;
}
Service层
@Override
public Page<Category> getCategoryPage(Integer cur, Integer size) {
Page<Category> categoryPage = new Page<>(cur, size);
return categoryMapper.selectPage(categoryPage, null);
}
Controller层
@Override
public Page<Category> getCategoryPage(Integer cur, Integer size) {
Page<Category> categoryPage = new Page<>(cur, size);
return categoryMapper.selectPage(categoryPage, null);
}
<tbody>
<tr th:each="category:${categoryPage.records}">
<td th:text="${category.catId}"></td>
<td th:text="${category.catName}"></td>
<td>
<a th:href="@{/updateCategory/{id}(id=${category.catId})}">
<button type="button" class="btn btn-primary">修改</button>
</a>
<a th:href="@{/category/deleteCategoryById/{id}(id=${category.catId})}">
<button type="button" class="btn btn-danger">删除</button>
</a>
</td>
</tr>
</tbody>
</table>
<nav aria-label="Page navigation example">
<ul class="pagination">
<th:block th:if="${categoryPage.hasPrevious()}">
<li class="page-item">
<a class="page-link" th:href="@{/categoryPage/{cur}(cur=${categoryPage.current}-1)}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</th:block>
<th:block th:if="${!categoryPage.hasPrevious()}">
<li class="page-item">
<a class="page-link" th:href="@{/categoryPage/{cur}(cur=${categoryPage.current})}"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</th:block>
<th:block th:if="${categoryPage.getCurrent()<=3}"
th:each="i:${#numbers.sequence(1,categoryPage.pages)}">
<li class="page-item"><a class="page-link" th:href="@{/categoryPage/{cur}(cur=${i})}"
th:text="${i}">1</a></li>
</th:block>
<th:block th:if="${categoryPage.hasNext()}">
<li class="page-item">
<a class="page-link" th:href="@{/categoryPage/{cur}(cur=${categoryPage.current}+1)}"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</th:block>
<th:block th:if="${!categoryPage.hasNext()}">
<li class="page-item">
<a class="page-link" th:href="@{/categoryPage/{cur}(cur=${categoryPage.current})}"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</th:block>
</ul>
</nav>