package com.lkd.http.controller;
import com.lkd.entity.RegionEntity;
import com.lkd.http.vo.RegionReq;
import com.lkd.service.RegionService;
import com.lkd.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/region")
public class RegionController {
@Autowired
private RegionService regionService;
@GetMapping("/search")
Pager<RegionEntity> search(
@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
@RequestParam(value = "pageSize",required = false,defaultValue = "10") Long pageSize,
@RequestParam(value = "name",required = false,defaultValue = "") String name){
return regionService.search(pageIndex,pageSize,name);
}
@PostMapping
public boolean add(@RequestBody RegionReq regionReq){
RegionEntity region=new RegionEntity();
region.setName(regionReq.getRegionName());
region.setRemark(regionReq.getRemark());
return regionService.save(region);
}
@PutMapping("/{id}")
public boolean update(@PathVariable String id,@RequestBody RegionReq req){
RegionEntity region = regionService.getById(Long.valueOf(id));
if(region == null) return false;
region.setName(req.getRegionName());
region.setRemark(req.getRemark());
return regionService.updateById(region);
}
@GetMapping("/{regionId}")
public RegionEntity findById(@PathVariable String regionId){
return regionService.getById(Long.valueOf(regionId));
}
@DeleteMapping("/{regionId}")
public Boolean delete(@PathVariable String regionId){
return regionService.removeById(Long.valueOf(regionId));
}
}
其中模糊查询需要些Service方法,因为这个是动态Sql拼接。
@Override
public Pager<RegionEntity> search(Long pageIndex, Long pageSize, String name) {
Page<RegionEntity> page = new Page<>(pageIndex,pageSize);
LambdaQueryWrapper<RegionEntity> wrapper = new LambdaQueryWrapper<>();
if(!Strings.isNullOrEmpty(name)){
wrapper.like(RegionEntity::getName,name);
}
this.page(page,wrapper);
return Pager.build(page);
}
|