当前位置:网站首页>6. package management business development
6. package management business development
2022-06-24 10:16:00 【xjhqre】
Package management business development
1、 preparation
Prepare the required classes and interfaces
- Entity class
SetmealDish
- DTO
SetmealDto
- Mapper Interface
SetmealDishMapper
- Business layer interface
SetmealDishService
- Business layer implementation classes
SetmealDishServiceImpl
- Control layer
SetmealController
2、 New package
Execute the process :
- page (backend/page/comboladd.html) send out ajax request , Request the server to obtain the package classification data and display it in the drop-down box
- Page sending ajax request , Get the dish classification data and add it to the service side
- Page sending ajax request , Request server , Query the corresponding dish data according to the dish classification and display it in the add dish window
- The page sends a request for image upload , Request the server to save the picture to the server
- The page sends a request for image download , Echo the uploaded picture
- Click the save button , send out ajax request , The data related to the package is displayed in json Submit to the server in the form of
2.1、 Query the menu classification list
stay DishController
establish list
Method
/** * Query the corresponding dish data according to the conditions * @param dish * @return */
@GetMapping("/list")
public R<List<Dish>> list(Dish dish) {
// Paparazzi query criteria
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
// The query status is 1 The dishes
queryWrapper.eq(Dish::getStatus, 1);
// Add sorting criteria
queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(queryWrapper);
return R.success(list);
}
2.2、 Alibaba cloud OSS Picture echo ( Optional )
modify backend/page/combo/add.html
The following pages , Change to your Alibaba cloud address plus the image name
2.3、 Save new information
2.3.1、SetmealController
package com.itheima.reggie.controller;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;
/** * @Author: xjhqre * @DateTime: 2022/6/19 15:36 */
@RequestMapping("/setmeal")
@RestController
@Slf4j
public class SetmealController {
@Autowired
SetmealService setmealService;
@Autowired
SetmealDishService setmealDishService;
/** * New package * @param setmealDto * @return */
@PostMapping
public R<String> save(@RequestBody SetmealDto setmealDto) {
log.info(" New package information :{}", setmealDto);
setmealService.saveWithDish(setmealDto);
return R.success(" Successfully added package ");
}
}
2.3.2、SetmealService
// New package , At the same time, you need to save the relationship between packages and dishes
void saveWithDish(SetmealDto setmealDto);
2.3.3、SetmealServiceImpl
/** * New package , At the same time, you need to save the relationship between packages and dishes * @param setmealDto */
@Override
@Transactional
public void saveWithDish(SetmealDto setmealDto) {
// Save the basic information of the package , operation setmeal, perform insert operation
this.save(setmealDto);
List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
setmealDishes = setmealDishes.stream().map(item -> {
item.setSetmealId(setmealDto.getId());
return item;
}).collect(Collectors.toList());
// Save the related information of packages and dishes , operation setmeal_dish perform insert operation
setmealDishService.saveBatch(setmealDishes);
}
2.4、 Test the new package
3、 Paging query of package information
Execute the process :
- page (backend/page/combo/list.html) send out ajax request , Paging query parameters (page、pageSize.name) Submit to the server , Get paging data
- Page send request , Request the server to download pictures , For page image display
3.1、 Method 1 : The teacher's method
stay SetmealController
establish page
Method
/** * Paging query of package information * @param page * @param pageSize * @param name * @return */
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name) {
// Page builder object
Page<Setmeal> pageInfo = new Page<>(page, pageSize);
Page<SetmealDto> dtoPage = new Page<>();
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
// Add query criteria , according to name Conduct like Fuzzy query
queryWrapper.like(name != null, Setmeal::getName, name);
// Add sorting criteria , Sort by update time in descending order
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
setmealService.page(pageInfo, queryWrapper);
// Object Copy
BeanUtils.copyProperties(pageInfo, dtoPage, "records");
List<Setmeal> records = pageInfo.getRecords();
List<SetmealDto> list = records.stream().map(item -> {
SetmealDto setmealDto = new SetmealDto();
// Object Copy
BeanUtils.copyProperties(item, setmealDto);
// classification id
Long categoryId = item.getCategoryId();
// According to the classification id Query classification objects
Category category = categoryService.getById(categoryId);
if (category != null) {
// Category name
String categoryName = category.getName();
setmealDto.setCategoryName(categoryName);
}
return setmealDto;
}).collect(Collectors.toList());
dtoPage.setRecords(list);
return R.success(pageInfo);
}
3.2、 Method 2 : Join table query method
3.2.1、SetmealController
/** * Paging query of package information * @param page * @param pageSize * @param name * @return */
@GetMapping("/page")
public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
// Paging constructor
Page<SetmealDto> pageInfo = new Page<>(page, pageSize);
setmealService.querySetmealPageWithCategoryName(pageInfo, name);
return R.success(pageInfo);
}
3.2.2、SetmealService
// Query the package paging information with category name
void querySetmealPageWithCategoryName(Page<SetmealDto> pageInfo, String name);
3.2.3、SetmealServiceImpl
/** * Query the package paging information with category name * @param pageInfo * @param name */
@Override
public void querySetmealPageWithCategoryName(Page<SetmealDto> pageInfo, String name) {
List<SetmealDto> records = setmealMapper.querySetmealPageWithCategoryName(pageInfo, name == null ? "" : name);
pageInfo.setRecords(records);
}
3.2.4、SetmealMapper
@Select("SELECT a.*, b.`name` AS category_name FROM setmeal a JOIN category b ON a.`category_id` = b.`id` WHERE a.`name` LIKE CONCAT('%',#{name},'%') AND a.`is_deleted` = 0 ORDER BY sort")
List<SetmealDto> querySetmealPageWithCategoryName(Page<SetmealDto> pageInfo, String name);
3.3、 Package management page Alibaba cloud OSS Picture shows
modify backend/page/combo/list.html
There are two places in the document
3.4、 Test paging query
4、 Delete package information
- The front end sends a delete request , The parameter form is
ids=id1,id2,...
- The back end receives requests , according to
id
Delete the table first setmeal_dish Table contents , In the delete setmeal Table contents
The deletion method is logical deletion
4.1 modify Setmeal and SetmealDish Entity class
Modify the... Of two entity classes isDeleted
attribute , add @TableLogic(value = "0", delval = "1")
annotation . For logical deletion . After marking this note , call mybatis-plus The deletion method given in the framework , Will turn into pairs is_deleted
Field update method
// Whether or not to delete
@TableLogic(value = "0", delval = "1")
private Integer isDeleted;
4.2、SetmealController
establish delete Method
/** * Delete package * @param ids * @return */
@DeleteMapping
public R<String> delete(@RequestParam Long[] ids) {
log.info(" To delete the package ids:{}", Arrays.toString(ids));
setmealService.removeSetmealWithDish(ids);
return R.success(" Package data deleted successfully ");
}
4.3、SetmealService
// Delete package , At the same time, you need to delete the associated data of packages and dishes
void removeSetmealWithDish(Long[] ids);
4.4、SetmealServiceImpl
/** * Delete package , At the same time, you need to delete the associated data of packages and dishes * @param ids */
@Override
@Transactional
public void removeSetmealWithDish(Long[] ids) {
// Query package status , Determine if delete... Is available
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(Setmeal::getId, ids);
queryWrapper.eq(Setmeal::getStatus, 1);
int count = this.count(queryWrapper);
if (count > 0) {
// The current package is for sale , Can't delete
throw new CustomException(" The package is on sale , Can't delete ");
}
// Delete if available , Delete the table first setmeal_dish Information about the associated dishes in
// delete from setmeal_dish where setmeal_id in ()
LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
setmealDishService.remove(setmealDishLambdaQueryWrapper);
// Then delete the package information
this.removeByIds(Arrays.asList(ids));
}
4.5、 Test delete package
5、 Modify package information ( Optional )
5.1、 Package data echo
Click Modify , The front page will send a query request , with id Parameters
We need to return a SetmealDto Object of type
5.1.1、SetmealController
/** * according to id Query package information and corresponding dish information * @param id * @return */
@GetMapping("/{id}")
public R<SetmealDto> get(@PathVariable Long id) {
SetmealDto setmealDto = setmealService.getByIdWithDish(id);
return R.success(setmealDto);
}
5.1.2、SetmealService
// according to id Query package information and corresponding dish information
SetmealDto getByIdWithDish(Long id);
5.1.3、SetmealServiceImpl
/** * according to id Query package information and corresponding dish information * @param id * @return */
@Override
@Transactional
public SetmealDto getByIdWithDish(Long id) {
// Query the basic information of the package
Setmeal setmeal = this.getById(id);
SetmealDto setmealDto = new SetmealDto();
BeanUtils.copyProperties(setmeal, setmealDto);
// Query the menu information corresponding to the current package
// select * from setmeal_dish where setmeal_id = id;
LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SetmealDish::getSetmealId, id);
List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
setmealDto.setSetmealDishes(setmealDishes);
return setmealDto;
}
5.1.4、 Alibaba cloud OSS Picture echo ( Optional )
modify backend/page/combo/add.html
The document contains the following
5.1.5、 Test data echo
5.2、 Save modification information
Delete first setmeal All corresponding dish, Adding
5.2.1、SetmealController
/** * Modify package * @param setmealDto * @return */
@PutMapping
public R<String> update(@RequestBody SetmealDto setmealDto) {
log.info(" Save and modify package information :{}", setmealDto);
setmealService.updateSetmealWithDish(setmealDto);
return R.success(" Package modification succeeded ");
}
5.2.2、SetmealService
// Update package information
void updateSetmealWithDish(SetmealDto setmealDto);
5.2.3、SetmealServiceImpl
/** * Update package information * @param setmealDto */
@Override
public void updateSetmealWithDish(SetmealDto setmealDto) {
// to update setmeal Table basic information
this.updateById(setmealDto);
// Delete the original corresponding dish information
LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
setmealDishService.remove(queryWrapper);
// Add new dish information
List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
setmealDishes = setmealDishes.stream().peek(item -> item.setSetmealId(setmealDto.getId())).collect(Collectors.toList());
setmealDishService.saveBatch(setmealDishes);
}
5.2.4、 Test modification package
6、 Start selling in batches / Stop selling package ( Optional )
Front end page request :
6.1、SetmealController
/** * Start selling in batches / Stop selling package * @param status * @param ids * @return */
@PostMapping("/status/{status}")
public R<String> status(@PathVariable("status") int status, @RequestParam Long[] ids) {
log.info(" Start and stop selling in batches :{}", status, Arrays.toString(ids));
setmealService.updateStatusByIds(status, ids);
return R.success(" Successful start and stop of batch sales ");
}
6.2、SetmealService
// Start and stop package in batch
void updateStatusByIds(int status, Long[] ids);
6.3、SetmealServiceImpl
do not know why mybat-plus There is no method for batch modification .
Here I directly update in the loop for convenience .
Another way is to mapper Batch update in the file .
I don't know which method is more elegant . If there is a big guy who knows, I hope I can answer it in the comment area .
/** * Start and stop package in batch * @param status * @param ids */
@Override
public void updateStatusByIds(int status, Long[] ids) {
// Query all corresponding id Of setmeal
List<Setmeal> setmeals = setmealMapper.selectBatchIds(Arrays.asList(ids));
// Loop through updates
setmeals.forEach(item -> {
item.setStatus(status);
setmealMapper.updateById(item);
});
}
6.4、 Test the start and stop of batch sales
边栏推荐
- Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
- 被困英西中学的师生安全和食物有保障
- 如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
- 使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
- Leetcode-498: diagonal traversal
- CVPR 2022 oral | NVIDIA proposes an efficient visual transformer network a-vit with adaptive token. The calculation of unimportant tokens can be stopped in advance
- Wechat cloud hosting launch public beta: in the appointment of the publicity meeting
- leetCode-498: 对角线遍历
- El table Click to add row style
- 一群骷髅在飞canvas动画js特效
猜你喜欢
Binary tree part I
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
3.员工的增删改查
Machine learning - principal component analysis (PCA)
上升的气泡canvas破碎动画js特效
美国电子烟巨头 Juul 遭遇灭顶之灾,所有产品强制下架
uniapp实现点击拨打电话功能
微信小程序學習之 實現列錶渲染和條件渲染.
How to standardize data center infrastructure management process
SQL Server AVG function rounding
随机推荐
SQL Sever中的窗口函数row_number()rank()dense_rank()
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
Nvisual digital infrastructure operation management software platform
What are the characteristics of EDI local deployment and cloud hosting solutions?
Detailed explanation of PHP singleton mode
线程的 sleep() 方法与 wait() 方法的区别
Canvas draw picture
Status of the thread pool
413-二叉树基础
SQL sever试题求最晚入职日期
How does home office manage the data center network infrastructure?
[db2] sql0805n solution and thinking
Three ways to use applicationcontextinitializer
上升的气泡canvas破碎动画js特效
Use of vim
SQL statistics of users logged in for N consecutive days
MySQL data advanced
如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
Cookie encryption 4 RPC method determines cookie encryption
Symbol. Iterator iterator