【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】
其实功能做到现在,很多事情已经很明朗了,
查全部改成了分页,现在要加条件,不就是在执行分页的时候把条件带上嘛。
找到页面中
就是这仨了
但是现在数据模型还没有
我们可以直接将条件数据加到分页的数据中
pagination: {//分页相关模型数据
currentPage: 1,//当前页码
pageSize: 10,//每页显示的记录数
total: 0,//总记录数
type: "",
name: "",
description: ""
}
实现双向绑定
现在数据就已经有了
现在就将其使用到发请求
getAll() {
//组织参数,拼接URL请求地址
param = "?type=" + this.pagination.type;
param += "&name=" + this.pagination.name;
param += "&description=" + this.pagination.description;
console.log("-------" + param);
//发送异步请求
axios.get('/books/' + this.pagination.currentPage + "/" + this.pagination.pageSize + param).then((res) => {
console.log(res.data);
this.pagination.pageSize = res.data.data.size;
this.pagination.currentPage = res.data.data.current;
this.pagination.total = res.data.data.total;
this.dataList = res.data.data.records;
})
},
OK,这样就发过去了
【controller 接收参数】
@GetMapping("/{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage, @PathVariable int pageSize,Book book) {
System.out.println("参数======>>>>" + book);
IPage<Book> page = bookService.getPage(currentPage, pageSize);
//如果当前页码值大于了总页码值,那么重新执行查询操作,使用最大页码值作为当前页码值
if (currentPage > page.getPages()){
page = bookService.getPage((int) page.getPages(),pageSize);
}
return new R(true, page);
}
重启服务器试试
OK,这样就可以拿到了
新增业务层接口
IPage<Book> getPage(int currentPage, int pageSize , Book book);
重载
实现类补充
@Override
public IPage<Book> getPage(int currentPage, int pageSize, Book book) {
IPage page = new Page(currentPage,pageSize);
LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<Book>();
queryWrapper.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());
queryWrapper.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
queryWrapper.like(Strings.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription(););
return bookDao.selectPage(page , queryWrapper);
}
修改一下控制器
@GetMapping("/{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage, @PathVariable int pageSize,Book book) {
System.out.println("参数======>>>>" + book);
IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
//如果当前页码值大于了总页码值,那么重新执行查询操作,使用最大页码值作为当前页码值
if (currentPage > page.getPages()){
page = bookService.getPage((int) page.getPages(),pageSize);
}
return new R(null != page, page);
}
直接运行
OK, 从日志都可以看出来,已经成功了。就是这样
回顾一下
查询条件数据封装
页面数据模型绑定
组织数据成为get请求发送的数据
getAll() {
//1.获取查询条件,拼接查询条件
param = "?name="+this.pagination.name;
param += "&type="+this.pagination.type;
param += "&description="+this.pagination.description;
console.log("-----------------"+ param);
axios.get("/books/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param)
.then((res) => {
this.dataList = res.data.data.records;
});
},
Controller接收参数
@GetMapping("{currentPage}/{pageSize}")
public R getAll(@PathVariable int currentPage,@PathVariable int pageSize,Book book) {
System.out.println("参数=====>"+book);
IPage pageBook = bookService.getPage(currentPage,pageSize);
return new R(null != pageBook ,pageBook);
}
直接写参数能接的原因:如果有实体类,会自动实现映射
业务层接口功能开发
public interface IBookService extends IService {
IPage getPage(Integer currentPage,Integer pageSize,Book queryBook);
}
实现类
@Service
public class BookServiceImpl2 extends ServiceImpl implements IBookService {
public IPage getPage(Integer currentPage,Integer pageSize,Book queryBook){
IPage page = new Page(currentPage,pageSize);
LambdaQueryWrapper lqw = new LambdaQueryWrapper();
lqw.like(Strings.isNotEmpty(queryBook.getName()),Book::getName,queryBook.getName());
lqw.like(Strings.isNotEmpty(queryBook.getType()),Book::getType,queryBook.getType());
lqw.like(Strings.isNotEmpty(queryBook.getDescription()),
Book::getDescription,queryBook.getDescription());
return bookDao.selectPage(page,lqw);
}
}
Controller调用业务层分页条件查询接口
@GetMapping("{currentPage}/{pageSize}")
public R getAll(@PathVariable int currentPage,@PathVariable int pageSize,Book book) {
IPage pageBook = bookService.getPage(currentPage,pageSize,book);
return new R(null != pageBook ,pageBook);
}
页面回显数据
getAll() {
//1.获取查询条件,拼接查询条件
param = "?name="+this.pagination.name;
param += "&type="+this.pagination.type;
param += "&description="+this.pagination.description;
console.log("-----------------"+ param);
axios.get("/books/"+this.pagination.currentPage+"/"+this.pagination.pageSize+param)
.then((res) => {
this.pagination.total = res.data.data.total;
this.pagination.currentPage = res.data.data.current;
this.pagination.pagesize = res.data.data.size;
this.dataList = res.data.data.records;
});
},