目录
新增分类
分类分页查询
启用禁用分类
根据类型查询
修改分类
本文介绍SpringBoot项目中的分类管理,操作类似员工管理模块,具体详解可见以下博客,此处给出各部分代码
2--SpringBoot项目中员工管理 详解(一)-CSDN博客
3--SpringBoot项目中员工管理 详解(二)-CSDN博客
新增分类
package com.sky.controller.admin;import com.sky.dto.CategoryDTO;
import com.sky.properties.JwtProperties;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {@Autowiredprivate CategoryService categoryService;//新增分类@PostMapping@ApiOperation("新增分类")public Result save(@RequestBody CategoryDTO categoryDTO){log.info("新增分类:{}",categoryDTO);categoryService.save(categoryDTO);return Result.success();}}
package com.sky.service;import com.sky.dto.CategoryDTO;public interface CategoryService {//新增分类void save(CategoryDTO categoryDTO) ;
}
package com.sky.service.impl;import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;@Service
public class CategoryServiceImpl implements CategoryService {//新增分类@Autowiredprivate CategoryMapper categoryMapper;@Overridepublic void save(CategoryDTO categoryDTO) {Category category = new Category();BeanUtils.copyProperties(categoryDTO,category);// BeanUtils.copyProperties(categoryDTO,category);category.setStatus(StatusConstant.ENABLE);category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());category.setCreateUser(BaseContext.getCurrentId());category.setUpdateUser(BaseContext.getCurrentId());categoryMapper.insert(category);}
}
package com.sky.mapper;import com.sky.entity.Category;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface CategoryMapper {@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);
}
分类分页查询
//分类分页查询@GetMapping("/page")@ApiOperation("分类分页查询")public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){log.info("分类分页查询,参数为:{}",categoryPageQueryDTO);PageResult pageResult=categoryService.pageQuery(categoryPageQueryDTO);return Result.success(pageResult);}
//分类分页查询PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
@Overridepublic PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());Page<Category> page=categoryMapper.pageQuery(categoryPageQueryDTO);long total = page.getTotal();List<Category> records = page.getResult();return new PageResult(total,records);}
//分类分页查询Page<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
<?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 create_time desc</select>
</mapper>
启用禁用分类
//启用禁用分类@PostMapping("/status/{status}")@ApiOperation("启用禁用分类")public Result startOrStop(@PathVariable Integer status,Long id){log.info("启用禁用分类:{},{}",status,id);categoryService.startOrStop(status,id);return Result.success();}
//启用禁用分类void startOrStop(Integer status, Long id);
//启用禁用分类@Overridepublic void startOrStop(Integer status, Long id) {Category category = Category.builder().status(status).id(id).build();categoryMapper.update(category);}
//启用禁用分类void update(Category category);
<!--启用禁用分类--><update id="update" parameterType="category">update category<set><if test="name!=null">name=#{name},</if><if test="type!=null">type=#{type},</if><if test="sort!=null">sort=#{sort},</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>
根据类型查询
//根据类型查询@GetMapping("/list")@ApiOperation("根据类型查询")public Result<Category> getByType(Integer type){log.info("根据类型查询:{}",type);Category category=categoryService.getByType(type);return Result.success(category);}
//根据类型查询Category getByType(Integer type);
//根据类型查询@Overridepublic Category getByType(Integer type) {Category category=categoryMapper.getByType(type);return category;}
//根据类型查询@Select("select *from category where type=#{type}")Category getByType(Integer type);
修改分类
//修改分类@PutMapping@ApiOperation("修改分类")public Result update(@RequestBody CategoryDTO categoryDTO){log.info("修改分类:{}",categoryDTO);categoryService.update(categoryDTO);return Result.success();}
//修改分类void update(CategoryDTO categoryDTO);
//修改分类@Overridepublic void update(CategoryDTO categoryDTO) {Category category = new Category();BeanUtils.copyProperties(categoryDTO,category);category.setUpdateTime(LocalDateTime.now());category.setUpdateUser(BaseContext.getCurrentId());categoryMapper.update(category);}