Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the new dishes and paging query in Java

2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article "Java new dishes and paging query how to achieve" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Java new dishes and paging query how to achieve" article bar.

one。 Demand Analysis of newly added dishes 1.1

The background system can manage classification information, classification of dishes and classification of packages. When we add dishes in the background system, we need to select a category of dishes.

When we add a package to the background system, we need to select a package category, and the corresponding dishes and packages will be displayed according to the dish classification and package classification on the mobile end.

At the same time, the classification of dishes and packages are added to the classification management page of the background system:

Add dish classification

Add package category

Data model:

Relates to a table Category table:

The data JavaBean corresponding to the table is Category.java

Category.java

Package com.itheima.reggie.entity;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;import lombok.Getter;import lombok.Setter;import java.io.Serializable;import java.time.LocalDateTime;/** * Classification * / @ Datapublic class Category implements Serializable {private static final long serialVersionUID = 1L; private Long id / / Type 1 dish category 2 package category private Integer type; / / category name private String name; / / sequence private Integer sort; / / create time @ TableField (fill = FieldFill.INSERT) private LocalDateTime createTime; / / update time @ TableField (fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; / / founder @ TableField (fill = FieldFill.INSERT) private Long createUser / / modifier @ TableField (fill = FieldFill.INSERT_UPDATE) private Long updateUser; / / whether to delete private Integer isDeleted;}

The specific shelf refers to the construction of the Employee employee entity in front of it.

1.2 Code Development

The service address of the request for new dish classification and package classification is the same as the submitted JSON data structure. The server only needs to provide one method:

API specification value request URL/category request data {

Name: Sichuan cuisine

"type": "1"

"sort": "1"

} Code

Write new code in CategoryController.java:

Package com.itheima.reggie.controller;import com.itheima.reggie.common.R;import com.itheima.reggie.entity.Category;import com.itheima.reggie.service.CategoryService;import lombok.extern.slf4j.Slf4j;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource / * @ author jektong * @ date 21:47, 06 May 2022 * / @ RestController@Slf4j@RequestMapping ("/ category") public class CategoryController {@ Resource private CategoryService categoryService; / * New category * @ param category * @ return * / @ PostMapping public R save (@ RequestBody Category category) {log.info ("category: {}", category); categoryService.save (category) Return R.success ("new category successfully");}} II. Paging query of classified information

The paging query is the same as the previous employee information query, directly on the code:

@ GetMapping ("/ page") public R page (int page, int pageSize) {/ / paging construction Page pageInfo = new Page (page,pageSize); / / query and sort LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper (); queryWrapper.orderByAsc (Category::getSort); / / paging query categoryService.page (pageInfo,queryWrapper); return R.success (pageInfo);} III. Delete Category 3.1 requirements Analysis

On the category management list page, you can delete a category. It should be noted that when the category is associated with dishes or packages, this category is not allowed to be deleted.

API

Specify the value request URL/category?id=

You need to use two entities: imported dishes and set meals:

Dish.java: dish entity

Package com.itheima.reggie.entity;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;import java.io.Serializable;import java.math.BigDecimal;import java.time.LocalDateTime;/** food * / @ Datapublic class Dish implements Serializable {private static final long serialVersionUID = 1L; private Long id; / / dish name private String name / / vegetable Classification id private Long categoryId; / / vegetable Price private BigDecimal price; / / Product Code private String code; / / Picture private String image; / / description Information private String description; / / 0 stop Sale 1 start-up private Integer status; / / order private Integer sort; @ TableField (fill = FieldFill.INSERT) private LocalDateTime createTime; @ TableField (fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime @ TableField (fill = FieldFill.INSERT) private Long createUser; @ TableField (fill = FieldFill.INSERT_UPDATE) private Long updateUser; / / whether to delete private Integer isDeleted;}

Setmeal.java: package entity

Package com.itheima.reggie.entity;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;import java.io.Serializable;import java.math.BigDecimal;import java.time.LocalDateTime;/** * package * / @ Datapublic class Setmeal implements Serializable {private static final long serialVersionUID = 1L; private Long id; / / classified id private Long categoryId / / package name private String name; / / package price private BigDecimal price; / / status 0: disable 1: enable private Integer status; / / Encoding private String code; / / description Information private String description; / / Picture private String image; @ TableField (fill = FieldFill.INSERT) private LocalDateTime createTime; @ TableField (fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime @ TableField (fill = FieldFill.INSERT) private Long createUser; @ TableField (fill = FieldFill.INSERT_UPDATE) private Long updateUser; / / whether to delete the private Integer isDeleted;} 3.2 core code

CategoryServiceImpl.java

Package com.itheima.reggie.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.itheima.reggie.common.CustomException;import com.itheima.reggie.entity.Category;import com.itheima.reggie.entity.Dish;import com.itheima.reggie.entity.Setmeal;import com.itheima.reggie.mapper.CategoryMapper;import com.itheima.reggie.service.CategoryService;import com.itheima.reggie.service.DishService;import com.itheima.reggie.service.SetmealService Import org.springframework.stereotype.Service;import javax.annotation.Resource;/** * @ author jektong * @ date 21:44 * / @ Servicepublic class CategoryServiceImpl extends ServiceImpl implements CategoryService {@ Resource private DishService dishService; @ Resource private SetmealService setmealService; / * according to ID deletion classification, you need to judge * @ param id * / @ Override public void remove (Long id) {LambdaQueryWrapper dishLambdaQueryWrapper = new LambdaQueryWrapper () before you delete the classification. / / query whether the current category is associated with dishes. If so, throw an exception dishLambdaQueryWrapper.eq (Dish::getCategoryId,id); int count = dishService.count (dishLambdaQueryWrapper); if (count > 0) {/ / already associated dishes, throw an exception throw new CustomException ("current category is associated with dishes and cannot be deleted") } / / query whether the current category is associated with a package. If so, throw an exception LambdaQueryWrapper setmealLambdaQueryWrapper = new LambdaQueryWrapper (); setmealLambdaQueryWrapper.eq (Setmeal::getCategoryId,id); int count1 = setmealService.count (setmealLambdaQueryWrapper) If (count > 0) {/ / already associated package, throw exception throw new CustomException ("current category is associated with package and cannot be deleted");} / / normal delete category super.removeById (id);}}

Add the following to the previous custom exception class:

/ * param customException * @ return * / @ ExceptionHandler (CustomException.class) public R exceptionHandler (CustomException customException) {log.error (customException.getMessage ()); return R.error (customException.getMessage ());}

CustomException.java

Package com.itheima.reggie.common;/** * @ author jektong * @ date 22:26 on May 10, 2022 * / public class CustomException extends RuntimeException {public CustomException (String msg) {super (msg);}} four. Modify classification

It is easy to modify the classification, just modify it according to the category ID. The code is as follows:

@ PutMappingpublic R update (@ RequestBody Category category) {log.info ("modify classification information {}" + category); categoryService.updateById (category); return R.success ("classification modified successfully") } the above is about the content of the article "how to realize the new dishes and paging query in Java". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 226

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report