瑞吉外卖Day03

本文介绍了如何在SpringBoot项目中管理员工账号,包括启用/禁用、编辑功能的Controller实现,以及使用ThreadLocal和MetaObjectHandler进行公共字段的自动填充。还涉及了Category的增删改操作和分页查询功能的实现。

 小张推荐:瑞吉外卖Day02    ,    瑞吉外卖Day04

1.启用/禁用员工账号

1.1 思路分析

1.2Controller层 

  @PutMapping()
    public R<String> update(@RequestBody Employee employee, HttpServletRequest request) {
        log.info(employee.toString());

        Long emp = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(emp);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

2.编辑员工信息

2.1需求分析

2.2Controller实现 

 @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id) {
        log.info("根据员工查询");
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("查询无果");
    }

3.公共字段自动填充

3.1公共字段何为

前面我们已经完成了后台系统的员工管理功能开发,在新增员工时需要设置创建时间、创建人、修改时间、修改人等字段,在编辑员工时需要设置修改时间和修改人等字段。这些字段属于公共字段,也就是很多表中都有这些字段。

3.2字段自动填充 

  • 2.按照框架要求编写元数据对象处理器,在此类中统一为公共字段赋值,此类需要实现MetaObjectHandler接口

1.实体类添加@TalbeField

1.在实体类的属性上加入@TableField注解,指定自动填充的策略

@Data
@TableName("employee")
@NoArgsConstructor
@AllArgsConstructor
public class Employee {

    private Long id;
    private String username;
    private String name;
    private String password;
    private String phone;
    private String sex;
    private String idNumber;
    private Integer status;

    @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;
}

2.ThreadLocal 

ThreadLocal并不是一个Thread,而是Thread的局部变量。当使用ThreadLocal维护变量时, ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。

  • 在拦截器里面获取当前登陆对象的id
  • 在用登录对象的id时直接调用即可 
/**
 * 基于ThreadLocal封装工具类,用于保存和获取当前登录用户的id
 */
public class BaseContext {
    private static ThreadLocal<Long> threadLocal=new ThreadLocal<>();

    public static  void setThreadLocal(Long id){
        threadLocal.set(id);
    }

    public static Long getCurrentId(){
      return threadLocal.get();
    }
}

2.实现MetaObjectHandler接口

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入操作自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    /**
     * 更新操作自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}

4.新增分类

4.1实体类

@Data
public class Category {
    private Long id;
    private Integer type;
    private String name;
    private Integer sort;
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    @TableField(fill = FieldFill.INSERT)
    private Long creteEmployee;
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateEmployee;
}

4.2mapper层

@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}

4.3service层

public interface CategoryService extends IService<Category> {
}
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
}

4.4controller

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("新增分类成功");
    }

}

 5.分类信息分页查询

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        //分页构造器
        Page<Category> pageInfo = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        //根据sort排序
        queryWrapper.orderByAsc(Category::getSort);
        //查询
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo); 
    }

6.删除分类

需要先查看分类是否关联的菜品或套餐

6.1Service层

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据id删除分类,删除之前判断是否关联菜品
     *
     * @param id
     */
    @Override
    public void remove(Long id) {
        //查询当前分类是否关联了菜品,如果已经关联,抛出一个异常
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        //根据id查询
        dishQueryWrapper.eq(Dish::getCategoryId, id);
        long count1 = dishService.count(dishQueryWrapper);
        if (count1 > 0) {//说明关联菜品
            throw new CustomException("当前分类下关联菜品");
        }

        //查询当前分类是否关联了套餐,如果已经关联,抛出一个异常
        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        //根据id查询
        setmealQueryWrapper.eq(Setmeal::getCategoryId, id);
        long count2 = setmealService.count(setmealQueryWrapper);
        if (count2 > 0) {//说明关联套餐
            throw new CustomException("当前分类下关联套餐");
        }

        //正常删除
        super.removeById(id);
    }
}

6.2controller层

  /**
     * 删除分类
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        categoryService.remove(id);
        return R.success("删除成功");
    }

7.修改分类

 @PutMapping()
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会敲代码的小张

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值