1、导入相关Jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、编写application.properties配置文件
#通用数据源配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://118.31.170.247:3306/test?charset=utf8mb4&useSSL=false
spring.datasource.username=root
spring.datasource.password=
# Hikari 数据源专用配置
spring.datasource.hikari.maximum-pool-size=20
spring.datasource.hikari.minimum-idle=5
# JPA 相关配置
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
3、编写实体类
DaySeason.java
@Table(name = "day_season")
@Entity
public class DaySeason implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id ;
@NotNull
@Column(name = "uuid", nullable = false)
private String uuid;
@OneToMany(mappedBy = "daySeason")
private List<DaySeasonMember> daySeasonMember ;
}
DaySeasonMember.java
@Table(name = "day_season_member")
@Entity
public class DaySeasonMember implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id ;
@NotNull
@Column(name = "uuid", nullable = false)
private String uuid;
@ManyToOne(targetEntity = DaySeason.class)
@JoinColumn(name="day_season_id",referencedColumnName ="id")
private DaySeason daySeason;
}
4、编写接口
DaySeasonDao.java
@Repository
public interface DaySeasonDao extends JpaRepository<DaySeason,Long> {
}
DaySeasonMemberDao.java
@Repository
public interface DaySeasonMemberDao extends JpaRepository<DaySeasonMember,Long> {
}
5、测试
@Autowired
private DaySeasonMemberDao daySeasonMemberDao ;
@Autowired
private DaySeasonDao daySeasonDao ;
@Test
public void test1() throws IOException, JRException {
List<DaySeasonMember> all = daySeasonMemberDao.findAll();
List<DaySeason> all1 = daySeasonDao.findAll();
all.forEach((daySeasonMember)->{
System.out.println(daySeasonMember);
});
}