Spring Boot数据库操作(上)

一 关于Spring-Data-Jpa
Spring Boot数据库操作(上)_第1张图片
JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate、TopLink等。

二 RESTful API设计
Spring Boot数据库操作(上)_第2张图片
三 编写pom

  4.0.0
  com.imooc
  girl
  0.0.1-SNAPSHOT
 
  
    org.springframework.boot
    spring-boot-starter-parent
    1.4.1.RELEASE
  
 
  
     1.8
  
 
  
    
      org.springframework.boot
      spring-boot-starter-web
    
   
    
      org.springframework.boot
      spring-boot-starter-thymeleaf
    
    
      org.springframework.boot
      spring-boot-starter-data-jpa
    
    
      mysql
      mysql-connector-java
    
   
  
 
四 application.yml配置
spring:
  profiles:
    active: dev
  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:表示每次执行程序都会创建一个新的表,如果之前有这个表,会将这个表给删掉。
ddl-auto:update:如果没有这个表会创建新表,如果有这个表并且表中有数据,不会删除表中的数据。

五 新建实体类Girl
1 代码
package com.imooc;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Girl {
     
     @Id
     @GeneratedValue
     private Integer id;
     private String cupSize;
     private Integer age;
     public Girl() {
           super();
     }
     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;
     }
     
}
2 运行后查看数据库,发现生成新表
Spring Boot数据库操作(上)_第3张图片

你可能感兴趣的:(微服务)