Spring-Data-Jpa

来源:互联网 发布:淘宝风力灭火机价格 编辑:程序博客网 时间:2024/06/03 12:27

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:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <!--项目的基本描述-->    <groupId>com.hcx</groupId>    <artifactId>girl</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>girl</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.6.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>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-jpa</artifactId>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

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<Girl,Integer> {    /**JpaRepository<>     * 第一个泛型:类名     * 第二个泛型:id的类型     */    //通过年龄来查询    public List<Girl> 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. */@RestControllerpublic class GirlController {    @Autowired    private GirlRepository girlRepository;    /**     * 查询所有女生     * @return     */    @GetMapping(value = "/girls")    public List<Girl> 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<Girl> 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);    }}