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";
   }


}

@@RestController:Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。

@RequestMapping 处理请求地址映射。
method - 指定请求的方法类型:POST/GET/DELETE/PUT 等
value - 指定实际的请求地址
consumes - 指定处理请求的提交内容类型,例如 Content-Type 头部设置application/json, text/html
produces - 指定返回的内容类型
@PathVariable URL 映射时,用于绑定请求参数到方法参数
@RequestBody 这里注解用于读取请求体 boy 的数据,通过 HttpMessageConverter 解析绑定到对象中


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值