Spring Boot笔记——3

    xiaoxiao2023-08-02  133

    数据库操作 Spring-Data-Jpa —— MySQL JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等。 RESTful API设计

    GET         /list                    获取女生列表

    POST       /add                  创建一个女生

    PUT         /update/{id}      更新

    DELETE    /delete/{id}       删除

    ————————————————————————

    maven添加jpa、mysql组件

    ————————————————————————

    mysql、jpa配置

    jpa:

        ddl-auto: create/update...

    —————————————————————————

    @Entity @Id @GeneratedValue

    package com.wuti; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * Created by wtrover on 2017/6/16. */ @Entity public class Girl { @Id @GeneratedValue private Integer id; private String cupSize; private Integer age; public Girl() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } }

    表:girl

    —————————————————————————————————

    @Repository

    package com.wuti; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; /** * Created by wtrover on 2017/6/16. */ @Repository public interface GirlRepository extends JpaRepository<Girl,Integer> { //通过年龄查询 public List<Girl> findByAge(Integer age); }

    —————————————————————————————————

    Controller

    package com.wuti; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; /** * Created by wtrover on 2017/6/16. */ @RestController @RequestMapping(value = "/girl") public class GirlController { @Autowired private GirlRepository girlRepository; @Autowired private GirlService girlService; /** * 查询所有女生列表 * @return */ @GetMapping(value = "/list") public List<Girl> girlList() { return girlRepository.findAll(); } /** * 添加一个女生 * @param cupSize * @param age * @return */ @PostMapping(value = "/add") public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam(value = "age", required = false, defaultValue = "0") Integer age) { Girl girl = new Girl(); girl.setCupSize(cupSize); girl.setAge(age); return girlRepository.save(girl); } //查询一个女生 @GetMapping(value = "/find/{id}") public Girl firlFindOne(@PathVariable("id") Integer id) { return girlRepository.findOne(id); } //更新 @PutMapping(value = "/update/{id}") public Girl girlUpdate(@PathVariable("id") Integer id, @RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age) { Girl girl = new Girl(); girl.setId(id); girl.setCupSize(cupSize); girl.setAge(age); return girlRepository.save(girl); } //删除 @DeleteMapping(value = "/delete/{id}") public void girlDelete(@PathVariable("id") Integer id) { girlRepository.delete(id); } //通过年龄查询女生列表 @GetMapping(value = "/age/{age}") public List<Girl> girlListByAge(@PathVariable("age") Integer age) { return girlRepository.findByAge(age); } //添加两个女生 @PostMapping(value = "/two") public void girlTwo() { girlService.insertTwo(); } }

    ——————————————————————————

    事务管理

    @Service @Autowired

    @Transactional

    package com.wuti; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * Created by wtrover on 2017/6/16. */ @Service public class GirlService { @Autowired private GirlRepository girlRepository; @Transactional public void insertTwo() { Girl girlA = new Girl(); girlA.setCupSize("A"); girlA.setAge(18); girlRepository.save(girlA); Girl girlB = new Girl(); girlB.setCupSize("B"); girlB.setAge(19); girlRepository.save(girlB); }; }

    相关资源:敏捷开发V1.0.pptx
    最新回复(0)