环境
SpringBoot + JPA + Mysql
JPA逆向生成数据表- pom.xml
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- 配置文件application.yml
spring:
jpa:
hibernate:
ddl-auto: update
datasource:
url: jdbc:mysql://127.0.0.1:3306/cms?characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: sunday
- jpa.hibernate.ddl-auto属性值:
create:表示启动的时候先drop,再create
create-drop: 也表示创建,只不过再系统关闭前执行一下drop
update: 这个操作启动的时候会去检查schema是否一致,如果不一致会做scheme更新
validate: 启动时验证现有schema与你配置的hibernate是否一致,如果不一致就抛出异常,并不做更新
- 映射实体类
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "test1")
public class Test {
@Id
private long id;
@Column(length = 16)
private String name;
private String sex;
private Integer age;
}
-
启动程序
-
运行结果
映射关系配置
单向一对多关系
- Banner.java
import lombok.Getter;
import javax.persistence.*;
import java.util.List;
@Entity
@Getter
public class Banner {
/**
* @Id 主键
* @GeneratedValue(strategy = GenerationType.IDENTITY) 自增长
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(length = 16)
private String name;
/**
* 忽略序列化
*/
@Transient
private String description;
private String img;
private String title;
/**
* @OneToMany 指定一对多关系
* FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载
* FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载
* @JoinColumn(name = "bannerId") 指定外键
*/
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "bannerId")
private List<BannerItem> items;
}
- BannerItem.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
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;
}
双向一对多关系
1、在一方(关系的被维护端)打上@OneToMany,在多方(关系维护端)打上@ManyToOne
2、需要在多方(关系维护端)上指明关联的外键@JoinColumn
3、在一方(关系的被维护端)的@OneToMany增加一个参数mappedBy,值是多方(关系维护端)中的导航属性的名字
- Banner.java
@Entity
@Getter
public class Banner {
/**
* @Id 主键
* @GeneratedValue(strategy = GenerationType.IDENTITY) 自增长
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(length = 16)
private String name;
/**
* 忽略序列化
*/
@Transient
private String description;
private String img;
private String title;
/**
* @OneToMany 指定一对多关系
* 关系被维护端
* FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载
* FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载
*/
@OneToMany(mappedBy = "banner", fetch = FetchType.EAGER)
private List<BannerItem> items;
}
- BannerItem.java
@Entity
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;
}
单向多对多关系
- Theme.class
@Entity
public class Theme {
@Id
private Long id;
private String title;
private String name;
/**
* @ManyToMany 多对多关系
* @JoinTable() 指定第三张表规范
* name = "theme_spu" 指定第三张表表名
* joinColumns = @JoinColumn(name="theme_id") 指定外键
* inverseJoinColumns = @JoinColumn(name="spu_id") 指定外键
*/
@ManyToMany
@JoinTable(name = "theme_spu",
joinColumns = @JoinColumn(name="theme_id"),
inverseJoinColumns = @JoinColumn(name="spu_id"))
private List<Spu> spuList;
}
- Spu.class
@Entity
public class Spu {
@Id
private Long id;
private String title;
private String subtitle;
}
双向多对多关系
- Theme.class
@Entity
public class Theme {
@Id
private Long id;
private String title;
private String name;
/**
* @ManyToMany 多对多关系
* @JoinTable() 指定第三张表规范
* name = "theme_spu" 指定第三张表表名
* joinColumns = @JoinColumn(name="theme_id") 指定外键
* inverseJoinColumns = @JoinColumn(name="spu_id") 指定外键
*/
@ManyToMany
@JoinTable(name = "theme_spu",
joinColumns = @JoinColumn(name="theme_id"),
inverseJoinColumns = @JoinColumn(name="spu_id"))
private List<Spu> spuList;
}
- Spu.class
@Entity
public class Spu {
@Id
private Long id;
private String title;
private String subtitle;
/**
* @ManyToMany(mappedBy = “spuList”)声明关系的被维护端
* 多对多关系被维护端
*/
@ManyToMany(mappedBy = "spuList")
private List<Theme> themeList;
}