基于 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);}}