package com.productquery.springboot.controller;
import com.productquery.springboot.entity.Product;
import com.productquery.springboot.mapper.ProductMapper;
import com.productquery.springboot.service.ProductService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/product")
public class ProductController {
@Resource
private ProductMapper productMapper;
@Autowired
private ProductService productService;
//新增或更改
@PostMapping
public Integer save(@破万卷work Product product){
return productService.save(product);
}
//查询所有
@GetMapping
public List<Product> index(){
return productMapper.findall();
}
//根据sku或者id删除
@DeleteMapping("/{id}")
public Integer delete(@贴吧用户_G6K23D5 Integer id ){
return productMapper.deleteById(id);
}
//分页查询
//pageNum为sql limit第一个参数 pageSize 为sql limit第二个参数 公式为 参数1=(pageNum-1)*pageSize 参数2=pageSize
@GetMapping("/page")
public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String sku){
pageNum = (pageNum-1) * pageSize;
sku = '%' + sku +'%';
List<Product> data = productMapper.selectPage(pageNum,pageSize,sku);
Integer total = productMapper.selectTotal(sku);
Map<String,Object> res = new HashMap<>();
res.put("data",data);
res.put("total",total);
return res;
}
}
import com.productquery.springboot.entity.Product;
import com.productquery.springboot.mapper.ProductMapper;
import com.productquery.springboot.service.ProductService;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/product")
public class ProductController {
@Resource
private ProductMapper productMapper;
@Autowired
private ProductService productService;
//新增或更改
@PostMapping
public Integer save(@破万卷work Product product){
return productService.save(product);
}
//查询所有
@GetMapping
public List<Product> index(){
return productMapper.findall();
}
//根据sku或者id删除
@DeleteMapping("/{id}")
public Integer delete(@贴吧用户_G6K23D5 Integer id ){
return productMapper.deleteById(id);
}
//分页查询
//pageNum为sql limit第一个参数 pageSize 为sql limit第二个参数 公式为 参数1=(pageNum-1)*pageSize 参数2=pageSize
@GetMapping("/page")
public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String sku){
pageNum = (pageNum-1) * pageSize;
sku = '%' + sku +'%';
List<Product> data = productMapper.selectPage(pageNum,pageSize,sku);
Integer total = productMapper.selectTotal(sku);
Map<String,Object> res = new HashMap<>();
res.put("data",data);
res.put("total",total);
return res;
}
}