JD强子 2022-01-26 19:11 采纳率: 0%
浏览 54
已结题

写一个查询 springboot+mybatisPlus+restful风格

数据库表

img

按照categoryType进行条件查询

controller

@RestController
@RequestMapping("/product-category")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;
    
    @ApiOperation("按id查找")
    @GetMapping("find/{categoryId}")
    public ProductCategory findById(@PathVariable String categoryId){

        ProductCategory byId = productCategoryService.getById(categoryId);
        return byId;
    }

    @ApiOperation("按type查找")
    @GetMapping("/find/{categoryType}")
    public List<ProductCategory> findByType(@PathVariable List<Integer> categoryType) {

    }

数据库sql

DROP TABLE IF EXISTS `product_category`;
CREATE TABLE `product_category`  (
  `category_id` int(0) NOT NULL AUTO_INCREMENT,
  `category_name` varchar(64) CHARACTER SET utf8  COLLATE utf8_general_ci  NOT NULL COMMENT '类目名字',
  `category_type` int(0) NOT NULL COMMENT '类目编号',
  `create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '修改时间',
  PRIMARY KEY (`category_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8  COLLATE = utf8_general_ci  COMMENT = '类目表' ROW_FORMAT = Dynamic;

实体类

@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="ProductCategory对象", description="类目表")
public class ProductCategory implements Serializable {

    private static final long serialVersionUID = 1L;

      @TableId(value = "category_id", type = IdType.AUTO)
    private Integer categoryId;

    @ApiModelProperty(value = "类目名字")
    private String categoryName;

    @ApiModelProperty(value = "类目编号")
    private Integer categoryType;

    @ApiModelProperty(value = "创建时间")
    @TableField(fill = FieldFill.INSERT)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    @ApiModelProperty(value = "修改时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

}

最好能帮忙详细写下service层接口实现和controller层
感激不尽

  • 写回答

2条回答 默认 最新

  • JD强子 2022-01-27 01:41
    关注

    已解决

    使用queryWrapper.in

    @PostMapping("find/{categoryType}")
        public List<ProductCategory> findByType(@PathVariable List<Integer> categoryType){
    
            QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
    
            queryWrapper.in("category_type",categoryType);
    
            List<ProductCategory> list = productCategoryService.list(queryWrapper);
            return list;
        }
    

    注意GetMapping和PostMapping

    不得不说mybatisPlus是真的强大

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月27日
  • 创建了问题 1月26日