提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
一 公共字段自动填充
1.1 代码实现
employee实体类中以下属性添加注解
@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;
1.2 为了使用公共字段自动填充,将controller层中save方法和update方法注释掉。
1.3 common包下自定义元数据对象处理器
package cn.hncj.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;
/**
* Created on 2022/6/20.
* 自定义元数据对象处理器
*
* @author Hou chaof
*/
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {
//插入操作,自动填充
@Override
public void insertFill(MetaObject metaObject) {
log.info("公共字段自动填充[insert]");
log.info(metaObject.toString());
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("createUser", new Long(1));
metaObject.setValue("updateUser", new Long(1));
}
//修改操作自动填充
@Override
public void updateFill(MetaObject metaObject) {
log.info("公共字段自动填充[update]");
log.info(metaObject.toString());
metaObject.setValue("updateTime", LocalDateTime.now());
metaObject.setValue("updateUser", new Long(1));
}
}
1.4 完善公共字段自动填充id是固定值问题
ThreadLocal解读:
工具类:
package cn.hncj.reggie.common;
/**
* Created on 2022/6/21.
* 基于ThreadLocal封装工具类,用户保存和获取当前登录用户id
*
* @author Hou chaof
*/
public class BaseContext {
private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
/**
* Description:设置值
* @date:2022/6/21 10:30
* @params: [java.lang.Long]
* @return: void
*/
public static void setCurrentId(Long id) {
threadLocal.set(id);
}
/**
* Description:获取值
* @date:2022/6/21 10:31
* @params: []
* @return: java.lang.Long
*/
public static Long getCurentId() {
return threadLocal.get();
}
}
将MyMetaObjectthandler类中updateuser设置为动态
metaObject.setValue("updateUser",BaseContext.getCurentId());
二 新增分类
2.1 开发流程
package cn.hncj.reggie.mapper;
import cn.hncj.reggie.entity.Category;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* Created on 2022/6/21.
*
* @author Hou chaof
*/
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {
}
package cn.hncj.reggie.service;
import cn.hncj.reggie.entity.Category;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* Created on 2022/6/21.
*
* @author Hou chaof
*/
public interface CategoryService extends IService<Category> {
}
package cn.hncj.reggie.service.impl;
import cn.hncj.reggie.entity.Category;
import cn.hncj.reggie.mapper.CategoryMapper;
import cn.hncj.reggie.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* Created on 2022/6/21.
*
* @author Hou chaof
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
}
package cn.hncj.reggie.controller;
import cn.hncj.reggie.common.R;
import cn.hncj.reggie.entity.Category;
import cn.hncj.reggie.service.CategoryService;
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;
/**
* Created on 2022/6/21.
*
* @author Hou chaof
*/
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* Description:新增分类
* @date:2022/6/21 11:44
* @params: [cn.hncj.reggie.entity.Category]
* @return: cn.hncj.reggie.common.R<java.lang.String>
*/
@PostMapping
public R<String> save(@RequestBody Category category) {
log.info("category:{}",category);
categoryService.save(category);
return R.success("新增分类成功");
}
}
2.2 分页查询
/**
* Description: 分页查询
* @date:2022/6/22 15:08
* @params: [int, int]
* @return: cn.hncj.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
*/
@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);
}
2.3 删除分类
/**
* Description: 根据id删除分类
* @date:2022/6/22 16:43
* @params: [java.lang.Long]
* @return: cn.hncj.reggie.common.R<java.lang.String>
*/
@DeleteMapping
public R<String> delete(Long ids) {
log.info("删除分裂,id为:{}",ids);
categoryService.removeById(ids);
return R.success("分类信息删除成功");
}
删除分类功能完善
package cn.hncj.reggie.service.impl;
import cn.hncj.reggie.common.CustomException;
import cn.hncj.reggie.entity.Category;
import cn.hncj.reggie.entity.Dish;
import cn.hncj.reggie.entity.Setmeal;
import cn.hncj.reggie.mapper.CategoryMapper;
import cn.hncj.reggie.service.CategoryService;
import cn.hncj.reggie.service.DishService;
import cn.hncj.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created on 2022/6/21.
*
* @author Hou chaof
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private DishService dishService;
@Autowired
private SetmealService setmealService;
/**
* Description: 根据id删除分类,删除之前需要进行判断
*
* @date:2022/6/22 17:54
* @params: [java.lang.Long]
* @return: void
*/
@Override
public void remove(Long id) {
LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//添加查询条件,根据分类id进行查询
dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
int count1 = dishService.count(dishLambdaQueryWrapper);
//查询当前分类是否关联了菜品,如果已经关联,抛出一个业务异常
if (count1 > 0) {
//已经关联菜品,抛出一个业务异常
throw new CustomException("当前分类下关联了菜品,不能删除");
}
//查询当前分类是否关联了套餐,如果已经关联,抛出一个业务异常
LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
int count2 = setmealService.count(setmealLambdaQueryWrapper);
if (count2 > 0) {
//已经关联了套餐,抛出一个业务异常
throw new CustomException("当前分类下关联了套餐,不能删除");
}
//删除分类
super.removeById(id);
}
}