MVC-Controller示例
Bitgeek 2022-08-15 springspringmvc请求
# 代码包结构
cn.bitgeek.springboot.jdbc
├── rest // rest包专门放置Controller接口
├──── ClassController.java
├── service // service包专门放置Service服务类
├──── ClassService.java
├── dto // dto包放传输对象
├──── ClassDTO.java
├── entity // entity包放实体类对象
├──── ClassEntity.java
├── mapper // mapper包专门放置和数据库交互的接口类
├──── ClassMapper.java
├──── ClassMapper.xml
└── JdbcApplication.java // 服务启动类入口
# Controller代码示例
package cn.bitgeek.springboot.jdbc.rest;
import cn.bitgeek.springboot.jdbc.dto.ClassDTO;
import cn.bitgeek.springboot.jdbc.entity.Class1;
import cn.bitgeek.springboot.jdbc.service.ClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@Api(tags = "新闻信息录入系统接口")
@RequestMapping(value = "/news")
public class ClassController {
/**
* 注入ClassService类
*/
@Autowired
private ClassService classService;
/**
* <pre>创建用户</pre>
* @author bitgeek
* @date 2022/6/26 14:51
* @exception
* @return
* @param dto
*
*/
@ApiOperation(value = "创建课程/insert")
@PostMapping("/insert")
public HashMap<String, Object> create(@RequestBody ClassDTO dto){
return classService.insert(dto);
}
/**
* <pre>删除</pre>
* @author bitgeek
* @date 2022/6/26 14:54
* @exception
* @return
* @param id
*
*/
@ApiOperation(value = "删除课程/delete")
@DeleteMapping("/delete/{id}")
public HashMap<String, Object> delete(@PathVariable Integer id){
log.error("123");
return classService.delete(id);
}
/**
* <pre>修改课程</pre>
* @author bitgeek
* @date 2022/6/26 14:55
* @exception
* @return
* @param id,dto
*
*/
@ApiOperation(value = "修改课程/update")
@PutMapping("/update/{id}")
public HashMap<String, Object> update(@PathVariable String id,@RequestBody ClassDTO dto){
dto.setId(Long.parseLong(id));
return classService.update(dto);
}
/**
* <pre>根据id查询</pre>
* @author bitgeek
* @date 2022/6/26 15:02
* @exception
* @return
* @param id
*
*/
@ApiOperation(value = "查询/byid")
@PostMapping("/load/{id}")
public HashMap<String, Object> selectOne(@PathVariable Long id) {
return classService.load(id);
}
/**
* <pre>分页</pre>
* @author bitgeek
* @date 2022/6/26 15:03
* @exception
* @return
* @param start,limit,traverler
*
*/
@ApiOperation(value = "分页")
@PostMapping("/page/{start}/{limit}")
public Map<String, Object> pageList(@PathVariable int start, @PathVariable int limit, @RequestBody Class1 class1) {
return classService.pageList(start, limit, class1);
}
}