[JAVA] Frühjahr gründliche Einführung Version Upgrade Memo

"Eine gründliche Einführung in den Frühling" nannte ein gutes Buch, um das Frühlings-Framework zu lernen. Ich versuche zu lernen, während ich das Tutorial implementiere. Ich habe es mir notiert, weil ich manchmal anders als in meiner eigenen Umgebung darauf gestoßen bin. Ich habe versucht, es so weit wie möglich zu beheben, aber vielleicht gibt es noch ...?

Umgebung

Buch Meine Umgebung
Spring Framework 4.2.6 5.2.3
Spring Boot 1.3.5 2.2.4
Thymeleaf 2.1.0 3.0.4
thymeleaf-extras-springsecurity4 2.1.2 -
thymeleaf-extras-springsecurity5 - 3.0.4
SQL * Dies wird aufgrund von Problemen mit der persönlichen Umgebung geändert PostgreSQL MySQL

Änderungen

Einstellungen der Eigenschaftendatei

p.643 Für MySQL geändert

application.properties


spring.jpa.database=MYSQL
spring.datasource.url=jdbc:mysql://localhost:3306/spring_tutorial
spring.datasource.username=*****
spring.datasource.password=*****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.format_sql=true
spring.datasource.sql-script-encoding=UTF-8
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
spring.datasource.separator=/;

Data.sql und schema.sql werden auch für MySQL geändert.

Einspritzwechsel

Feldinjektion auf Konstruktorinjektion geändert. Die Konstruktorinjektion ist die am meisten empfohlene der drei DI-Methoden. Die Beschreibung wird erhöht, aber das Feld kann finalisiert werden (unveränderlich).

Wenn es nur einen Konstruktor in der Klasse gibt, kann @Autowired weggelassen werden (es wird unten weggelassen).

Serviceklasse

RoomService.java


 //Vorher ändern
 @Autowired
 ReservableRoomRepository reservableRoomRepository;
	
 @Autowired
 MeetingRoomRepository meetingRoomRepository;

 //Nach der veränderung
 private final ReservableRoomRepository reservableRoomRepository;
 private final MeetingRoomRepository meetingRoomRepository;
	
 public RoomService(ReservableRoomRepository reservableRoomRepository, 
				MeetingRoomRepository meetingRoomRepository) {
	this.reservableRoomRepository = reservableRoomRepository;
	this.meetingRoomRepository = meetingRoomRepository;
 }

ReservationUserDetailsService.java


 //Vorher ändern
 @Autowired
 UserRepository userRepository;

 //Nach der veränderung
 private final UserRepository userRepository;
	
 public ReservationUserDetailsService(UserRepository userRepository) {
	this.userRepository = userRepository;
 }

ReservationService.java


 //Vorher ändern
 @Autowired
 ReservationRepository reservationRepository;

 @Autowired
 ReservableRoomRepository reservableRoomRepository;

 //Nach der veränderung
 private final ReservationRepository reservationRepository;
 private final ReservableRoomRepository reservableRoomRepository;

 public ReservationService(ReservationRepository reservationRepository, 
			ReservableRoomRepository reservableRoomRepository) {
	this.reservationRepository = reservationRepository;
	this.reservableRoomRepository = reservableRoomRepository;
 }	

Controller-Klasse

RoomsController.java


 //Vorher ändern
 @Autowired
 RoomService roomService;

 //Nach der veränderung
 private final RoomService roomService;

 public RoomsController(RoomService roomService) {
	this.roomService = roomService;
 }

ReservationsController.java


 //Vorher ändern
 @Autowired
 RoomService roomService;

 @Autowired
 ReservationService reservationService;

 //Nach der veränderung
 private final RoomService roomService;
 private final ReservationService reservationService;

 public ReservationsController(RoomService roomService, 
				ReservationService reservationService) {
	this.roomService = roomService;
	this.reservationService = reservationService;
 }

Anforderungszuordnung ändern

@RequestMapping wurde in die Annotation geändert, die jeder HTTP-Methode entspricht "path =" ist optional

Controller-Klasse

RoomsController.java


 //Vorher ändern
 @RequestMapping(method = RequestMethod.GET)
 String listRooms(Model model) {
・ ・ ・
 }

 @RequestMapping(path = "{date}", method = RequestMethod.GET)
 String listRooms(
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) 
	@PathVariable("date") LocalDate date, Model model) {
・ ・ ・
 }

 //Nach der veränderung
 @GetMapping
 String listRooms(Model model) {
・ ・ ・
 }

 @GetMapping("{date}")
 String listRooms(
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) 
	@PathVariable("date") LocalDate date, Model model) {
・ ・ ・
 }

ReservationsController.java


 //Vorher ändern
 @RequestMapping(method = RequestMethod.GET)
 String reserveForm(
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
	@PathVariable("date") LocalDate date,
	@PathVariable("roomId") Integer roomId, Model model) {
・ ・ ・
 }

 @RequestMapping(method = RequestMethod.POST)
 String reserve(@Validated ReservationForm form, BindingResult bindingResult,
	@AuthenticationPrincipal ReservationUserDetails userDetails,
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
	@PathVariable("date") LocalDate date,
	@PathVariable("roomId") Integer roomId, Model model) {
・ ・ ・
 }

 @RequestMapping(method = RequestMethod.POST, params = "cancel")
 String cancel(
	@RequestParam("reservationId") Integer reservationId,
	@PathVariable("roomId") Integer roomId,
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
	@PathVariable("date") LocalDate date, Model model) {
・ ・ ・
 }

 //Nach der veränderung
 @GetMapping
 String reserveForm(
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
	@PathVariable("date") LocalDate date,
	@PathVariable("roomId") Integer roomId, Model model) {
・ ・ ・
 }

 @PostMapping
 String reserve(@Validated ReservationForm form, BindingResult bindingResult,
	@AuthenticationPrincipal ReservationUserDetails userDetails,
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
	@PathVariable("date") LocalDate date,
	@PathVariable("roomId") Integer roomId, Model model) {
・ ・ ・
 }

 @PostMapping(params = "cancel")
 String cancel(
	@RequestParam("reservationId") Integer reservationId,
	@PathVariable("roomId") Integer roomId,
	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
	@PathVariable("date") LocalDate date, Model model) {
・ ・ ・
 }

Wechseln Sie von Thymeleaf2 zu Thymeleaf3

Gemäß Thymeleaf3 in das HTML-Format geändert (Thymeleaf 2 ist im XHTML-Format) Auch ohne das schließende Tag "/" tritt kein Fehler auf.

loginForm.html


 //Vorher ändern
<!DOCTYPE html>
<html xmlns:th = "http://www.thymelaef.org">
<head>
<meta charset = "UTF-8" />
<title></title>
</head>

・ ・ ・

<td>
    <input type = "text" id = "username" name = "username" value = "aaaa" />
</td>

・ ・ ・

<td>
<input type = "password" id = "password" name = "password" value = "demo" />
</td>

・ ・ ・



 //Nach der veränderung
<!DOCTYPE html>
<html xmlns:th = "http://www.thymelaef.org">
<head>
<meta charset = "UTF-8">
<title></title>
</head>

・ ・ ・

<td>
    <input type = "text" id = "username" name = "username" value = "aaaa">
</td>

・ ・ ・

<td>
<input type = "password" id = "password" name = "password" value = "demo">
</td>

・ ・ ・


reserveForm.html


 //Vorher ändern
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org"
       xmlns:sec = "http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset = "UTF-8" />

・ ・ ・

	<tr th:each = "reservation : ${reservations}">
		<td>
			<span th:text = "${reservation.startTime}" />
			-
			<span th:text = "${reservation.endTime}" />
		</td>
		<td>
			<span th:text = "${reservation.user.lastName}" />
			<span th:text = "${reservation.user.firstName}" />
		</td>
		<td>
			<form th:action = "@{'/reservations/' + ${date} + '/' + ${roomId}}" method = "post"
					sec:authorize = "${hasRole('ADMIN') or #vars.user.userId == #vars.reservation.user.userId}">
				<input type = "hidden" name = "reservationId" th:value = "${reservation.reservationId}" />
				<input type = "submit" name = "cancel" value = "stornieren" />
			</form>
		</td>
	</tr>

・ ・ ・

 //Nach der veränderung
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org"
       xmlns:sec = "http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset = "UTF-8">

・ ・ ・

	<tr th:each = "reservation : ${reservations}">
		<td>
			<span th:text = "${reservation.startTime}">
			-
			<span th:text = "${reservation.endTime}">
		</td>
		<td>
			<span th:text = "${reservation.user.lastName}">
			<span th:text = "${reservation.user.firstName}">
		</td>
		<td>
			<form th:action = "@{'/reservations/' + ${date} + '/' + ${roomId}}" method = "post"
					sec:authorize = "${hasRole('ADMIN') or #vars.user.userId == #vars.reservation.user.userId}">
				<input type = "hidden" name = "reservationId" th:value = "${reservation.reservationId}">
				<input type = "submit" name = "cancel" value = "stornieren">
			</form>
		</td>
	</tr>

・ ・ ・


listRooms.html


 //Vorher ändern
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org"
       xmlns:sec = "http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset = "UTF-8" />

・ ・ ・

 //Nach der veränderung
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org"
       xmlns:sec = "http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset = "UTF-8">

・ ・ ・

Änderungen für Spring Data JPA 2.2

Unnötige Quelle

Es ist nicht erforderlich, die drei Konverter auf S.650 und 651 zu erstellen. (Weil Spring Data JPA2.2 java.time.Time unterstützt) ・LocalDateConverter.java ・LocalTimeConverter.java ・LocalDateTimeConverter.java

Ändern der findOne () -Methode des Repositorys

Die findOne () -Methode von CrudRepository wurde in findById () -Methode umbenannt. Der Rückgabewert wurde vom Entitätstyp in den optionalen Typ geändert. Ändern Sie daher wie folgt fineOne() => findById().get()

Serviceklasse

ReservatoinService.java


 //Vorher ändern

・ ・ ・
 public Reservation findById(Integer reservationId) {
    return reservationRepository.findOne(reservationId);
 }

 //Nach der veränderung

・ ・ ・
 public Reservation findById(Integer reservationId) {
    return reservationRepository.findById(reservationId).get();
 }

RoomService.java


 //Vorher ändern

・ ・ ・

 public MeetingRoom findMeetingRoom(Integer roomId) {
	return meetingRoomRepository.findOne(roomId);
 }

 //Nach der veränderung

・ ・ ・

 public MeetingRoom findMeetingRoom(Integer roomId) {
	return meetingRoomRepository.findById(roomId).get();
 }

ReservationUserDetailsService.java


 //Vorher ändern

・ ・ ・

 User user = userRepository.findOne(username);

 //Nach der veränderung

・ ・ ・

User user = userRepository.findById(username).get();

Recommended Posts

Frühjahr gründliche Einführung Version Upgrade Memo
Frühlingsrückblick Memo
Spring Fox ① Einführung
JJUG CCC Frühjahr 2018 Memo
Hinweise zur Verwendung von Spring Shell
Schreiben von Frühlingsstiefel-Memos (1)
Schreiben von Spring Boot-Memos (2)
Upgrade der CentOS 7 Curl-Version
Erstellen Sie mit IntelliJ IDEA eine Entwicklungsumgebung "Spring Thorough Introduction"
[Persönliche Notizen] Über das Spring Framework
JJUG CCC 2018 Frühlingsbeteiligungsprotokoll
Spring Security-Nutzungsnotiz CSRF
Spring Framework Selbststudium Memo series_1
Einführung in Ratpack (7) - Guice & Spring
Sicherheit der Verwendungsnotizmethode für Spring Security
Spring Security-Nutzungsnotiz Remember-Me
Einführung in Spring Boot ② ~ AOP ~
Eclipse 4.8 Einführungsnotiz (Plugin Edition)
Spring Security-Nutzungsnotiz CORS
Spring Security-Verwendungsnotiztest
Memo zur Spring Boot Controller-Methode