基于 Spring Boot 瑞吉外賣系統開發(三)
分類列表
靜態頁面
實現功能所需要的接口
定義Mapper接口
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {}
定義Service接口
public interface CategoryService extends IService<Category> {
}
定義Service實現類
@Service
public class CategoryServiceImplextends ServiceImpl<CategoryMapper, Category> implements CategoryService {}
配置Mybati Plus的分頁插件
在com.itheima.reggie.config
包下創建配置類,并在配置類中創建MyBatis-Plus分頁插件對象,并交由Spring管理
@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());return mybatisPlusInterceptor;}
}
定義Controller類
@RestController
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;@GetMapping("/page")public R<Page> page(int page, int pageSize) {//分頁構造器Page<Category> pageInfo = new Page<>(page, pageSize);//條件構造器QueryWrapper<Category> query = new QueryWrapper<>();//添加排序條件,根據sort進行排序query.orderByAsc("sort");//分頁查詢categoryService.page(pageInfo, query);return R.success(pageInfo);}}