1. 程式人生 > >SpringBoot中Controller以及Jpa操作資料庫的使用

SpringBoot中Controller以及Jpa操作資料庫的使用

常用註解

  1. @PathVariable:獲取Url中的資料
  2. @RequestParam: 獲取請求引數的值
  3. @GetMapping : 組合註解,相當於@RequestMapping(method = RequestMethod.GET)
  4. @PostMapping: 組合註解,相當於@RequestMapping(method = RequestMethod.POST)
  5. @RestController:組合註解,相當於@Controller 加 @ResponseBody

    註解示例

## Jpa ##

  • Spring-Data-Jpa定義了一系列物件持久化的標準,目前實現這一規範的產品有Hibernate,TopLink.

    pom依賴:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent
>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies
>
<!--web專案必須引入的依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--單元測試--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--Jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--mysql附件--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--spring官方模板,跳轉指定頁面--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

資料庫配置:

這裡寫圖片描述

增刪改查程式碼:
實體類 :

package com.cym;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 * Created by hasee
 * on 2017/4/15.
 */
@Entity    //表示當前類代表資料庫中的表
public class Girl {
    @Id
    @GeneratedValue   //自增
    private Integer id;

    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;
    }
}

介面:

package com.cym;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

/**
 * Created by hasee
 * on 2017/4/15.
 */
public interface GirlPreposity extends JpaRepository<Girl,Integer>{

    //通過年齡age查詢
   public List<Girl> findByAge(Integer age);
}

Controller層(邏輯實際是寫在servicec層,這裡為了簡化):

/**
 * Created by hasee
 * on 2017/4/15.
 */

@RestController
public class GirlController {
    @Autowired
    private GirlPreposity girlPreposity;
    /**
     *查詢所有列表
     */
    @GetMapping(value = "/girls")
    public List<Girl> findlist(){
        return girlPreposity.findAll();
    }
    /**
     *id查詢一條資料
     */
    @GetMapping(value = "/select")
    public Girl findid(@RequestParam(value = "id",defaultValue = "1") Integer id){

        return girlPreposity.findOne(id);
    }
    /**
     *age 查詢
     */
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> findlist1(@PathVariable("age") Integer age){
        return girlPreposity.findByAge(age);
    }
    /**
     *新增一條資料
     */
    @PostMapping (value= "/girls")
    public Girl add(@RequestParam(value = "age") Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        return girlPreposity.save(girl);
    }
    /**
     *更新
     */
    @PutMapping(value= "/girls/{id}")
    public Girl update(@PathVariable("id") Integer id,
                       @RequestParam(value = "age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);
        System.out.println(id+age);
        return girlPreposity.save(girl);
    }
    /**
     *更新
     */
    @DeleteMapping(value= "/girls/{id}")
    public void delet(@PathVariable("id") Integer id){

        girlPreposity.delete(id);
    }
}