1. 程式人生 > >springboot 入門-框架解析(二)

springboot 入門-框架解析(二)

一、springboot框架中,專案配置檔案

spring:
  application:
    name: springboot
  datasource:
    driver-class-name: oracle.jdbc.driver.OracleDriver
    url: jdbc:oracle:thin:@ip:1521:orcl
    username: i
    password: p
    
server:
  port: 8001
  context-path: /springboot
mybatis:
  mapperLocations: classpath*:mapper/*.xml
 
  redis:
    host: 127.0.0.1
    port: 6379
    password:
    timeout: 10000
    pool:
      max-idle: 20
      min-idle: 5
      max-active: 20
      max-wait: 2

採用yml格式編寫專案配置,資料庫、mybatis、redis等都採用配置進行。

二、controller 層程式碼解析

@RestController
@RequestMapping(value="/users")     // 通過這裡配置使下面的對映都在/users下,可去除
public class UserController {


@Autowired
private WarnUserServiceImpl warnUserServiceImpl;

   @ApiOperation(value="獲取提醒人員列表", notes="")
   @RequestMapping(value={""}, method=RequestMethod.GET)
   public Map getUserList() {
       List<WarnUser> list = warnUserServiceImpl.infoList();
       Map map = new HashMap();
       map.put("userList",list);
       return map ;
   }
   @ApiOperation(value="建立使用者", notes="根據User物件建立使用者")
   @ApiImplicitParam(name = "user", value = "使用者詳細實體user", required = true, dataType = "User")
   @RequestMapping(value="/a", method=RequestMethod.POST)
   public Map<String, User> postUser(@RequestBody User user) {
       return null;
   }
   @ApiOperation(value="獲取使用者詳細資訊", notes="根據url的id來獲取使用者詳細資訊")
   @ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long")
   @RequestMapping(value="/1", method=RequestMethod.GET)
   public Map<String, User> getUser(@PathVariable Long id) {
       return null;
   }
   
   @ApiOperation(value="更新使用者詳細資訊", notes="根據url的id來指定更新物件,並根據傳過來的user資訊來更新使用者詳細資訊")
   @ApiImplicitParams({
           @ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long"),
           @ApiImplicitParam(name = "user", value = "使用者詳細實體user", required = true, dataType = "User")
   })
   @RequestMapping(value="/2", method=RequestMethod.PUT)
   public String putUser(@PathVariable Long id, @RequestBody User user) {
       return "success";
   }


   @ApiOperation(value="刪除使用者", notes="根據url的id來指定刪除物件")
   @ApiImplicitParam(name = "id", value = "使用者ID", required = true, dataType = "Long")
   @RequestMapping(value="/3", method=RequestMethod.DELETE)
   public String deleteUser(@PathVariable Long id) {
       return "success";
   }


}

@RequestMapping 處理請求地址對映。
method - 指定請求的方法型別:POST/GET/DELETE/PUT 等 value - 指定實際的請求地址 consumes - 指定處理請求的提交內容型別,例如 Content-Type 頭部設定application/json, text/html
produces - 指定返回的內容型別
@PathVariable URL 對映時,用於繫結請求引數到方法引數 @RequestBody 這裡註解用於讀取請求體 boy 的資料,通過 HttpMessageConverter 解析繫結到物件中