当前位置: 首页 > news >正文

4--SpringBoot项目中分类管理

 

目录

新增分类

分类分页查询

启用禁用分类

根据类型查询

修改分类


本文介绍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);}


http://www.mrgr.cn/news/33679.html

相关文章:

  • 【小bug】使用 RestTemplate 工具从 JSON 数据反序列化为 Java 对象时报类型转换异常
  • python编程,把所有子目录和文件输出到文本文件
  • C++面向对象:多态!
  • ComfyUI生成头像
  • spring揭秘22-springmvc01-概述
  • 基于51单片机的手环设计仿真
  • 数据结构与算法——Java实现 10.习题——删除有序链表重复节点
  • Java编程规范
  • Python知识点:如何使用Python进行智能合约开发(Solidity、Web3.py)
  • 跟着chatgpt一起学|多模态入门
  • Junit4测试报错:java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
  • 数字阅读步履蹒跚,阅文集团定位尴尬
  • 结合HashMap与Java 8的Function和Optional消除ifelse判断
  • idea2021git从dev分支合并到主分支master
  • Vue的指令v-model的原理
  • 反序列化- Jackson...
  • ComfyUI三个超实用插件,一定不要错过!
  • JavaEE: 创造无限连接——网络编程中的套接字
  • Python中的null是什么?
  • 梧桐数据库(WuTongDB):向量化查询优化器的技术细节介绍