** 20190508 update **
I've been using Ruby for a long time, but now I'm using Java and I'm training for it. So I used Spring for the first time, but since I was developing using Ruby and Rails so far, there is a point, so I decided to write this time if possible to share this. In terms of level, I really moved a little by reading a book about the basics of Java and the basics of the Web.
What I got stuck in was that the shape of MVC was different from Rails, so I didn't know how to implement it. I noticed this after doing Spring, but I felt that Rails' MVC was made so that it could be implemented quite easily. Since each MVC is divided into only three, Model, View, and Controller, it's fairly easy to know what to write. It's really easy. On the other hand, Spring has a slightly different form of MVC realization. When you break up into Controller, Service, Entity, Repository. I understand that the role of Controller is not so different from Rails, but what else? What should I write? It became.
From here the great journey began. Apparently, I understand that Repository interacts with DB, but Service is also relatively close, isn't it? What is Entity? It became. Anyway, if you don't understand this, you can't learn even if you make it! Anyway, I will investigate various things. What was difficult here was that each role was expressed relatively abstractly. For example, Service implements business logic. No, it's hard to understand. What is that business logic in the first place? While doing so, I came across a site that has what I want to know. Web basics with Spring boot It's from Slide Share and it's easy to understand. I finally got my own answer here. I will write it here for my own study.
Each name | role |
---|---|
Controller | controller. Interact with the browser and Service. |
Service | The part that describes what you want to do with the DB. A place to write specific things such as saving and updating. |
Repository | Receive a processing request from Service and communicate directly with DB. Use it as an abstraction. Data acquisition, update, etc. |
Entity | A container for data. It's like a box for storage. |
It's organized within me, but it may be quite confusing. With some annotations, Repository can be interpreted as an abstraction for the realization of Service processing. It is for implementing what the Service does. So, Entity needs to save the value to communicate with DB, so the box to use at that time. Like putting numbers. It plays such a role. I've written it here so that it's easy for me to understand, but if you don't understand it, you may want to take a closer look at the link above.
Thanks to the site I mentioned earlier, I understand the role of the actor in Spring. My impression is that some people are likely to get stuck.
Recommended Posts