本文共 9068 字,大约阅读时间需要 30 分钟。
我们通过Spring Boot搭建Web项目的后端服务
等等
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.2.RELEASE com.imooc girl 0.0.1-SNAPSHOT girl Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java org.springframework.boot spring-boot-maven-plugin
server: port: 8080cupSize: Bage: 18content: "cuoSize: ${cupSize}, age: ${age}"girl: cupSize: B age: 18
server: port: 8081cupSize: Bage: 18content: "cuoSize: ${cupSize}, age: ${age}"girl: cupSize: F age: 18
spring: profiles: active: dev datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/girl username: root password: root jpa: hibernate: ddl-auto: update show-sql: true
package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.*;/** * @Author: TEAM-AG * @Description: 与前端连接* @Date: Created in 21:55 2020-08-10 * @Modified By: */@RestController@RequestMapping("/hello")public class HelloController { // @Value("${cupSize}")// private String cupSize;// @Value("${age}")// private Integer age;// @Value("${content}")// private String content; @Autowired private GirlProperties girlProperties; @GetMapping(value = { "/say{id}"}) public String say(@PathVariable("id") Integer id){ return "TEAM-AG " + id + girlProperties.getCupSize() + girlProperties.getAge(); }// @RequestMapping(value = {"/say"}, method = RequestMethod.GET)// public String say(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id){ // 默认值// return "TEAM-AG " + id + girlProperties.getCupSize() + girlProperties.getAge();// }}
package com.imooc.girl;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.stereotype.Component;/** * @Author: TEAM-AG * @Description: 实体类* @Date: Created in 22:25 2020-08-10 * @Modified By: */@Component@ConfigurationProperties(prefix = "girl") // yml 对应的映射public class GirlProperties { private String cupSize; private Integer age; public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
package com.imooc.girl;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * @Author: TEAM-AG * @Description: 与数据库对应* @Date: Created in 19:42 2020-08-13 * @Modified By: */@Entitypublic class Girldb { @Id @GeneratedValue private Integer id; private String cupSize; private Integer age; public Girldb() { // 必须有一个无参 构造方法 } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
package com.imooc.girl;import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;/** * @Author: TEAM-AG * @Description: 接口继承 Jpa * @Date: Created in 20:01 2020-08-13 * @Modified By: */public interface GirlRepository extends JpaRepository{ // 接口本口 对数据库的操作都靠它! // 查年龄 public List findByAge(Integer age);}
package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;import java.util.Optional;/** * @Author: TEAM-AG * @Description: 与前端连接* @Date: Created in 19:54 2020-08-13 * @Modified By: */@RestControllerpublic class GirlController { @Autowired private GirlRepository girlRepository; @Autowired // 事务 private GirlService girlService;/** * * @Description: * 查询 女生列表 * @auther: TEAM-AG * @date: 20:10 2020-08-13 * @param: [] * @return: java.util.List *【important】=> 用哪个,开哪个! */// @GetMapping(value = "/girls") // @查所有// public List girldbList(){ // return girlRepository.findAll();//// }// // 查询某个女生// @GetMapping(value = "/girls") // @查某个id// public Girldb Onegirldb(@RequestParam("id") Integer id){ // return girlRepository.findById(id).orElse(null);//// }// // 查询某个女生// @GetMapping(value = "/girls") // @查某个age // 需要在 Girlrepository中加个接口// public List Onegirldb(@RequestParam("age") Integer age){ // return girlRepository.findByAge(age);//// }/** * * @Description: * 插入 * @auther: TEAM-AG * @date: 21:15 2020-08-13 * @param: [cupSize, age] * @return: java.util.List *【important】=> 用哪个,开哪个! */// @PostMapping(value = "/girls") // @插入后看所有// public List addgirl(@RequestParam("cupSize") String cupSize,// @RequestParam("age") Integer age){ // 需要参数咯//// Girldb girl = new Girldb();// girl.setCupSize(cupSize);// girl.setAge(age);// girlRepository.save(girl);//// return girlRepository.findAll();//// }// @PostMapping(value = "/girls") // @插入后看那个// public Girldb addgirl(@RequestParam("cupSize") String cupSize,// @RequestParam("age") Integer age){ // 需要参数咯//// Girldb girl = new Girldb();// girl.setCupSize(cupSize);// girl.setAge(age);//// return girlRepository.save(girl);//// } @PostMapping(value = "/girls/cooinserttwo") public void girlTwo(){ girlService.insertTwo(); }/** * * @Description: * 更新 * @auther: TEAM-AG * @date: 21:30 2020-08-13 * @param: * @return: * *///@PostMapping(value = "/girls")//public Girldb updategirl(@RequestParam("id") Integer id,// @RequestParam("cupSize") String cupSize,// @RequestParam("age") Integer age){ // 需要参数咯//// Girldb girl = new Girldb();// girl.setId(id);// girl.setCupSize(cupSize);// girl.setAge(age);//// return girlRepository.save(girl);//////}/** * * @Description: * 删除 * @auther: TEAM-AG * @date: 21:30 2020-08-13 * @param: * @return: * *///@DeleteMapping(value = "/girls")// public void girlDelete(@RequestParam("id") Integer id){ // girlRepository.deleteById(id);////}}
package com.imooc.girl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;/** * @Author: TEAM-AG * @Description: 事务管理 * 买商品与扣钱,两者都OK则OK,任何一个不通过交易都算失败 * @Date: Created in 10:29 2020-08-14 * @Modified By: */@Servicepublic class GirlService { @Autowired private GirlRepository girlRepository; @Transactional //事务 public void insertTwo(){ Girldb girlA = new Girldb(); girlA.setCupSize("A"); girlA.setAge(18); girlRepository.save(girlA); Girldb girlB = new Girldb(); girlA.setCupSize("C"); girlA.setAge(18); girlRepository.save(girlB); }}
转载地址:http://hizdf.baihongyu.com/