时间:2021-07-01 10:21:17 帮助过:15人阅读
application.yml
1 spring: 2 profiles: 3 active: dev 4 datasource: 5 driver-class-name: com.mysql.jdbc.Driver 6 url: jdbc:mysql://127.0.0.1:3306/dbgirl 7 username: root 8 password: 123456 9 jpa: 10 hibernate: 11 ddl-auto: update 12 show-sql: true
application-dev.yml
1 server: 2 port: 8080 3 girl: 4 cupSize: B 5 age: 18
active: dev 这句active后面的dev代码加载的是application-dev.yml这个配置.
ddl-auto: update这句ddl-auto后面是的update代表的是更新这个表,如果的create的话,代表删掉原来的表重新建表,配置完这些之后就不需要我们自己建表
,数据库会给我们建好相应的表.
二.代码
Girl.java
1 */ 2 @Entity 3 public class Girl { 4 5 @Id 6 @GeneratedValue 7 private Integer id; 8 9 @NotBlank(message = "这个字段必传") 10 private String cupSize; 11 12 @Min(value = 18, message = "未成年少女禁止入门") 13 // @NotNull 14 // @Max() 15 // @Length() 16 private Integer age; 17 18 @NotNull(message = "金额必传") 19 private Double money; 20 21 public Girl() { 22 } 23 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 public String getCupSize() { 33 return cupSize; 34 } 35 36 public void setCupSize(String cupSize) { 37 this.cupSize = cupSize; 38 } 39 40 public Integer getAge() { 41 return age; 42 } 43 44 public void setAge(Integer age) { 45 this.age = age; 46 } 47 48 public Double getMoney() { 49 return money; 50 } 51 52 public void setMoney(Double money) { 53 this.money = money; 54 } 55 }
程序是的入口
GirlApplication.java
1 @SpringBootApplication 2 public class GirlApplication { 3 4 public static void main(String[] args) { 5 SpringApplication.run(GirlApplication.class, args); 6 } 7 }
GirlProperties.java
1 @Component 2 @ConfigurationProperties(prefix = "girl") 3 public class GirlProperties { 4 5 private String cupSize; 6 7 private Integer age; 8 9 public String getCupSize() { 10 return cupSize; 11 } 12 13 public void setCupSize(String cupSize) { 14 this.cupSize = cupSize; 15 } 16 17 public Integer getAge() { 18 return age; 19 } 20 21 public void setAge(Integer age) { 22 this.age = age; 23 } 24 }
GirlController.java
1 @RestController 2 public class GirlController { 3 4 @Autowired 5 private GirlRepository girlRepository; 6 7 @Autowired 8 private GirlService girlService; 9 10 /** 11 * 查询所有女生列表 12 * @return 13 */ 14 @GetMapping(value = "/girls") 15 public List<Girl> girlList() { 16 return girlRepository.findAll(); 17 } 18 19 /** 20 * 添加一个女生
* 21 * @return 22 */ 23 @PostMapping(value = "/girls") 24 public Girl girlAdd(@Valid Girl girl, BindingResult bindingResult) { 25 if (bindingResult.hasErrors()) { 26 System.out.println(bindingResult.getFieldError().getDefaultMessage()); 27 return null; 28 } 29 30 girl.setCupSize(girl.getCupSize()); 31 girl.setAge(girl.getAge()); 32 33 return girlRepository.save(girl); 34 } 35 36 //查询一个女生 37 @GetMapping(value = "/girls/{id}") 38 public Girl girlFindOne(@PathVariable("id") Integer id) { 39 return girlRepository.findOne(id); 40 } 41 42 //更新 43 @PutMapping(value = "/girls/{id}") 44 public Girl girlUpdate(@PathVariable("id") Integer id, 45 @RequestParam("cupSize") String cupSize, 46 @RequestParam("age") Integer age) { 47 Girl girl = new Girl(); 48 girl.setId(id); 49 girl.setCupSize(cupSize); 50 girl.setAge(age); 51 52 return girlRepository.save(girl); 53 } 54 55 //删除 56 @DeleteMapping(value = "/girls/{id}") 57 public void girlDelete(@PathVariable("id") Integer id) { 58 girlRepository.delete(id); 59 } 60 61 //通过年龄查询女生列表 62 @GetMapping(value = "/girls/age/{age}") 63 public List<Girl> girlListByAge(@PathVariable("age") Integer age) { 64 return girlRepository.findByAge(age); 65 } 66 67 @PostMapping(value = "/girls/two") 68 public void girlTwo() { 69 girlService.insertTwo(); 70 } 71 }
最上面添加女生,这个是通过传入Gril这个参数,通过点出get方法来获取对应的属性值,推荐这个写法.
GirlRepository .java
1 public interface GirlRepository extends JpaRepository<Girl, Integer> { 2 3 //通过年龄来查询 4 public List<Girl> findByAge(Integer age); 5 }
GirlService.java
1 @Service 2 public class GirlService { 3 4 @Autowired 5 private GirlRepository girlRepository; 6 7 @Transactional 8 public void insertTwo() { 9 Girl girlA = new Girl(); 10 girlA.setCupSize("A"); 11 girlA.setAge(18); 12 girlRepository.save(girlA); 13 14 15 Girl girlB = new Girl(); 16 girlB.setCupSize("BBBB"); 17 girlB.setAge(19); 18 girlRepository.save(girlB); 19 } 20 }
GirlService.java这个类是用来测试事物管理.
在这个类的某个方法上加上@Transactional这个注解说明这个方法就已经交由springboot事物管理.有事物管理和没有事物管理的区别是:
有事物管理这个方法内有一个操作失败,如这个类的插入操作,有一个插入失败,这个方法内的操作全部失败.
如果没有事物就只有失败的那一个操作失败.
一般用于同时操作多条数据,只有查询的时候不加事物.
springboot数据库操作及事物管理操作例子
标签:文件 drive 有一个 error 自己 ups name add java