JPA是Java Persistence API的简称, JCP 组织发布的 Java EE 标准之一,因此任何声称符合 JPA 标准的框架都遵循同样的架构,提供相同的访问API,这保证了基于JPA开发的企业应用能够经过少量的修改就能够在不同的JPA框架下运行。

pom引用:

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
	 

yml配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mytest
    type: com.alibaba.druid.pool.DruidDataSource
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver //驱动
  jpa:
    hibernate:
      ddl-auto: update //自动更新
    show-sql: true  //日志中显示sql语句
    #jpa.hibernate.ddl-auto是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。该参数的几种配置如下:
    #·create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
    #·create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
    #·update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
    #·validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
		
@Entity
@Getter
@Setter
public class Per {
    @Id
    @GeneratedValue
    private Long id;
    @Column(name = "name", nullable = true, length = 20)
    private String name;
}
========================================
public interface PerRepository extends JpaRepository<Person, Long> {
}
========================================
@RestController
@RequestMapping(value = "per")
public class PerController {
    @Autowired
    private PerRepository perRepository;
    @PostMapping(path = "addPer")
    public void addPer(Person per) {
        perRepository.save(per);
    }
}

完!