【发布时间】:2020-01-12 18:58:53
【问题描述】:
尝试通过创建一个用于存储足球队的简单项目来学习 Java Spring。 我正在使用 JPA 将数据存储到数据库中。 目前我可以使用我的登录用户将“团队”保存到我的数据库中。 现在我希望更新团队更改或用户出错等情况的记录。
有人可以帮助我并提供最佳方法吗?
1 团队模型类
@Data
@Entity
@Table(name="User_Team")
public class Team implements Serializable {
@OneToOne
private User user;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
@NotBlank(message="Team Name is required")
private String teamName;
@NotBlank(message="Location is required")
private String location;
@NotBlank(message="Nickname required")
private String nickName;
private String yearEstablished;
public Sport sport;
private Divison divison;
}
2。团队存储库
@Repository
public interface TeamRepository extends CrudRepository<Team, Long> {
Team findAllById(Long id);
}
3。团队控制器
@Slf4j
@Controller
@RequestMapping("/team")
@SessionAttributes("Team")
public class TeamController {
private TeamRepository teamRepository;
public TeamController(TeamRepository teamRepository) {
this.teamRepository = teamRepository;
}
@Autowired
TeamRepository service;
@PostMapping
public String processOrder(@Valid Team team, Errors errors,
SessionStatus sessionStatus,
@AuthenticationPrincipal User user) {
if (errors.hasErrors()) {
return "team";
}
team.setUser(user);
service.save(team);
sessionStatus.setComplete();
return "redirect:/team";
}
@GetMapping
public String displayTeam(Model model) {
model.addAttribute("team", service.findAll());
return "/team";
}
}
4。团队 HTML
<!DOCTYPE html>
<!--suppress ALL -->
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Team</title>
<link rel="stylesheet" th:href="@{/styles.css}" />
</head>
<body>
<div class="hero-image">
<div class="hero-text">
<h1 style="font-size:50px">Team</h1>
</div>
</div>
<div class="tab">
<button class="tablinks"><a href="team">Team Info</a></button>
<button class="tablinks"><a href="player">Players Info</a></button>
<button class="tablinks"><a href="fixtures">Fixtures</a></button>
</div>
<div id="Home" class="tabcontent">
<h3>Home</h3>
</div>
<div id="About" class="tabcontent">
<h3>Team Info</h3>
</div>
<div id="Contact" class="tabcontent">
<h3>Player Info</h3>
</div>
<table>
<tr>
<th>Team name</th>
<th>Location</th>
<th>Nickname</th>
<th>Year Formed</th>
<th>Sport</th>
<th>Division</th>
</tr>
<tr th:each = "about: ${team}">
<td th:text="${about.teamName}"></td>
<td th:text="${about.location}"></td>
<td th:text="${about.nickName}"></td>
<td th:text="${about.yearEstablished}"></td>
<td th:text="${about.sport}"></td>
<td th:text="${about.divison.displayValue}"></td>
</tr>
<!--<input type="file" name="pic" accept="image/*">
<input type="submit">-->
</table>
<button class="open-button" onclick="openForm()">Add your team</button>
<div class="form-popup" id="myForm">
<form method="POST" th:action="@{/team}" class="form-container" id="aboutForm">
<label for="teamName">Team name: </label>
<input type="text" name="teamName"/><br/>
<label for="location">Location: </label>
<input type="text" name="location"/><br/>
<label for="nickName">Nickname: </label>
<input type="text" name="nickName"/><br/>
<label for="yearEstablished">Year Formed: </label>
<input type="text" name="yearEstablished"/><br/>
<label for="sport">Sport:</label>
<select name="sport">
<option th:each="colorOpt : ${T(sportsapp.sportsapp.Sport).values()}"
th:value="${colorOpt}" th:text="${colorOpt.displayName}"></option>
</select>
<label for="divison">Divison:</label>
<select name="divison">
<option th:each="colorOpt : ${T(sportsapp.sportsapp.Divison).values()}"
th:value="${colorOpt}" th:text="${colorOpt.displayValue}"></option>
</select>
<div class="mytest">
<input type="submit" value="Add"/>
</div>
<input type="button" class="btn cancel" onclick="closeForm()" value="Close"/>
</form>
</div>
<script>
function openForm() {
document.getElementById("myForm").style.display = "block";
}
function closeForm() {
document.getElementById("myForm").style.display = "none";
}
</script>
</body>
</html>
【问题讨论】:
标签: java database spring jpa crud