分析
需求分析
SQL的写法
1 在mapper中写
2 在xml中写
@Mapper
public interface CategoryMapper {
/**
* 插入数据
* @param category
*/
@AutoFill(OperationType.INSERT)
@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
*/
List<Category> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO);
controller注解的使用
@GetMapping不支持@RequestBody,所以需要使用@PostMapping 和 @RequestBody
post
@PostMapping("/page")
@ApiOperation("分类管理-分页查询")
public Result<PageResult> page(@RequestBody CategoryPageQueryDTO categoryPageQueryDTO) {
log.info("***分类管理-分页查询***参数***{}", categoryPageQueryDTO);
PageResult pageResult = categoryService.page(categoryPageQueryDTO);
return Result.success(pageResult);
}
get
有两种:
一、 @GetMapping(“/getById”) 不写{}参数名 参数用@RequestParam 即可
二、@GetMapping(“/getIn/{id}”) 写{}参数名 参数用 @PathVariable
postman -> http://ip:port/xxxxx/getInfo/1
三、不写{} 也不写@
/**
* 分类管理-通过id查询
*
* @param id
* @return
*/
@ApiOperation("分类管理-通过id查询")
@GetMapping("/getById")
public Result getById(@RequestParam Long id) {
log.info("***分类管理-通过id查询***参数:{}", id);
Category category = categoryService.getById(id);
return Result.success(category);
}
/**
* 分类管理-通过id查询
*
* @param id
* @return
*/
@ApiOperation("分类管理-通过id查询")
@GetMapping("/getById/{id}")
public Result getById(@PathVariable Long id) {
log.info("***分类管理-通过id查询***参数:{}", id);
Category category = categoryService.getById(id);
return Result.success(category);
}
@ApiOperation("分类管理-通过id查询path")
@GetMapping("/getById/{id}")
public Result getById(@PathVariable(value = "id",required = true) Long ids) {
log.info("***分类管理-通过id查询***参数:{}", ids);
Category category = categoryService.getById(ids);
return Result.success(category);
}
@ApiOperation("分类管理-通过id查询param")
@GetMapping("/getById")
public Result getById2(@RequestParam(value = "idId",required = true) Long id) {
log.info("***分类管理-通过id查询***参数:{}", id);
Category category = categoryService.getById(id);
return Result.success(category);
}
@GetMapping("/page")
// @PostMapping("/page")
@ApiOperation("分类管理-分页查询")
public Result<PageResult> page( CategoryPageQueryDTO categoryPageQueryDTO) {
log.info("***分类管理-分页查询***参数***{}", categoryPageQueryDTO);
PageResult pageResult = categoryService.page(categoryPageQueryDTO);
return Result.success(pageResult);
}
delete
@DeleteMapping("/{id}")
@ApiOperation("分类管理-删除")
public Result deleteById(@PathVariable Long id) {
log.info("***分类管理-删除***参数***{}", id);
categoryService.deleteById(id);
return Result.success();
}
put
@PutMapping("/startOrStop/{status}/{id}")
@ApiOperation("分类管理-启用禁用")
public Result startOrStop(@PathVariable Integer status, @PathVariable Long id) {
log.info("***分类管理-启用禁用***参数:{}", status);
categoryService.startOrStop(status, id);
return Result.success();
}
@PutMapping
@ApiOperation("修改分类")
public Result<String> update(@RequestBody CategoryDTO categoryDTO){
categoryService.update(categoryDTO);
return Result.success();
}
mybatisPlus的使用
@Override
public List<Category> selectByType(Long type) {
LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
categoryLambdaQueryWrapper.eq(Category::getType,type).eq(Category::getStatus,StatusConstant.ENABLE);
List<Category> categoryList= categoryMapper.selectList(categoryLambdaQueryWrapper);
return categoryList;
}
@Override
public List<Category> selectByType(Long type) {
// LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
//
// categoryLambdaQueryWrapper.eq(Category::getType,type).eq(Category::getStatus,StatusConstant.ENABLE);
//
// List<Category> categoryList= categoryMapper.selectList(categoryLambdaQueryWrapper);
HashMap<String, Object> map = new HashMap<>();
//注意,keys数据库中的列名
map.put("type", type);
map.put("status", StatusConstant.ENABLE);
List<Category> categoryList = categoryMapper.selectByMap(map);
return categoryList;
}
mybatisPlus排序
@Override
public List<Category> selectByType(Long type) {
LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
categoryLambdaQueryWrapper.eq(Category::getType, type).eq(Category::getStatus, StatusConstant.ENABLE);
categoryLambdaQueryWrapper.orderByDesc(Category::getCreateTime).orderByAsc(Category::getSort);
List<Category> categoryList = categoryMapper.selectList(categoryLambdaQueryWrapper);
// HashMap<String, Object> map = new HashMap<>();
// //注意,keys数据库中的列名
// map.put("type", type);
// map.put("status", StatusConstant.ENABLE);
// List<Category> categoryList = categoryMapper.selectByMap(map);
return categoryList;
}