jpa的一些知识点

jpa是持久层框架,前身是hibernate

项目中使用:

(一)、pom.xml引入

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

(二)。application.yml文件配置

#环境配置 dev就是新建的application-dev.yml
spring:
  profiles:
    active: dev
  jpa:
    hibernate:
#      create-drop的话,每次修改字段,就会把表清空
      ddl-auto: update

(三)、创建实体类

@Entity
public class Banner {
    // 这是表的主键
    @Id
    private String id;
    private String name;
    private String description;
    private String img;
    private String title;
}

运行项目,就可以在数据库创建实体类对应的表

备注:

有时候会遇到和这个错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path 
resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed;
nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.zb.missyou.model.Banner

这是因为没有在实体类中指定主键,就是Banner里的@Id

猜你喜欢

转载自www.cnblogs.com/zhaobao1830/p/12703764.html
今日推荐