小张推荐:瑞吉外卖Day02
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("修改成功");
}