@Data public class Dish implements Serializable { private Long id;//主键 private String name;//菜品名称 private Long categoryId;//所属分类id private BigDecimal price;//菜品价格 private String code;//商品码 private String image;//图片 private String description;//描述信息 private Integer status;//0 停售 1 起售 private Integer sort;//顺序 @TableField(fill = FieldFill.INSERT) @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; @TableField(fill = FieldFill.INSERT_UPDATE) @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; @TableField(fill = FieldFill.INSERT) private Long createUser; @TableField(fill = FieldFill.INSERT_UPDATE) private Long updateUser; //===================数据表中不存在下面字段,仅仅用于页面显示=================== //菜品口味列表 @TableField(exist = false) private List<DishFlavor> flavors = new ArrayList<>(); //菜品分类名称 @TableField(exist = false) private String categoryName; @TableField(exist = false) private Integer copies; }利用springboot vue分页查询并能与前段交互
时间: 2023-08-08 07:05:48 浏览: 156
Java高级程序设计实战教程-综合实训代码.docx
首先,在后端(Spring Boot)中需要编写一个接口,用于接受前端传来的分页参数,并返回相应的数据。可以使用Spring Data JPA来操作数据库,实现数据的分页查询。
例如:
```java
@RestController
@RequestMapping("/dish")
public class DishController {
@Autowired
private DishRepository dishRepository;
@GetMapping("/list")
public ResponseEntity<Page<Dish>> getDishList(@RequestParam(name = "pageNumber", defaultValue = "0") int pageNumber,
@RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
Pageable pageable = PageRequest.of(pageNumber, pageSize, Sort.Direction.ASC, "id");
Page<Dish> dishPage = dishRepository.findAll(pageable);
return ResponseEntity.ok(dishPage);
}
}
```
在前端(Vue)中,可以使用axios库来发送请求,获取后端返回的数据,并实现分页效果。
例如:
```vue
<template>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr v-for="dish in dishList" :key="dish.id">
<td>{{ dish.id }}</td>
<td>{{ dish.name }}</td>
<td>{{ dish.categoryName }}</td>
<td>{{ dish.price }}</td>
</tr>
</tbody>
</table>
<div class="pagination">
<button :disabled="pageNumber === 0" @click="prevPage">Prev</button>
<span>{{ pageNumber + 1 }} / {{ totalPages }}</span>
<button :disabled="pageNumber + 1 === totalPages" @click="nextPage">Next</button>
</div>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
dishList: [],
pageNumber: 0,
pageSize: 10,
totalPages: 0
};
},
created() {
this.getDishList();
},
methods: {
getDishList() {
axios.get(`/dish/list?pageNumber=${this.pageNumber}&pageSize=${this.pageSize}`)
.then((response) => {
this.dishList = response.data.content;
this.totalPages = response.data.totalPages;
});
},
prevPage() {
this.pageNumber--;
this.getDishList();
},
nextPage() {
this.pageNumber++;
this.getDishList();
}
}
};
</script>
```
阅读全文