This is a demo of a Spring Boot 2 MultiProject with several layers and two DAO implementations using maven profiles: MyBatis or JPA + Hibernate.
It has the following modules:
- application
- services
- database
- mybatis-repository
- jpa-repository
Using maven profiles and Spring, the DAO implementation is chosen at build time. MyBatis is the DAO implementation by default.
mvn package -Pmybatis
mvn package -Pjpa
The profiles are designed to exclude the unused dependencies. For example, if you choose MyBatis, the module jpa-repository is excluded from the final JAR file.
To start the applicacion you can use the following commands:
mvn spring-boot:run -Pmybatis
mvn spring-boot:run -Pjpa
This module contains the Application entry point of the project. Also, it includes the application Controllers and common configuration shared between the layers.
The application Controllers invoke Services methods.
This layer contains the Application Services used by Controllers or other Services. In this layer, Services can invoke DAO or other Services methods.
This module sets up an in memory H2 Database for testing purposes. Also, it defines the DAO API with 2 implementations: jpa-repository and mybatis-repository.
To avoid Hibernate or MyBatis dependencies on this module, in the entities for example, I have decided to translate the entities results from jpa-repository and mybatis-repository to an intermediate entity defined in this module.
The modules jpa-repository and mybatis-repositoy are responsible to translate their entities definition to Database module entities.
This module implements DAOs defined in database module and it uses MyBatis for this purpose. The DAO methods converts the MyBatis entities to database entities, defined in database module, using the ModelMapper library.
This module implements DAOs defined in database module and it uses JPA and Hibernate for this purpose. The DAO methods converts the JPA entities to database entities, defined in database module, using the ModelMapper library.