SpringBoot + Vue 前后端分离项目 微人事(九)

职位管理后端接口设计

在controller包里面新建system包,再在system包里面新建basic包,再在basic包里面创建PositionController类,在定义PositionController类的接口的时候,一定要与数据库的menu中的url地址到一致,不然会出现没有权限访问的问题

PositionController

@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {
    
    

    @Autowired
    PositionsService positionsService;
    @GetMapping("/")
    public List<Position> getAllPositions(){
    
    

        return positionsService.getAllPositions();
    }
 }

PositionService类

@Autowired
PositionsService positionsService;
@GetMapping("/")
public List<Position> getAllPositions(){
    
    

    return positionsService.getAllPositions();

}

PositionMapper接口

在这里插入图片描述

    List<Position> getAllPositions();

PositionMapper.xml

  <select id="getAllPositions" resultMap="BaseResultMap">
    select  * from position;
  </select>

打开Postman测试查询所有的position,效果如下图:
在这里插入图片描述
再把position的增删改三个接口也给写一下

PositionController


@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {
    
    

    @Autowired
    PositionsService positionsService;
    @GetMapping("/")
    public List<Position> getAllPositions(){
    
    

        return positionsService.getAllPositions();

    }

    @PostMapping("/")
    public RespBean addPosition(@RequestBody Position position) {
    
    

        if (positionsService.addPosition(position) == 1) {
    
    
           return RespBean.ok("添加成功!");
        }
            return RespBean.err("添加失败!");
    }


    @PutMapping("/")
    public RespBean updatePositions(@RequestBody Position position){
    
    

        if(positionsService.updatePositions(position)==1){
    
    
            return RespBean.ok("更新成功");
        }
        return RespBean.err("更新失败");
    }



    @DeleteMapping("/{id}")
    public RespBean deletePositionById(@PathVariable Integer id){
    
    
        if(positionsService.deletePositionById(id)==1){
    
    
            return RespBean.ok("删除成功");
        }
        return RespBean.err("删除失败");
    }

}

PositionService

@Service
public class PositionsService {
    
    
    @Autowired
    PositionMapper positionMapper;
    public List<Position> getAllPositions() {
    
    

        return positionMapper.getAllPositions();
    }

    public Integer addPosition(Position position) {
    
    
        position.setEnabled(true);
        position.setCreatedate(new Date());
        return positionMapper.insert(position) ;
    }

    public int updatePositions(Position position) {
    
    

        return positionMapper.updateByPrimaryKeySelective(position);
    }


    public int deletePositionById(Integer id) {
    
    

        return positionMapper.deleteByPrimaryKey(id);
    }
}

PositionMapper接口和PositionMapper.xml和前面那个是一样的,测试的添加效果如下图所示:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45709416/article/details/132351829