SpringBoot+SSM项目实战 苍穹外卖(2)

继续上一节的内容,本节完成新增员工、员工分页查询、启用禁用员工账号、编辑员工、导入分类模块功能代码。

目录

  • 新增员工(完整流程分为以下五个部分)
    • 需求分析和设计
    • 代码开发
    • 功能测试
    • 代码完善 (ThreadLocal 线程局部变量)
    • 代码提交
  • 员工分页查询
    • 代码完善 扩展Spring MVC消息转化器 extendMessageConverters
  • 启用禁用员工账号
  • 编辑员工
  • 导入分类模块功能代码

新增员工(完整流程分为以下五个部分)

需求分析和设计

产品原型

一般在做需求分析时,往往都是对照着产品原型进行分析,因为产品原型比较直观,便于我们理解业务。

在这里插入图片描述

当填写完表单信息, 点击"保存"按钮后, 会提交该表单的数据到服务端, 在服务端中需要接受数据, 然后将数据保存至数据库中。

注意事项:账号必须是唯一的、手机号为合法的11位手机号码、身份证号为合法的18位身份证号码、密码默认为123456

接口设计

在这里插入图片描述

明确新增员工接口的请求路径、请求方式、请求参数、返回数据。

本项目约定:

  • 管理端发出的请求,统一使用/admin作为前缀。
  • 用户端发出的请求,统一使用/user作为前缀。

表设计

新增员工,其实就是将我们新增页面录入的员工数据插入到employee表。status字段已经设置了默认值1,表示状态正常。

在这里插入图片描述




代码开发

设计DTO类

前面看到前端传输过来的是json的数据,是否可以使用对应的实体类来接收呢?

在这里插入图片描述

由于上述传入参数和实体类有较大差别,所以还是自定义DTO类。sky-pojo的com.sky.dto定义EmployeeDTO

@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层

sky-server的com.sky.controller.adminEmployeeController中创建新增员工方法

@PostMapping
@ApiOperation("新增员工")
public Result save(@RequestBody EmployeeDTO employeeDTO){
    log.info("新增员工:{}",employeeDTO);
    employeeService.save(employeeDTO);
    return Result.success();
}

Service层

com.sky.server.impl.EmployeeServiceImpl,需要在EmployeeService 接口中先声明该方法,后续不再赘述。

public void save(EmployeeDTO employeeDTO) {
    Employee employee = new Employee();

    //对象属性拷贝 employeeDTO拷贝给employee 前提是公有属性名字要一致
    BeanUtils.copyProperties(employeeDTO, employee);

    //设置账号的状态,默认正常状态 1表示正常 0表示锁定
    employee.setStatus(StatusConstant.ENABLE);

    //设置密码,默认密码123456
    employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

    //设置当前记录的创建时间和修改时间
    employee.setCreateTime(LocalDateTime.now());
    employee.setUpdateTime(LocalDateTime.now());

    //设置当前记录创建人id和修改人id
    // TODO 后期需要修改为当前登录用户的id
    employee.setCreateUser(10L);//目前写个假数据,后期修改
    employee.setUpdateUser(10L);

    employeeMapper.insert(employee);
}

PS:上面的TODO注释会高亮显示,提醒程序员要修复这段代码。

Mapper层

com.sky.EmployeeMapper中声明insert方法

@Insert("insert into employee (name, username, password, phone, sex, id_number, create_time, update_time, create_user, update_user,status) " +
        "values " +
        "(#{name},#{username},#{password},#{phone},#{sex},#{idNumber},#{createTime},#{updateTime},#{createUser},#{updateUser},#{status})")
void insert(Employee employee);

在application.yml中已开启驼峰命名,故id_number和idNumber可对应。

mybatis:
  configuration:
    #开启驼峰命名
    map-underscore-to-camel-case: true



功能测试

重启服务:访问http://localhost:8080/doc.html,进入新增员工接口

在这里插入图片描述

响应码:401 报错,通过断点调试:进入到JwtTokenAdminInterceptor拦截器发现由于JWT令牌校验失败,导致EmployeeController的save方法没有被调用。

解决方法:调用员工登录接口获得一个合法的JWT令牌

使用admin用户登录获取令牌

在这里插入图片描述
添加令牌:将合法的JWT令牌添加到全局参数中,文档管理–>全局参数设置–>添加参数

在这里插入图片描述

再次进行新增测试,成功,其中,请求头部含有JWT令牌:

在这里插入图片描述

在这里插入图片描述

注意:由于开发阶段前端和后端是并行开发的,后端完成某个功能后,此时前端对应的功能可能还没有开发完成,导致无法进行前后端联调测试。所以在开发阶段,后端测试主要以接口文档测试为主。

注意:后面在测试中再有401 报错可能是JWT令牌过期了。




代码完善 (ThreadLocal 线程局部变量)

目前,程序存在的问题主要有两个:1.录入的用户名已存,抛出的异常后没有处理 2.新增员工时,创建人id和修改人id设置为固定值

问题一

username已经添加了唯一约束,不能重复。报错:

在这里插入图片描述

解决:通过全局异常处理器来处理。进入到sky-server模块,com.sky.hander包下,GlobalExceptionHandler.java添加方法

/**
 * 处理SQL异常
 * @param ex
 * @return
 */
@ExceptionHandler
public Result exceptionHandler(SQLIntegrityConstraintViolationException ex){
    //录入的用户名已存在时报错:Duplicate entry 'zhangsan' for key 'employee.idx_username'
    String message = ex.getMessage();
    if(message.contains("Duplicate entry")){
        String[] split = message.split(" ");
        String username = split[2];
        String msg = username + MessageConstant.ALREADY_EXISTS;
        return Result.error(msg);
    }else{
        return Result.error(MessageConstant.UNKNOWN_ERROR);
    }
}

进入到sky-common模块,在MessageConstant.java添加

public static final String ALREADY_EXISTS = "已存在";

再次,接口测试:

在这里插入图片描述



问题二

描述:新增员工时,创建人id和修改人id设置为固定值,应该动态的设置为当前登录的用户id。

在这里插入图片描述

解决:通过某种方式动态获取当前登录员工的id。员工登录成功后会生成JWT令牌并响应给前端,后续请求中,前端会携带JWT令牌,通过JWT令牌可以解析出当前登录员工id,但是解析出登录员工id后,如何传递给Service的save方法?

可以通过ThreadLocal进行传递。

ThreadLocal 并不是一个Thread,而是Thread的局部变量。
ThreadLocal为每个线程提供单独一份存储空间,具有线程隔离的效果,只有在线程内才能获取到对应的值,线程外则不能访问,而客户端发起的每一次请求都是一个单独的线程。常用方法:

public void set(T value) //设置当前线程的线程局部变量的值
public T get() //返回当前线程所对应的线程局部变量的值
public void remove() //移除当前线程的线程局部变量

所以解决流程如下:

在这里插入图片描述

初始工程中sky-common模块已经封装了 ThreadLocal 操作的工具类:

package com.sky.context;

public class BaseContext {

    public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

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

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

    public static void removeCurrentId() {
        threadLocal.remove();
    }

}

sky-server模块,在拦截器中解析出当前登录员工id,并放入线程局部变量中:

@Component
@Slf4j
public class JwtTokenAdminInterceptor implements HandlerInterceptor {

	...
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
			...
            Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
            log.info("当前员工id:", empId);
            /将用户id存储到ThreadLocal
            BaseContext.setCurrentId(empId);
			...
	}
}

在Service中获取线程局部变量中的值:

public void save(EmployeeDTO employeeDTO) {
    //.............................

    //设置当前记录创建人id和修改人id
    employee.setCreateUser(BaseContext.getCurrentId());
    employee.setUpdateUser(BaseContext.getCurrentId());
    employeeMapper.insert(employee);
}

测试:使用admin(id=1)用户登录后添加一条记录

在这里插入图片描述

在这里插入图片描述




代码提交

点击提交:

在这里插入图片描述

提交过程中,出现提示继续push。

在这里插入图片描述

在这里插入图片描述




员工分页查询

系统中的员工很多的时候,如果在一个页面中全部展示出来会显得比较乱,不便于查看,所以一般的系统中都会以分页的方式来展示列表数据。而在我们的分页查询页面中, 除了分页条件以外,还有一个查询条件 “员工姓名”。

在这里插入图片描述

业务规则:
根据页码展示员工信息、每页展示10条数据、分页查询时可以根据需要,输入员工姓名进行查询

请添加图片描述

请求参数类型为Query,不是json格式提交,在路径后直接拼接。/admin/employee/page?name=zhangsan。返回数据中records数组中使用Employee实体类对属性进行封装。

设计DTO类,根据请求参数进行封装,在sky-pojo模块中,EmployeePageQueryDTO

@Data
public class EmployeePageQueryDTO implements Serializable {
    //员工姓名
    private String name;
    //页码
    private int page;
    //每页显示记录数
    private int pageSize;
}

后面所有的分页查询,统一都封装为PageResult对象。在sky-common模块。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageResult implements Serializable {
    private long total; //总记录数
    private List records; //当前页数据集合
}

员工信息分页查询后端返回的对象类型为: Result

Controller层

sky-server模块中,com.sky.controller.admin.EmployeeController

@GetMapping("/page")
@ApiOperation("员工分页查询")
public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
    log.info("员工分页查询,参数为:{}", employeePageQueryDTO);
    PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);//后续定义
    return Result.success(pageResult);
}

Service层实现类

在EmployeeServiceImpl中实现pageQuery方法:

public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
   // select * from employee limit 0,10
   //开始分页查询
   PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

   Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);//后续定义

   long total = page.getTotal();
   List<Employee> records = page.getResult();

   return new PageResult(total, records);
}

注意:此处使用 mybatis 的分页插件 PageHelper 来简化分页代码的开发。底层基于 mybatis 的拦截器实现。故在pom.xml文中添加依赖(初始工程已添加)

<dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-starter</artifactId>
   <version>${pagehelper}</version>
</dependency>

Mapper层

EmployeeMapper 中声明 pageQuery

Page<Employee> pageQuery(EmployeePageQueryDTO employeePageQueryDTO);

src/main/resources/mapper/EmployeeMapper.xml 中编写动态SQL:

<select id="pageQuery" resultType="com.sky.entity.Employee">
    select * from employee
    <where>
        <if test="name != null and name != ''">
            and name like concat('%',#{name},'%')
        </if>
    </where>
    order by create_time desc
</select>

重启服务:访问http://localhost:8080/doc.html,进入员工分页查询

在这里插入图片描述

在这里插入图片描述

前后端联调测试发现,最后操作时间格式不清晰,在代码完善中解决。

在这里插入图片描述




代码完善 扩展Spring MVC消息转化器 extendMessageConverters

下面进行代码完善。

方式一

在属性上加上注解,对日期进行格式化

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Employee implements Serializable {
	...
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;
	...
}

但这种方式,需要在每个时间属性上都要加上该注解,使用较麻烦,且不能全局处理。

方式二(推荐 )

在配置类WebMvcConfiguration中扩展SpringMVC的消息转换器,消息转换器的作用就是对我们后端返回给前端的数据进行统一处理,我们这里需要统一对日期类型进行格式处理。
WebMvcConfiguration继承了父类WebMvcConfigurationSupport,这个是spring给我们提供的,web层的配置类一般都会去继承它,拓展消息转换器就需要重写父类里的extendMessageConverters方法,写的代码都是固定的。

WebMvcConfiguration

/**
 * 扩展Spring MVC框架的消息转化器
 * @param converters
 */
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {//该方法在程序启动时就会被调用到
    log.info("扩展消息转换器...");
    //创建一个消息转换器对象
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    //需要为消息转换器设置一个对象转换器,对象转换器可以将Java对象序列化为json数据
    converter.setObjectMapper(new JacksonObjectMapper());
    //设置完对象转换器还需要将自己的消息转化器加入容器中 converters存放的是我们整个spring mvc框架使用到的消息转换器
    converters.add(0,converter);//存入容器时指定索引,否则如果插入到最后默认是使用不到的,我们希望优先使用自己的消息转换器,放在第一位
}

时间格式的对象转换器JacksonObjectMapper已经提前写好,存放在sky-common模块的json包下:

/**
 * 对象映射器:基于jackson将Java对象转为json,或者将json转为Java对象
 * 将JSON解析为Java对象的过程称为 [从JSON反序列化Java对象]
 * 从Java对象生成JSON的过程称为 [序列化Java对象到JSON]
 */
public class JacksonObjectMapper extends ObjectMapper {//继承jackson包里的ObjectMapper类 是json处理的一个类
//代码写法比较固定,知道当前类起什么作用就行
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    //public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; //如果想加上秒就取消这段注释 注释下面这句
    public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public JacksonObjectMapper() {
        super();
        //收到未知属性时不报异常
        this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);

        //反序列化时,属性不存在的兼容处理
        this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        //下面可以看到其实是对我们这种比如LocalDateTime这种类型设置了反序列化器(Deserializer)和序列化器(Serializer)
        //同时也针对转换的格式进行了指定
        SimpleModule simpleModule = new SimpleModule()
                .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
                .addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
                .addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
                .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
                .addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
                .addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));

        //注册功能模块 例如,可以添加自定义序列化器和反序列化器
        this.registerModule(simpleModule);
    }
}

添加后,再次测试

在这里插入图片描述

功能完成,提交代码,和新增员工代码提交一致,不再赘述。




启用禁用员工账号

在这里插入图片描述

可以对状态为“启用” 的员工账号进行“禁用”操作,可以对状态为“禁用”的员工账号进行“启用”操作,状态为“禁用”的员工账号不能登录系统。

请添加图片描述

路径参数携带状态值。同时,把id传递过去,id的类型是Query类型,即通过地址栏传参的方式传入id,明确对哪个用户进行操作。

Controller层

sky-server模块中,EmployeeController 中创建启用禁用员工账号的方法

@PostMapping("/status/{status}")
@ApiOperation("启用禁用员工账号")
public Result startOrStop(@PathVariable Integer status,Long id){
    log.info("启用禁用员工账号:{},{}",status,id);
    employeeService.startOrStop(status,id);//后绪步骤定义
    return Result.success();
}

Service层实现类

EmployeeServiceImpl

public void startOrStop(Integer status, Long id) {
    Employee employee = Employee.builder()
            .status(status)
            .id(id)
            .build();

    employeeMapper.update(employee);
}

Mapper层

EmployeeMapper 接口中声明 update 方法

void update(Employee employee);

在 EmployeeMapper.xml 中编写动态SQL:

<update id="update" parameterType="Employee">
    update employee
    <set>
        <if test="name != null">name = #{name},</if>
        <if test="username != null">username = #{username},</if>
        <if test="password != null">password = #{password},</if>
        <if test="phone != null">phone = #{phone},</if>
        <if test="sex != null">sex = #{sex},</if>
        <if test="idNumber != null">id_Number = #{idNumber},</if>
        <if test="updateTime != null">update_Time = #{updateTime},</if>
        <if test="updateUser != null">update_User = #{updateUser},</if>
        <if test="status != null">status = #{status},</if>
    </set>
    where id = #{id}
</update>

测试:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

测试完毕,提交代码。




编辑员工

在这里插入图片描述

在这里插入图片描述

注:点击修改时,数据应该正常回显到修改页面。点击 “保存” 按钮完成编辑操作。

编辑员工功能涉及到两个接口:

根据id查询员工信息
请添加图片描述

编辑员工信息

请添加图片描述

回显员工信息功能

EmployeeController 中创建 getById:

@GetMapping("/{id}")
@ApiOperation("根据id查询员工信息")
public Result<Employee> getById(@PathVariable Long id){
    Employee employee = employeeService.getById(id);
    return Result.success(employee);
}

EmployeeServiceImpl 中实现 getById 方法:

public Employee getById(Long id) {
    Employee employee = employeeMapper.getById(id);
    employee.setPassword("****");//回显给前端展示的信息不展示密码
    return employee;
}

EmployeeMapper 接口中声明 getById 方法:

@Select("select * from employee where id = #{id}")
Employee getById(Long id);

修改员工信息功能

EmployeeController 中创建 update 方法:

@PutMapping
@ApiOperation("编辑员工信息")
public Result update(@RequestBody EmployeeDTO employeeDTO){
    log.info("编辑员工信息:{}", employeeDTO);
    employeeService.update(employeeDTO);
    return Result.success();
}

EmployeeServiceImpl 中实现 update 方法:

    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);

        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());//获取线程局部变量中的值

        employeeMapper.update(employee);
    }

在实现启用禁用员工账号功能时,已实现employeeMapper.update(employee),所以在此不需写Mapper层代码。

分别测试根据id查询员工信息和编辑员工信息两个接口:

在这里插入图片描述

在这里插入图片描述

前后端联调测试也通过,提交代码。




导入分类模块功能代码

在这里插入图片描述

后台系统中可以管理分类信息,分类包括两种类型,分别是菜品分类和套餐分类。先来分析菜品分类相关功能。

新增菜品分类菜品分类分页查询根据id删除菜品分类(要注意的是当分类关联了菜品或者套餐时,此分类不允许删除)、修改菜品分类(点击修改按钮,弹出修改窗口,在修改窗口回显分类信息)、启用禁用菜品分类分类类型查询(当点击分类类型下拉框时,从数据库中查询所有的菜品分类数据进行展示)

业务规则:分类名称必须是唯一的、分类按照类型可以分为菜品分类和套餐分类、新添加的分类状态默认为“禁用”

根据上述原型图分析,菜品分类模块共涉及如下6个接口:

请添加图片描述

请添加图片描述

请添加图片描述

请添加图片描述

请添加图片描述

请添加图片描述

category表结构:

字段名数据类型说明备注
idbigint主键自增
namevarchar(32)分类名称唯一
typeint分类类型1菜品分类 2套餐分类
sortint排序字段用于分类数据的排序
statusint状态1启用 0禁用
create_timedatetime创建时间
update_timedatetime最后修改时间
create_userbigint创建人id
update_userbigint最后修改人id

导入资料中的分类管理模块功能代码即可

在这里插入图片描述

可按照mapper–>service–>controller依次导入,这样代码不会显示相应的报错。进入到sky-server模块中:

Mapper层

DishMapper.java

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

}

SetmealMapper.java

@Mapper
public interface SetmealMapper {

    /**
     * 根据分类id查询套餐的数量
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

}

CategoryMapper.java

@Mapper
public interface CategoryMapper {

    /**
     * 插入数据
     * @param category
     */
    @Insert("insert into category(type, name, sort, status, create_time, update_time, create_user, update_user)" +
            " VALUES" +
            " (#{type}, #{name}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    void insert(Category category);

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);

    /**
     * 根据id删除分类
     * @param id
     */
    @Delete("delete from category where id = #{id}")
    void deleteById(Long id);

    /**
     * 根据id修改分类
     * @param category
     */
    void update(Category category);

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    List<Category> list(Integer type);
}

CategoryMapper.xml,进入到resources/mapper目录下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.CategoryMapper">

    <select id="pageQuery" resultType="com.sky.entity.Category">
        select * from category
        <where>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
            <if test="type != null">
                and type = #{type}
            </if>
        </where>
        order by sort asc , create_time desc
    </select>

    <update id="update" parameterType="Category">
        update category
        <set>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser}
            </if>
        </set>
        where id = #{id}
    </update>

    <select id="list" resultType="Category">
        select * from category
        where status = 1
        <if test="type != null">
            and type = #{type}
        </if>
        order by sort asc,create_time desc
    </select>
</mapper>

Service层

CategoryService.java

public interface CategoryService {

    /**
     * 新增分类
     * @param categoryDTO
     */
    void save(CategoryDTO categoryDTO);

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);

    /**
     * 根据id删除分类
     * @param id
     */
    void deleteById(Long id);

    /**
     * 修改分类
     * @param categoryDTO
     */
    void update(CategoryDTO categoryDTO);

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     */
    void startOrStop(Integer status, Long id);

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    List<Category> list(Integer type);
}

CategoryServiceImpl.java

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     * @param categoryDTO
     */
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);

        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);

        //设置创建时间、修改时间、创建人、修改人
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insert(category);
    }

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //下一条sql进行分页,自动加入limit关键字分页
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id删除分类
     * @param id
     */
    public void deleteById(Long id) {
        //查询当前分类是否关联了菜品,如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品,不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐,如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品,不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        categoryMapper.deleteById(id);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        //设置修改时间、修改人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.update(category);
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}

Controller层

CategoryController.java

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")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增分类")
    public Result<String> save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询:{}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result<String> deleteById(Long id){
        log.info("删除分类:{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result<String> update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){
        categoryService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}

导入完毕后还需要编译,确保代码无错误:

在这里插入图片描述

重启服务,功能太多,直接前后端联调测试:

在这里插入图片描述

测试通过提交代码。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/211112.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

css中元素水平居中的方式

文章目录 前言水平居中&#xff1a;垂直居中方法一: text-align: centerdisplay: table-cell方法二:父元素静态定位子元素通过相对定位来实现方法三:通过静态和相对定位方法四 css图片居中用text-align:center无效怎么回事&#xff1f;如何让图片在DIV中水平和垂直两个方向都居…

prometheus|云原生|kubernetes内部安装prometheus

架构说明&#xff1a; prometheus是云原生系统内的事实上的监控标准&#xff0c;而kubernetes集群内部自然还是需要就地取材的部署prometheus服务了 那么&#xff0c;prometheus-server部署的方式其实是非常多的&#xff0c;比如&#xff0c;kubesphere集成方式&#xff0c;h…

前端面试JS— JS数据类型及相关内容

目录 JS数据类型 基本数据类型&#xff1a; 引用数据类型&#xff1a; 两种数据存储方式&#xff1a; 两种数据类型的区别&#xff1a; 数据类型的检测方式 null和undefined区别 JS数据类型 基本数据类型&#xff1a; Number&#xff0c;String&#xff0c;Boolean&#xff0c;…

Safe and Practical GPU Computation in TrustZone论文阅读笔记

Safe and Practical GPU Computation in TrustZone 背景知识&#xff1a; youtube GR视频讲解链接&#xff1a;ASPLOS’22 - Session 2A - GPUReplay: A 50-KB GPU Stack for Client ML - YouTube GPU软件栈&#xff1a; 概念&#xff1a;"GPU软件栈"指的是与GPU硬件…

以下哪项不是在内蒙古举办的?()

需要查看更多试题和答案&#xff0c;可以前往题海舟&#xff08;题海舟&#xff09;进行搜题查看。可以搜“题干关键词”。 以下哪项不是在内蒙古举办的&#xff1f;&#xff08;&#xff09; A.中蒙博览会 B.东北亚区域合作高峰论坛 C.中蒙俄合作高层论坛 D.中日经济合作会 …

C语言之位段(详解)

C语言之位段 文章目录 C语言之位段1. 位段的介绍2. 位段的内存分配3. 位段跨平台问题4. 位段的应用5. 位段使用注意 1. 位段的介绍 位段&#xff08;bit-field&#xff09;是C语言中的一种特殊数据类型&#xff0c;它允许将一个字节分成几个部分&#xff0c;并为每个部分指定特…

Sql Server数据库跨机器完整恢复(源文件恢复)

问题描述 在操作系统异常的情况下&#xff0c;SQL Server 和相关的业务系统遭受了不可用的情况。由于操作系统问题&#xff0c;导致旧服务器无法正常运行。为了恢复业务功能并确保数据完整性&#xff0c;采取了以下步骤来在新机器上进行 SQL Server 的重新安装和数据恢复。 面…

C#网络编程(System.Net命名空间和System.Net.Sockets命名空间)

目录 一、System.Net命名空间 1.Dns类 &#xff08;1&#xff09;示例源码 &#xff08;2&#xff09;生成效果 2.IPAddress类 &#xff08;1&#xff09;示例源码 &#xff08;2&#xff09;生成效果 3.IPEndPoint类 &#xff08;1&#xff09; 示例源码 &#xff0…

如何用Java实现扑克牌(附源码)

目录 一.扑克牌的数据结构 二.买牌(扑克牌的初始化) 三.洗牌 四.发牌 五.完整代码 Card.java CardList.java 六.测试 输出结果 一.扑克牌的数据结构 首先&#xff0c;扑克牌是一幅一幅的&#xff0c;除去大小王以外一共有52张&#xff0c;我们可以考虑用数组来存储…

开源 LLM 安全扫描器

Vigil 是一款开源安全扫描程序&#xff0c;可检测即时注入、越狱以及对大型语言模型(LLM) 的其他潜在威胁。 当攻击者使用专门设计的输入成功影响 LLM 时&#xff0c;就会出现即时注入。这导致 LLM 无意中实现了攻击者设定的目标。 ​ 我对 LLM 的可能性感到非常兴奋&#xff…

隐形内嵌!触想智能发布全新B款内嵌式工控一体机及内嵌式工业显示器

近日&#xff0c;触想智能发布全新B款内嵌式工控系列TPC-19.该系列可支持显示器和一体机等多种品类、多级配置的灵活选购。标志性的2.5mm矮阶窄边面板设计&#xff0c;适配隐形内嵌式安装&#xff0c;专为机柜类设备应用打造&#xff0c;以高契合的物理结构&#xff0c;带动稳定…

YUVRGB

一、直观感受 根据上面的图片&#xff0c;不难看出&#xff1a; RGB的每个分量&#xff0c;是对当前颜色的一个亮度值Y分量对呈现出清晰的图像有着很大的贡献Cb、Cr分量的内容不太容易识别清楚YUV将亮度信息&#xff08;Y&#xff09;与色度信息&#xff08;UV&#xff09;分离…

进程的创建:fork()

引入 创建进程的方式我们已经学习了一个&#xff01;在我们运行指令(或者运行我们自己写的可执行程序)的时候不就是创建了一个进程嘛&#xff1f;那个创建进程的方式称为指令级别的创建子进程&#xff01; 那如果我们想要在代码中创建进程该怎么办呢&#xff1f; fork() for…

Python基础学习快速入门

文章目录 Number变量String字符串Def函数Class类List列表Tuple元组Dictionary字典Set集合值与引用类型if条件控制Loop循环 Number变量 python直接赋值&#xff0c;不需要定义变量类型。不需要**,逗号结尾符 使用print**直接进行输出 #赋值 a 1.0 print(a)a 7 print(a)p…

OpenShare | 前端三件套初识

文章目录 &#x1f4da;总述&#x1f4da;一个案例&#x1f407;首先搭HTML框架&#x1f407;CSS加样式&#x1f407;js交互实现&#x1f32e;编辑按钮实现&#x1f32e;我还想要换头像 &#x1f6a9;加点悬浮框交互&#x1f6a9;框架梳理 &#x1f4da;资源分享 &#x1f4da;…

go第三方包发布(短精细)

1、清除其他依赖项 $ go mod tidy # 清除不必要的依赖依赖清除完成后&#xff0c;查看go.mod文件配置是否规范 module github.com/fyupeng/rpc-go-netty go 1.19 require ( )2、本地版本创建 $ git tag v0.1.0 # 本地 创建标签3、版本提交 $ git push github v0.1.0 # 推送…

如何快速生成项目目录结构树?

经常在网上看到下面这种由一个项目&#xff0c;生成一个结构树&#xff0c;你知道它是怎么生成的吗&#xff1f; 这就是利用本文要介绍的一个工具——Treer&#xff0c;treer就是一款专门用来快速生成目录结构树的命令行工具。 第一步&#xff1a;安装treer 在终端执行全局…

优先队列详解

优先队列是计算机科学中的一种抽象数据类型&#xff0c;它是一种队列&#xff1a;元素拥有优先级&#xff0c;优先级最高的元素最先得到服务&#xff1b;优先级相同的元素按照在集合中的顺序得到服务。优先队列有两种主要的实现方法&#xff1a;堆和二叉搜索树。 简单来说&…

【LeetCode热题100】【双指针】移动零

给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数组进行操作。 示例 1: 输入: nums [0,1,0,3,12] 输出: [1,3,12,0,0] 示例 2: 输入: nums [0] 输出…

leetCode 46. 全排列 + 回溯算法 + 图解 + 笔记

46. 全排列 - 力扣&#xff08;LeetCode&#xff09; 给定一个不含重复数字的数组 nums &#xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3] 输出&#xff1a;[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],…
最新文章