我工作的一个编程项目,我必须创建服务器应用程序提供通过REST API的前端独立客户端的数据模型(MySQL的)。我已经成功地初始化春季使用Web,MySQL和RestRepositories和JPA的依赖,连接到我的数据库。我创建了“球员”,并与一些属性的“团队”表,通过创建球员和团队的类和接口都与@RepositoryRestResource。不使用任何控制器。 在IntelliJ IDEA的工作2018年3月4日(终极版)
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-restapi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-restapi</name>
<description>SpringBoot REST API project for Programming Laboratories</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
# DataSource properties
spring.datasource.url = jdbc:mysql://localhost:3306/mysql1?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username = hello
spring.datasource.password = hello3
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
# Hibernate properties (create; MySQL8.x)
spring.jpa.hibernate.ddl-auto=create-drop
spring.jooq.sql-dialect=org.hibernate.dialect.MySQL8Dialect
Player.class:
package com.example.springbootrestapi.domain;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
@Table(name="player")
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int id;
@NotNull
@Column
private String nick;
@NotNull
@Column
private int matches;
@NotNull
@Column
private int wins;
@NotNull
@Column
private int kills;
public Player(@NotNull String nick, @NotNull int matches, @NotNull int wins, @NotNull int kills, Team team) {
this.nick = nick;
this.matches = matches;
this.wins = wins;
this.kills = kills;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public int getMatches() {
return matches;
}
public void setMatches(int matches) {
this.matches = matches;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getKills() {
return kills;
}
public void setKills(int kills) {
this.kills = kills;
}
}
Team.class:
package com.example.springbootrestapi.domain;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Set;
@Entity
@Table(name="team")
public class Team {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column
private int teamId;
@NotNull
@Column(name = "teamName")
private String teamName;
@NotNull
@Column
private int noTeamMembers;
@NotNull
@Column
private int teamMatches;
@NotNull
@Column
private int teamWins;
@NotNull
@Column
private int teamPlacement;
public Team() { }
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public int getNoTeamMembers() {
return noTeamMembers;
}
public void setNoTeamMembers(int noTeamMembers) {
this.noTeamMembers = noTeamMembers;
}
public int getTeamMatches() {
return teamMatches;
}
public void setTeamMatches(int teamMatches) {
this.teamMatches = teamMatches;
}
public int getTeamWins() {
return teamWins;
}
public void setTeamWins(int teamWins) {
this.teamWins = teamWins;
}
public int getTeamPlacement() {
return teamPlacement;
}
public void setTeamPlacement(int teamPlacement) {
this.teamPlacement = teamPlacement;
}
}
我想创建一个一到球队和球员之间有很多的映射,当我提出这意味着一个的 PUT本地主机:8080 /播放/ 1 的使用JSON车身请求我把价值的队名的(不,我Players.class尚未存在的字符串),它会链接到这个的团队名称的团队表(noTeamMembers将是由1递增)
还!
如何确保Player中的匹配不能大于Team中的 teamMatches ,与 wins 相同?
对不起,如果我说错了,希望一切都清楚了。我是Spring的初学者。