This article is [this blog](https://yk0807.com/techblog/2019/05/06/spring-boot%E5%85%A5%E9%96%80%E3%81%9D%E3%81 % AE1 /) is moved to Qiita. It's been a little over a month since I started using Spring Boot, and there may be some inappropriate explanations, but I would appreciate it if you could point out that.
In WEB + DB PRESS 106, there was a Spring Boot article about creating a task management service and Qiita crawl & distribution service. For the time being, I tried the task management service. The source code created by imitating the WEB + DB PRESS article this time can be found at here. We plan to make various improvements in the future. By the way, the finished product (?) Looks like this.
The Spring Framework, a Java web framework, was created in 2003, and Spring Boot was created in 2014 to make it easier to use. [DI (Dependency Injection)](https://ja.wikipedia.org/wiki/%E4%BE%9D%E5%AD%98%E6%80%A7%E3%81%AE%E6%B3 It is characterized by using the concept of% A8% E5% 85% A5).
-OpenJDK (Of course, if you develop in Java before the framework, you need it. If you want to use the current functions, you need at least 8 versions) -Spring Tool Suite (abbreviated as STS. Eclipse-based IDE. It seems that it was done in 3.9.5 for books, but as of April 2019, 4 or later It's the default, so I did it in 4.1.2) -Lombok (Read as Lombok. It is a convenient library that automatically generates getters and setters just by adding annotations. Install by simply double-clicking the downloaded lombok.jar. Will be.) -Database management system (Because it is a Web system, it is decided that it is necessary. I think that I should use the one I usually use. I used H2 Database in the book, but I did it with MySQL that I am familiar with. ) ・ Pleiades (Read as Pleiades. Eclipse Japanese localization plug-in. I don't think it is necessary for people who are good at English, but I installed it. See here for how to do this (the link is for Windows, but you can also use Mac to put the contents of the downloaded one in the features and plugins folders. It's okay).) -Maven (Read as Maven. Java build tool. If you download STS from the official page and install it, it should be okay, but install it with the brew command etc. If you do, check if it is included with mvn -version, and if not, bring it from the official page or install it with brew install maven)
Here, I will briefly explain the configuration of Spring Boot with a feeling like comparison with Laravel. In Spring Boot, unlike Laravel, the folder structure is not decided, so you need to think about the structure yourself. Java sources are placed under src / main / java, and non-Java (html, css, js, etc.) are placed under src / main / resources. The source for testing with JUnit is located under src / test.
src / main / java (Java source file) -Application (This is the main function. Annotate it with @SpringBootApplication. This main function is called when the project is executed. Is it close to Laravel's index.php?) -Entity (Data definition. Add @Entity annotation. Import lombok.Data and add @Data to automatically generate getters and setters. Furthermore, JPA If you add @Table (name = “table name”) using .org / wiki / Java_Persistence_API) (standard technology for handling databases in Java), a table will be created automatically. Laravel's Model (directly under app) He) + migration?) -Repository (It is for performing basic operations of CRUD (create, read, update, delete) of data defined in Entity, and is defined as an interface that inherits JpaRepository. It is a function that Laravel does not have.) -Controller (Controller that handles C of MVC, similar to that of Laravel, but implements processing for data etc. by calling Service explained below. Add @Controller annotation to class. Also, Get method Then you can map the request by annotating @GetMapping (value = ”(URL)”) in the method and @PostMapping (value = ”(URL)”) in the method in the case of Post method, so Laravel Then it may be an image close to Controller + routes) -Service (Implements the processing to be performed on the data and passes it to the Controller. This is also not in Laravel, but if you dare to say it, it may be an image that connects between the Model and the Controller. @Service annotation The actual processing is implemented by calling the CRUD operation of Repository.) -Form (a class for passing the value entered in the html form to the controller. It seems that validation is often done here. Request in Laravel?)
src / main / resources (front end, etc.) -Thymeleaf (read as time leaf. It seems to be named after the herbs of plants. So-called template engine. Blade is the default for Laravel, but Spring Boot seems to support several other template engines. ) -Application.properties (It has application setting values, and I think that the files under config are close to each other in Laravel. The database settings to be used are also set here.)
As mentioned above, I wrote it in a hurry, so it was a rough explanation, but when I write Part 2, I think I should write it in a little more detail.
Recommended Posts