The Generic CRUD Framework simplifies the development of Spring Boot applications by providing a structured approach to mapping Data Transfer Objects (DTOs) to entities and implementing CRUD operations. Leveraging the power of ModelMapper and abstract classes, it streamlines the creation of services and controllers with minimal boilerplate code.
- Simplified DTO to Entity mappings and vice versa.
- Abstract configurations for easy mapping between DTOs and entities.
- Extended support for CRUD operations on entities with direct and related entities.
- Predefined hooks for custom business logic before and after CRUD operations.
- Ready-Made CRUD Services and Controllers: Enables quick generation of fully functional CRUD services and controllers with minimal coding required. This feature allows developers to focus on business logic and application-specific requirements by leveraging generic patterns and practices for common CRUD operations.
- JDK 8+
- Spring Boot 2.x
- ModelMapper
Add the JitPack repository and the framework dependency to your pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.NikolayNN</groupId>
<artifactId>crud-generic</artifactId>
<version>latest.version.here</version>
</dependency>
Find the latest versions at: https://jitpack.io/#NikolayNN/crud-generic
Before diving into the specifics of entity and DTO creation, enable the framework in your Spring Boot application by using the @EnableAbsGenericCrud annotation. This step is crucial as it sets up the necessary configurations for ModelMapper and other components required by the framework.
Add @EnableAbsGenericCrud to your Spring Boot application's main class or any configuration class:
@SpringBootApplication
@EnableAbsGenericCrud
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}Entities should extend AbstractEntity<?>, where ? is the type of your identifier (e.g., Long).
@Entity
public class MyEntity extends AbstractEntity<Long> {
// Entity definition
}Define your DTOs for create, read, and update operations. Create and Update DTOs should extend AbsCreateDto and AbstractDto respectively, while Read DTO can directly extend AbstractDto.
public class MyCreateDto extends AbsCreateDto {
// Fields specific to creation
}
public class MyReadDto extends AbstractDto<Long> {
// Fields for reading
}
public class MyUpdateDto extends AbstractDto<Long> {
// Fields for updating
}Extend AbsFlexMapConfig in your configuration to set up mappings. Override abstract methods to return concrete mapper instances.
@Configuration
public class MyMappingConfig extends AbsFlexMapConfig<MyCreateDto, MyUpdateDto, MyReadDto, MyEntity> {
public MyMappingConfig(AbsDtoModelMapper mapper) {
super(mapper, MyCreateDto.class, MyUpdateDto.class, MyReadDto.class, MyEntity.class);
}
@Override
protected AbsMapBaseDtoToEntity<MyReadDto, MyEntity> mapperReadDtoToEntity(AbsDtoModelMapper mapper, Class<MyReadDto> readDtoClass, Class<MyEntity> entityClass) {
// Implementation of mapping from MyReadDto to MyEntity
}
// Implement other mappings similarly
}Extend AbsFlexServiceCRUD or AbsFlexServiceExtCRUD for CRUD services. Implement abstract methods and use the provided functionalities.
@Service
public class MyEntityService extends AbsFlexServiceCRUD<Long, MyEntity, MyReadDto, MyUpdateDto, MyCreateDto, MyRepository> {
// Constructor and methods
}Extend AbsFlexControllerCRUD or AbsFlexControllerExtCRUD for CRUD operations in your controller.
@RestController
@RequestMapping("/my-entity")
public class MyEntityController extends AbsFlexControllerCRUD<Long, MyReadDto, MyReadDtoView, MyUpdateDto, MyCreateDto, MySettings, MyEntityService> {
// Constructor and endpoint methods
}