Spring Boot简明教程--Spring Data Jpa

2021-01-25  本文已影响0人  听城

主流ORM框架简介

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

Spring Data JPA

添加依赖

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <scope>runtime</scope>
</dependency>

单向一对多

@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<BannerItem> 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<BannerItem> 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;

}

双向一对多

@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<BannerItem> 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<Spu> 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<Theme> 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<Spu> 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<Theme> themeList;
}
上一篇下一篇

猜你喜欢

热点阅读