Springboot+vue分页
时间: 2024-01-23 11:03:42 浏览: 106
Springboot Vue分页是指在使用Spring Boot框架后端和Vue.js前端开发的项目中,实现对数据的分页展示和操作。在Spring Boot中,可以通过定义一个分页查询的接口来获取分页数据,如UserController.java中的getAllUserByPage方法。这个方法通过使用@RequestParam注解来接收前端传递的页码和每页显示行数的参数,并调用userService的getAllUserByPage方法来获取对应页的数据。在前端使用Vue.js时,可以通过引入分页组件,如User.vue中的el-pagination组件,来实现分页功能的展示和操作。通过设置组件的属性和事件的绑定,可以实现页码切换、每页显示行数变更等功能。
另外,还可以通过复制官网上提供的分页组件代码来使用,如el-pagination组件。这样可以快速引入分页功能,并根据需要定制布局和样式。
相关问题
springboot+pagehelper+vue分页
### 实现Spring Boot与PageHelper及Vue.js分页功能集成
#### 后端配置
为了使 Spring Boot 应用程序能够支持分页查询,在 `pom.xml` 文件中引入必要的依赖项[^2]:
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
```
接着定义数据访问层接口 ArticleMapper 并编写 SQL 映射语句来执行带条件的分页检索操作。这里给出一个简单的例子,用于从数据库表 t_article 中按标题模糊匹配并返回指定页面的数据记录集合[^3]。
```java
public interface ArticleMapper {
List<Article> selectByPage(@Param("title") String title, @Param("pageNum") int pageNum,
@Param("pageSize") int pageSize);
}
```
对应的 XML 配置如下所示:
```xml
<select id="selectByPage" parameterType="map" resultType="Article">
SELECT *
FROM t_article
WHERE title LIKE CONCAT('%', #{title}, '%')
LIMIT #{(pageNum - 1) * pageSize}, #{pageSize}
</select>
```
注意上述代码片段中的 LIMIT 子句实现了 MySQL 数据库特有的分页机制;对于其他类型的 RDBMS 可能需要调整相应的语法形式。
在 Service 层调用此方法之前先设置好 PageHelper 的参数即可完成整个流程的设计工作。
#### 前端实现
前端部分采用 Vue.js 单文件组件的形式构建视图界面[^1]。下面展示了一个基本的例子,它展示了如何通过 HTTP 请求获取服务器响应并将结果显示在一个表格内,同时提供翻页按钮让用户浏览不同页码的内容。
```html
<!-- Pagination.vue -->
<template>
<div class="articles-list">
<!-- 表格区域 -->
<table border="1px solid black">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in articlesList" :key="index">
<td>{{ item.id }}</td>
<td>{{ item.title }}</td>
</tr>
</tbody>
</table>
<!-- 分页器 -->
<el-pagination background layout="prev, pager, next"
:total="totalElements"
:current-page.sync="currentPage"
@size-change="handleSizeChange"
@current-change="fetchData"></el-pagination>
</div>
</template>
<script>
import axios from 'axios';
export default {
name: "Pagination",
data() {
return {
currentPage: 1,
totalElements: null,
articlesList: [],
loading: false,
params: {}
};
},
methods: {
fetchData(pageNum = this.currentPage) {
const url = `/api/articles?page=${pageNum}&size=5`;
axios.get(url).then(response => {
let res = response.data;
this.articlesList = res.content || [];
this.totalElements = res.totalElements;
console.log('Fetched Data:', res);
}).catch(error => {
alert(`Error occurred while fetching data ${error}`);
});
}
},
mounted() {
this.fetchData();
}
};
</script>
<style scoped>
/* Add some styles here */
</style>
```
在这个案例里,每当用户点击不同的页数链接时都会触发 `@current-change` 事件处理器函数 `fetchData()` ,该函数会向后端发送带有新页号的新请求从而更新当前显示的文章列表。
springboot + vue实现分页
### 使用Spring Boot和Vue实现分页功能
#### 后端部分:Spring Boot配置
为了在Spring Boot项目中实现分页,可以利用`Spring Data JPA`提供的`Pageable`接口。此接口封装了分页所需的参数,比如当前页面索引和每页条目数等信息[^2]。
创建一个基于JPA的仓库接口继承自`PagingAndSortingRepository<T, ID>`,这使得可以直接调用带有`Pageable`作为参数的方法来获取分页后的查询结果:
```java
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
Page<User> findAll(Pageable pageable);
}
```
控制器层接收来自前端请求中的分页参数并传递给服务逻辑处理:
```java
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
public ResponseEntity<Page<User>> getUsers(@RequestParam int page,
@RequestParam int size) {
Pageable paging = PageRequest.of(page, size);
Page<User> pagedResult = userService.findAllUsers(paging);
return new ResponseEntity<>(pagedResult, HttpStatus.OK);
}
}
```
#### 前端部分:Vue.js组件开发
对于前端而言,在Vue实例内部管理状态变量用于存储当前选中的页码及每页展示的数据数量,并通过HTTP客户端库(如Axios)向后端发起GET请求以加载指定范围内的记录列表。
下面给出一段简单的Vue单文件组件代码片段,展示了如何构建具有基本分页能力的应用界面:
```html
<template>
<div id="app">
<!-- 显示用户列表 -->
<ul v-if="users.length > 0">
<li v-for="(user, index) in users" :key="index">{{ user.name }}</li>
</ul>
<!-- 分页按钮组 -->
<nav aria-label="Page navigation example">
<ul class="pagination justify-content-center">
<li class="page-item" :class="{ disabled: currentPage === 1 }">
<a href="#" class="page-link" @click.prevent="changePage(currentPage - 1)">Previous</a>
</li>
<li class="page-item"
v-for="n in totalPages"
:key="n"
:class="{ active: n === currentPage}">
<a href="#" class="page-link" @click.prevent="changePage(n)">{{ n }}</a>
</li>
<li class="page-item" :class="{ disabled: currentPage >= totalPages }">
<a href="#" class="page-link" @click.prevent="changePage(currentPage + 1)">Next</a>
</li>
</ul>
</nav>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
users: [],
totalElements: 0,
totalPages: 0,
currentPage: 1,
pageSize: 5
};
},
methods: {
async fetchUsers(page = this.currentPage) {
try {
const response = await axios.get(`/api/users?page=${page - 1}&size=${this.pageSize}`);
this.users = response.data.content;
this.totalPages = response.data.totalPages;
this.totalElements = response.data.totalElements;
} catch (error) {
console.error('There was an error fetching the users!', error);
}
},
changePage(newPage) {
if (newPage >= 1 && newPage <= this.totalPages) {
this.currentPage = newPage;
this.fetchUsers(this.currentPage);
}
}
},
created() {
this.fetchUsers();
}
};
</script>
```
上述示例实现了前后端交互完成分页显示的功能,其中涉及到的关键点包括但不限于RESTful风格API设计、AJAX异步通信机制以及响应式UI更新等方面的知识和技术栈组合运用。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)