Spring Boot简明教程--Spring Data Jpa

主流ORM框架简介

目前ORM框架非常多,目前流行的ORM框架有:

  • JPA(Java Persistence API,Java持久化API),是官方提出的Java持久化规范。持久化主要是将数据存储在数据库或者磁盘文件、xml文件中,JDBC就是一种持久化机制,文件IO也是一种持久化机制。
  • Hibernate,它实现了JPA规范,它对JDBC进行了轻量级的对象封装,将POJO与数据库表建立映射关系,是一个全自动的ORM框架,可以自动生成SQL语句。
  • Spring Data JPA,极大简化了JPA写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作,包括CRUD、分页、排序等。

Spring Data JPA

添加依赖

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


     mysql
     mysql-connector-java
     runtime

单向一对多

  • 一端
@Entity
@Data
public class Banner {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    private String description;
    private String img;
    private String title;


    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name="bannerId")
    private List items;
}
  • 多端
@Entity
@Data
public class BannerItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String img;
    private String keyword;
    private Short type;
    private String name;

    private Long bannerId;
}

单向多对一

  • 一端
@Entity
@Data
public class Banner {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    private String description;
    private String img;
    private String title;


//    @OneToMany(fetch = FetchType.LAZY)
//    @JoinColumn(name="bannerId")
//    private List items;
}
  • 多端
@Entity
@Data
public class BannerItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String img;
    private String keyword;
    private Short type;
    private String name;

    private Long bannerId;
    @ManyToOne
    @JoinColumn(insertable = false,updatable = false,name = "bannerId")
    private Banner banner;

}

双向一对多

  • 一端(关系被维护端,加mappedBy)
@Entity
@Data
public class Banner {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    private String description;
    private String img;
    private String title;


    @OneToMany(mappedBy="banner",fetch = FetchType.LAZY)
    private List items;
}
  • 多端(关系维护端)
@Entity
@Data
public class BannerItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String img;
    private String keyword;
    private Short type;
    private String name;

    private Long bannerId;
    @ManyToOne
    @JoinColumn(insertable = false,updatable = false,name = "bannerId")
    private Banner banner;

}

单向多对多

public class Theme{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String description;
    private String name;
    private String extend;
    private String entranceImg;
    private String internalTopImg;
    private Boolean online;
    private String titleImg;
    private String tplName;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name="theme_spu", joinColumns = @JoinColumn(name="theme_id")
        , inverseJoinColumns = @JoinColumn(name="spu_id"))
    private List spuList;
}
@Entity
@Getter
@Setter
public class Spu{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String subtitle;
    private Long categoryId;
    private Long rootCategoryId;
    private Boolean online;
    private String price;
    private Long sketchSpecId;
    private Long defaultSkuId;
    private String img;
    private String discountPrice;
    private String description;
    private String tags;
    private Boolean isTest;
//    private Object spuThemeImg;
    private String forThemeImg;

//    @ManyToMany(mappedBy = "spuList")
//    private List themeList;
}

双向多对多

public class Theme{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String description;
    private String name;
    private String extend;
    private String entranceImg;
    private String internalTopImg;
    private Boolean online;
    private String titleImg;
    private String tplName;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name="theme_spu", joinColumns = @JoinColumn(name="theme_id")
        , inverseJoinColumns = @JoinColumn(name="spu_id"))
    private List spuList;
}
@Entity
@Getter
@Setter
public class Spu{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String subtitle;
    private Long categoryId;
    private Long rootCategoryId;
    private Boolean online;
    private String price;
    private Long sketchSpecId;
    private Long defaultSkuId;
    private String img;
    private String discountPrice;
    private String description;
    private String tags;
    private Boolean isTest;
//    private Object spuThemeImg;
    private String forThemeImg;

    @ManyToMany(mappedBy = "spuList")
    private List themeList;
}

你可能感兴趣的:(Spring Boot简明教程--Spring Data Jpa)