课外开发苍穹外卖苍穹外卖学习笔记(二)
Jie一.新增员工
- 编写新增员工接口设计的 DTO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.sky.dto;
import lombok.Data;
import java.io.Serializable;
@Data public class EmployeeDTO implements Serializable {
private Long id;
private String username;
private String name;
private String phone;
private String sex;
private String idNumber;
}
|
- controller
1 2 3 4 5 6 7 8 9 10 11 12
|
@PostMapping @ApiOperation("新增员工") public Result save(@RequestBody EmployeeDTO employeeDTO) { log.info("新增员工:{}", employeeDTO); employeeService.save(employeeDTO); return Result.success(); }
|
- service
1 2 3 4 5 6
|
void save(@RequestBody EmployeeDTO employeeDTO);
|
- serviceimpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
@Override public void save(EmployeeDTO employeeDTO) { Employee employee = new Employee(); BeanUtils.copyProperties(employeeDTO, employee); employee.setStatus(StatusConstant.ENABLE); byte[] salt = new byte[SALT_LENGTH]; RANDOM.nextBytes(salt); employee.setSalt(Base64.getEncoder().encodeToString(salt)); String password = hashPassword(PasswordConstant.DEFAULT_PASSWORD, employee.getSalt()); employee.setPassword(password); employee.setCreateTime(LocalDateTime.now()); employee.setUpdateTime(LocalDateTime.now()); employee.setCreateUser(10L); employee.setUpdateUser(10L); employeeMapper.insert(employee); }
|
由于我这里将 Mybatis 换成了 Mybatis-plus,所以 Mybatis 相关的代码不再描写
- 引入依赖
1 2 3 4 5 6
| <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.7</version> </dependency>
|
- 修改 application.yml 配置文件
1 2 3 4 5 6 7
| mybatis-plus: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
- 修改 employee 实体类,添加 ID 自动增长策略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.sky.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor;
import java.io.Serializable; import java.time.LocalDateTime;
@Data @Builder @NoArgsConstructor @AllArgsConstructor @TableName("employee") public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO) private Long id;
private String username;
private String name;
private String password;
private String phone;
private String sex;
private String idNumber;
private Integer status;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private Long createUser;
private Long updateUser;
private String salt;
}
|
- 删除 resource 下的 mapper 包,将 server 中的 mapper 包改为继承自 BaseMapper
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.sky.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.sky.dto.EmployeeDTO; import com.sky.entity.Employee; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;
@Mapper public interface EmployeeMapper extends BaseMapper<Employee> {
}
|
- 修改启动类,增加 mapper 扫描区间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.sky;
import lombok.extern.slf4j.Slf4j; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication @EnableTransactionManagement @MapperScan("com.sky.mapper") @Slf4j public class SkyApplication { public static void main(String[] args) { SpringApplication.run(SkyApplication.class, args); log.info("server started"); } }
|
二.员工分页查询
- 编写分页查询接口的 DTO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.sky.dto;
import lombok.Data;
import java.io.Serializable;
@Data public class EmployeePageQueryDTO implements Serializable {
private String name;
private int page;
private int pageSize;
}
|
- controller
1 2 3 4 5 6 7
| @GetMapping("/page") @ApiOperation("分页查询员工") public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) { log.info("分页查询员工:{}", employeePageQueryDTO); employeeService.page(employeePageQueryDTO); return Result.success(); }
|
- service
1 2 3 4 5 6
|
PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
|
- serviceimpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Transactional @Override public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) { Page<Employee> page = new Page<>(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize()); LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.isNotNull(Employee::getName); if (StringUtils.isNotBlank(employeePageQueryDTO.getName())) { queryWrapper.like(Employee::getName, employeePageQueryDTO.getName()); } queryWrapper.orderByDesc(Employee::getCreateTime); Page<Employee> employeePage = employeeMapper.selectPage(page, queryWrapper); long total = employeePage.getTotal(); List<Employee> records = employeePage.getRecords(); return new PageResult(total, records); }
|
注意:使用 MP 分页时不要配置 MP 分页插件,否则会找不到对象(Mapper)
三.启用禁用员工账号
- controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
@PostMapping("/status/{status}") @ApiOperation("修改员工状态") public Result startOrStop(Long id, @PathVariable Integer status) { log.info("修改员工状态:id={},status={}", id, status); employeeService.startOrStop(id, status); return Result.success(); }
|
- service
1 2 3 4 5 6
|
void startOrStop(Long id, Integer status);
|
- impl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
@Override public void startOrStop(Long id, Integer status) {
Employee employee = Employee.builder() .id(id) .status(status) .updateTime(LocalDateTime.now()) .updateUser(BaseContext.getCurrentId()) .build(); employeeMapper.updateById(employee); }
|
四.编辑员工
- controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@GetMapping("/{id}") @ApiOperation("根据id查询员工") public Result<Employee> getById(@PathVariable Long id) { log.info("根据id查询员工:id={}", id); Employee employee = employeeService.getById(id); return Result.success(employee); }
@PutMapping() @ApiOperation("修改员工") public Result update(@RequestBody EmployeeDTO employeeDTO) { log.info("修改员工:{}", employeeDTO); employeeService.update(employeeDTO); return Result.success(); }
|
- service
1 2 3 4 5 6 7 8 9 10 11 12
|
Employee getById(Long id);
void update(EmployeeDTO employeeDTO);
|
- impl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
@Override public Employee getById(Long id) { Employee employee = employeeMapper.selectById(id); employee.setPassword("********"); return employee; }
@Override public void update(EmployeeDTO employeeDTO) { Employee employee = new Employee(); BeanUtils.copyProperties(employeeDTO, employee); employee.setUpdateTime(LocalDateTime.now()); employee.setUpdateUser(BaseContext.getCurrentId()); employeeMapper.updateById(employee); }
|
五.导入分类模块功能代码
最后的分类管理接口已经在分类分页查询中内置,可以删掉,好吧,后面添加菜品需要这个接口,不可以删掉
- controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| package com.sky.controller.admin;
import com.sky.dto.CategoryDTO; import com.sky.dto.CategoryPageQueryDTO; import com.sky.entity.Category; import com.sky.result.PageResult; import com.sky.result.Result; import com.sky.service.CategoryService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RequestMapping("/admin/category") @Slf4j @Api(tags = "分类管理") public class CategoryController {
@Autowired private CategoryService categoryService;
@PostMapping @ApiOperation("添加分类") public Result<String> saveCategory(@RequestBody CategoryDTO categoryDTO) { log.info("添加分类:{}", categoryDTO); categoryService.saveCategory(categoryDTO); return Result.success(); }
@GetMapping("/page") @ApiOperation("分类分页查询") public Result<PageResult> pageCategory(CategoryPageQueryDTO categoryPageQueryDTO) { log.info("分类分页查询:{}", categoryPageQueryDTO); PageResult pageResult = categoryService.pageCategory(categoryPageQueryDTO); return Result.success(pageResult); }
@PostMapping("/status/{status}") @ApiOperation("修改分类状态") public Result<String> updateCategoryStatus(@PathVariable("status") Integer status, Long id) { log.info("修改分类状态:status={}, id={}", status, id); categoryService.updateCategoryStatus(status, id); return Result.success(); }
@DeleteMapping @ApiOperation("根据id删除分类") public Result<String> deleteCategory(Long id) { log.info("删除分类:id={}", id); categoryService.deleteCategory(id); return Result.success(); }
@PutMapping @ApiOperation("修改分类") public Result updateCategory(@RequestBody CategoryDTO categoryDTO) { log.info("修改分类:{}", categoryDTO); categoryService.updateCategory(categoryDTO); return Result.success(); }
@GetMapping("/list") @ApiOperation("根据类型查询分类") public Result<List<Category>> listCategory(Integer type) { log.info("根据类型查询分类:type={}", type); List<Category> list = categoryService.listCategory(type); return Result.success(list); } }
|
- service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.sky.service;
import com.sky.dto.CategoryDTO; import com.sky.dto.CategoryPageQueryDTO; import com.sky.entity.Category; import com.sky.result.PageResult; import org.apache.ibatis.annotations.Param; import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
public interface CategoryService {
void saveCategory(@RequestBody CategoryDTO categoryDTO);
PageResult pageCategory(CategoryPageQueryDTO categoryPageQueryDTO);
void updateCategoryStatus(Integer status, Long id);
void deleteCategory(Long id);
void updateCategory(CategoryDTO categoryDTO);
List<Category> listCategory(@Param("type") Integer type); }
|
- impl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| package com.sky.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.sky.constant.StatusConstant; import com.sky.context.BaseContext; import com.sky.dto.CategoryDTO; import com.sky.dto.CategoryPageQueryDTO; import com.sky.entity.Category; import com.sky.entity.Employee; import com.sky.mapper.CategoryMapper; import com.sky.result.PageResult; import com.sky.service.CategoryService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime; import java.util.List;
@Slf4j @Service public class CategoryServiceImpl implements CategoryService {
@Autowired private CategoryMapper categoryMapper;
@Transactional @Override public void saveCategory(CategoryDTO categoryDTO) {
Category category = Category.builder() .name(categoryDTO.getName()) .id(categoryDTO.getId()) .sort(categoryDTO.getSort()) .type(categoryDTO.getType()) .build(); category.setStatus(StatusConstant.DISABLE);
log.info("添加分类:{}", category); categoryMapper.insert(category); }
@Transactional @Override public PageResult pageCategory(CategoryPageQueryDTO categoryPageQueryDTO) { Page<Category> page = new Page<>(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize()); LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>(); if (categoryPageQueryDTO.getType() != null) { queryWrapper.eq(Category::getType, categoryPageQueryDTO.getType()); } queryWrapper.isNotNull(Category::getName); if (StringUtils.isNotBlank(categoryPageQueryDTO.getName())) { queryWrapper.like(Category::getName, categoryPageQueryDTO.getName()); } queryWrapper.orderByDesc(Category::getSort); Page<Category> categoryPage = categoryMapper.selectPage(page, queryWrapper); long total = categoryPage.getTotal(); List<Category> records = categoryPage.getRecords(); return new PageResult(total, records); }
@Override public void updateCategoryStatus(Integer status, Long id) { Category build = Category.builder() .id(id) .status(status) .updateTime(LocalDateTime.now()) .updateUser(BaseContext.getCurrentId()) .build(); categoryMapper.updateById(build); }
@Override public void deleteCategory(Long id) { categoryMapper.deleteById(id); }
@Override public void updateCategory(CategoryDTO categoryDTO) { Category build = Category.builder() .id(categoryDTO.getId()) .name(categoryDTO.getName()) .sort(categoryDTO.getSort()) .type(categoryDTO.getType()) .updateTime(LocalDateTime.now()) .updateUser(BaseContext.getCurrentId()) .build(); categoryMapper.updateById(build); }
@Override public List<Category> listCategory(Integer type) { LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>(); boolean flag = type != null; queryWrapper.eq(flag, Category::getType, type) .eq(Category::getStatus, StatusConstant.ENABLE) .orderByAsc(Category::getSort) .orderByDesc(Category::getCreateTime); List<Category> categories = categoryMapper.selectList(queryWrapper); log.info("分类列表:{}", categories); return categories; } }
|
- mapper
1 2 3 4
| @Mapper public interface CategoryMapper extends BaseMapper<Category> {
}
|