springboot常用注解的使用

Controller:

@Controller:处理http请求

@RestController :返回json格式的数据。相当于@Controller+@ResponseBody

@RequestMapping:配置URL映射


@PathVariable:获取URL中的数据

//简洁请求方式:127.0.0.1:8080/hello/say/111

@RestController
@RequestMapping("/hello")

public class TestController{

    @RequestMapping(value="/say/{id}",method = RequestMethod.GET)
    public String say(@PathVariable("id") Integer myId){
        return "id:" + myId ;
    }
}

@RequestParam:获取请求参数的值

//传统请求方式:127.0.0.1:8080/hello/say?id=111

@RestController:
@RequestMapping("/hello")

public class TestController{

    @RequestMapping(value="/say",method = RequestMethod.GET)
   // @GetMapping(value="/say")
   // public String say(@RequestParam(value="id") Integer myId){
    public String say(@RequestParam(value="id",required=false,defaultValue="0") Integer myId){   //required =false 标示非必传参数
        return "id:" + myId ;
    }
}

@GetMapping:查询

@PostMapping 添加

@PutMapping 更新

@DeleteMapping 删除


Dao

//实体类可以自动生成表

spring:
    profiles:
        active: dev
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/xxx
        username: root
        password: 123456
    jpa:
        hibernate:
            ddl-auto: create    //注意这个create每次启动都会删除表后新建表。如果是update也会新建表,原来有相同的表里面有数据,会保留数据创建表。create-drop表示应用停下来的时候会自动删除表。none什么都不做。validate 会验证类的属性和表的字段是否一致,不一致会报错
        show-sql: true

@Entity 实体类映射表

@Id

@GeneratedValue            与@Id一起加到id属性上,id自增

猜你喜欢

转载自blog.csdn.net/fudayy/article/details/82916250