This Spring Boot service implements base CRUD operations and try to provide these operations for your entities in declarative way.
Implemented operations:
- Create
- Update
- Delete
- Get one
- Get all
For example:
POST /users
PATCH /users/1
DELETE /users/1
GET /users/1
GET /users
To use these operations with your entity you have perform the following steps:
-
Inherit your entity from
LongIdEntity
(or first create your own base entity - seeLongIdEntity
as example):@Getter @Setter @AllArgsConstructor @NoArgsConstructor @Entity @Table(name = "users") @DynamicInsert @DynamicUpdate @Cache(usage = READ_WRITE, region = "user") public class User extends LongIdEntity { @Column(nullable = false, length = 32) private String name; }
-
Create three DTOs: 'create request', 'update request' and 'response', based on
CreateRequest
,UpdateRequest
andBaseResponse<ID>
respectively:@Data @EqualsAndHashCode(callSuper = false) public class UserCreateRequest extends CreateRequest { @NotBlank private String name; } @Data @EqualsAndHashCode(callSuper = false) public class UserUpdateRequest extends UpdateRequest { private String name; } @Getter @Setter public class UserResponse extends BaseResponse<Long> { private String name; }
-
Create a repository for your entity, based on
BaseRepo<ID, T>
public interface UserRepo extends BaseRepo<Long, User> { }
-
Create a mapper, based on
BaseMapper
, which will perform mapping between your entity and DTOs:@Mapper(componentModel = "spring", nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) public abstract class UserMapper implements BaseMapper<Long, User, UserCreateRequest, UserUpdateRequest, UserResponse> { }
-
Create a service for your entity. based on
AbstractBaseService
:@Service public class UserService extends AbstractBaseService<Long, User, UserCreateRequest, UserUpdateRequest, UserResponse> { public UserService(UserRepo repo, UserMapper mapper) { super(repo, mapper); } }
-
And create a REST controller, based on
BaseController
:@RestController @RequestMapping("users") public class UserController extends BaseController<Long, User, UserCreateRequest, UserUpdateRequest, UserResponse> { public UserController(UserService service) { super(service); } @PostMapping @Override protected ResponseEntity<?> create(@Valid @RequestBody UserCreateRequest request) { return super.create(request); } @PatchMapping("/{id}") @Override protected ResponseEntity<?> update(@PathVariable("id") Long id, @Valid @RequestBody UserUpdateRequest request) { return super.update(id, request); } @DeleteMapping("/{id}") @Override protected ResponseEntity<?> delete(@PathVariable("id") Long id) { return super.delete(id); } @GetMapping @Override protected ResponseEntity<?> getAll(Pageable pageable) { return super.getAll(pageable); } @GetMapping("/{id}") @Override protected ResponseEntity<?> getOne(@PathVariable("id") Long id) { return super.getOne(id); } }
Then you will be able to perform all CRUD operations with your entity, for example:
GET /users/1
{
"name": "user1"
}
You can implement more complex entities, which have associations with over ones - see package io.github.cepr0.demo.user
as example.
To reduce a pressure to the database, the project uses Ehcache 3 as Hibernate 2nd level cache.
You can add respective entries to the 'resources/ehcache.xml' file for your entities.
Project uses MapStruct to generate mappers code automatically.
But for complex entities you can make additional codding (see package io.github.cepr0.demo.user
as example).
Project uses log4jdbc to log JDBC queries and their results (see 'pom.xml' for 'log4jdbc-spring-boot-starter' dependency and corresponding settings in the 'resources/application.yml'). It also logs Hibernate statistic which you can turn off in 'resources/application.yml'.
Project uses H2 database. You can replace it to another RDBMS - the project does not use specific SQL dialect, only Spring Data JPA query methods.
Project uses Java 11.