Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

package structure in Spring,Entity vs Model vs Controller

how do you define what is model or entity in MVC?

most of my Spring codes had package strucure like his: http://www.mkyong.com/spring-mvc/spring-mvc-form-handling-example/

I have my view, my controller, my dao and "the model" for the dao

but I try to learn thymeleaf and found this: https://github.com/thymeleaf/thymeleafexamples-stsm

there is no "model"package, he call it entity, it is an entity but..

then I thought...oh wait a minute...what is the definition of entiy and model? similar to this question: Entity vs Model vs View Model

so what is your package structre, do you call it model or entity? do you have maybe examples of the packagenames/structure of your spring projects?

like image 951
w3Charlie Avatar asked Mar 22 '17 08:03

w3Charlie


2 Answers

Literally, a Model is a class for representing POJO. Entity - has relation to DB.

Sometimes they are mixed together, like:

package net.lelyak.edu.entity;

@Entity
public class User extends BaseEntity {
// fields + get/set 

The structure depends on a convention of your project or team.

If it is your personal project you can fully decide which package structure you want to follow.

Here is some way which I did for my spring training:

enter image description here

However, for your own purpose, you can fully decide how to manage packages, following are also legit:

enter image description here

The main idea is that no strict borders for it.

For the personal project, it has to be a convenience for you.
When you collaborating with others on the same project, you have to agree on it together.

like image 126
catch23 Avatar answered Oct 21 '22 23:10

catch23


My Spring projects most have a package structure like below. This structure is my base structure for dao, services, implementations, view and util. Sure the structure can change and all that depends on what you have to develop.

com.companyName.appName.config -> 
I use config for my classes which are annotated with @Configuration.

com.companyName.appName.dao.model -> 
I use dao.model for all my entities from the DB.

com.companyName.appName.dao.repository -> 
I use dao.repository for all the repositories used with spring-data-jpa.

com.companyName.appName.dao.repository.impl -> 
I use dao.repository.impl for all customized implementations of repositories. 
For example to autowire the entityManager.

com.companyName.appName.service -> 
I use service for all my service interfaces

com.companyName.appName.service.impl -> 
I use service.impl for all my implementations of services

com.companyName.appName.controller -> 
I use controller for all my controllers.

com.companyName.appName.view.model -> 
I use view.model for all my frontend specific models which are no enitites.

com.companyName.appName.view.form -> 
I use view.form for all my frontend specific forms which has to be submitted and validated.

com.companyName.appName.util -> 
I use util for utility stuff.

I hope this gives you a short overview how I structure my packages for spring-boot applications. But everybody like to have its own naming. So its very difficult to make it general.

like image 6
Patrick Avatar answered Oct 21 '22 23:10

Patrick