04-分类管理页面的开发

1.公共字段的填充
  • 问题分析

每次都要修改一些公共字段,更新时间等等

解决方法:使用mybatisplus在某一个地方对这些公共字段进行统一的处理。
在插入或者更新数据时对这些公共的字段进行统一的处理。
  • 方法一:创建一个处理公共字段的类(通过注入Httpsession的方法实现)
package com.wcj.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/*
 * 自定义元数据对象处理器
 * */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    //这里注入HttpSession是为了获取用户id
    @Autowired
    @Qualifier("httpSession")
    private HttpSession httpSession;

    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        //无法直接获取到session,就无法获取到当前用户的id
        metaObject.setValue("createUser",httpSession.getAttribute("employee"));
        metaObject.setValue("updateUser",httpSession.getAttribute("employee"));
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",httpSession.getAttribute("employee"));
    }
}

  • 方法二:使用TreadLocal来存储用户的id
因为一次请求对应一个线程。TreadLocal是线程的一个变量,可以使用它保存一些数据,具有线程的隔离效果,只有在线程内才能获取到其中的值。
package com.wcj.reggie.common;

/*
 * 基于TreadLocal封装的工具类,用于保存当前的用户信息
 * */
public class BaseContext {
    private static final ThreadLocal<Long> threadLocal = new ThreadLocal<>();

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

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

  • 在过滤器中将当前登录用户的id添加到ThreadLocal中
//在过滤器中
//4.判断是否完成登录
        if(request.getSession().getAttribute("employee")!=null){
            //用户已经登录,将当前用户的id装入ThreadLocal
            Long empId=(Long)request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(empId);

            filterChain.doFilter(request, response);
            return;
        }
  • 设置公共字段的填充内容
package com.wcj.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/*
 * 自定义元数据对象处理器
 * */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        //无法直接获取到session,就无法获取到当前用户的id
        metaObject.setValue("createUser",BaseContext.getCurrentId());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getCurrentId());
    }
}

  • 在属性上加上注解
@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.新增分类
 @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增分类成功!");
    }
3.分类信息的分页查询

与03中分页查询类似

4.分类信息的编辑和删除
   //删除菜品分类
    @DeleteMapping
    public R<String> delete(String ids){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //传入的ids虽然String类型,但MySQL在执行时会将String转为数值型
        queryWrapper.eq(Category::getId,ids);
        categoryService.remove(queryWrapper);
        return R.success("删除成功!");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值