文章目錄
Spring Boot常用註解
項目配置
@Value(“${變量名}”)
變量名:配置文件中的全局變量
@Value("${name}")
private String name;
@Value("${desc}")
private String desc;
yml配置文件中的配置
name: 維吉爾
desc: 你好,${name}!
@ConfigurationProperties(prefix = “對象名”)
@ConfigurationProperties(prefix = “person”)註解要和@Component一起用
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Person{");
sb.append("name='").append(name).append('\'');
sb.append(", age=").append(age);
sb.append(", sex='").append(sex).append('\'');
sb.append('}');
return sb.toString();
}
}
yml配置文件中的對象配置
person:
name: Vigilr
age: 23
sex: 男
調用方式,會用到@Autowired
和@GetMapping("/person")
@Autowired
private Person person;
@GetMapping("/person")
public String printPerson() {
return this.person.toString();
}
請求方式
代碼 | 說明 | 舉例 |
---|---|---|
@GetMapping("/url") |
get請求 | @GetMapping("/user/{id}") |
@PostMapping("/url") |
post請求 | @PostMapping("/user") |
@PutMapping("/url") |
put請求 | @PutMapping("/user/{id}") |
Controller相關注解
@Controller
@Controller
public class HelloController {
@GetMapping("/index")
public String printIndex() {
return "index";
}
}
直接用可解析HTML
@ResponseBody
與@Controller
配合使用
@Controller
public class HelloController {
@GetMapping({"/hello", "hi"})
@ResponseBody
public String sayHello() {
return desc;
}
}
@RestController
@RestController
=@Controller
+@ResponseBody
@RestController
public class HelloController {
@GetMapping({"/hello", "hi"})
public String sayHello() {
return desc;
}
}
獲取參數
@PathVariable(“參數名”)
@GetMapping("/user/{id}")
public UserEntity findId(@PathVariable("id") Integer id) {
return repository.findById(id).orElse(null);
}
@RequestParam(“參數名”)
@PostMapping("/user")
public UserEntity create(@RequestParam("age") Integer age,
@RequestParam("name") String name, @RequestParam("sex") String sex) {
UserEntity userEntity = new UserEntity();
userEntity.setAge(age);
userEntity.setName(name);
userEntity.setSex(sex);
return repository.save(userEntity);
}
數據庫相關
@Entity
表的定義
@Id
設置主鍵
@GeneratedValue
設置自增
@Entity
public class UserEntity {
@Id
@GeneratedValue
private Integer id;
private String name;
private String sex;
private Integer age;
public UserEntity() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
在IDEA中聲明@Entity後,左側會有數據庫的標誌
@Transactional
@Transactional
public void createTwo() {
UserEntity userEntity1 = new UserEntity();
userEntity1.setAge(12);
userEntity1.setName("wasd");
userEntity1.setSex("sssss");
repository.save(userEntity1);
UserEntity userEntity2 = new UserEntity();
userEntity2.setAge(1314);
userEntity2.setName("zxcvbnm");
userEntity2.setSex("ddddddd");
repository.save(userEntity2);
}
數據庫表的引擎必須爲INNODB纔可進行事務管理