Spring-Data-Jpa

Spring-Data-Jpa

JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有hibernate、TopLink等

JPA

application.yml:

spring:
  profiles:
    active: prod
  datasource:
    driver-class-name: com.mysql.jdbc.driver
    url: jdbc:mysql://127.0.0.1:3306/dbgirl
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true

注意:ddl-auto中的属性:

create:在每次程序启动的时候都会重新创建一次表,之前的数据会被清空
update:第一次运行时会创建对应的表结构,如果里面有数据,会保留
create-drop:应用停止时,会把表删除
none:什么都不做
validate:验证类中的属性与表结构是否一致,不一致则报错

新建一个类Girl:

package com.hcx;

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

/**
 * Created by HCX on 2017/8/15.
 */
@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 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;
    }
}

例:

application.yml:

spring:
  profiles:
    active: prod
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/dbgirl
    username: root
    password: 123456
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true

pom.xml:



    4.0.0

    
    com.hcx
    girl
    0.0.1-SNAPSHOT
    jar

    girl
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    


    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            mysql
            mysql-connector-java
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



GirlRepository:

package com.hcx;

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

import java.util.List;

/**
 * Created by HCX on 2017/8/15.
 */
public interface GirlRepository extends JpaRepository {
    /**JpaRepository<>
     * 第一个泛型:类名
     * 第二个泛型:id的类型
     */

    //通过年龄来查询
    public List findByAge(Integer age);


}

GirlController:

package com.hcx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by HCX on 2017/8/15.
 */
@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    /**
     * 查询所有女生
     * @return
     */
    @GetMapping(value = "/girls")
    public List girlList(){
        return girlRepository.findAll();
    }

    /**
     * 添加一个女生
     * @param cupSize
     * @param age
     * @return
     */
    @PostMapping(value="/girls")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
                          @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    /**
     * 查询一个女生
     * @param id
     * @return
     */
    @GetMapping(value = "/girls/{id}") //访问:127.0.0.1:8080/girls/2
    public Girl girlFindOne(@PathVariable("id") Integer id){
        return girlRepository.findOne(id);
    }

    /**
     * 通过年龄查询女生列表
     * @param age
     * @return
     */
    @GetMapping(value = "/girls/age/{age}")
    public List girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     * 更新
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PutMapping(value = "girls/{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);//根据数据库字段的值,如果id设置为主键,则根据主键来更新

    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value = "/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

}

你可能感兴趣的:(spring)