Generates the standard SPRING 3 layer CRUD architecture starting from JPA entities.
- Download and extract the ZIP/TAR file from releases;
- Edit the file in /bin directory named 3layer-settings.yml;
- Run the application (layer3gen.sh or layer3gen.bat).
- Add the plugin in your build.gradle;
plugins {
id "gae.piaz.layer3gen" version "1.9"
}
or you can use gradle legacy plugin for older gradle versions:
buildscript {
dependencies {
classpath "gae.piaz:layer3gen:1.9"
}
// ....
}
// ...
apply plugin: 'gae.piaz.layer3gen'
- Create a file named 3layer-settings.yml (you willl find an example below) in the directory src/main/resources/;
- Run the gradle task.
gradlew clean build layer3gen
projectPath: /home/yourproject/demo
# projectPath: c://workspace/demo
classesDirectory: build/classes/java/main
outputDirectory : src/main/java
options:
dtoLayer : true # generates the dto from entities, controller using dto, and mapper layer
serviceInterface: true # whatever to generate CrudService Interface or not
entityControllers: false # whatever to generate controller using jpa entity or not
inputPackages:
jpaEntities : com.example.demo.model
outputPackages:
repositories : com.example.demo.repository
services: com.example.demo.service
controllers: com.example.demo.controller
dtos: gae.piaz.layer3gen.output.dtos # if this in not specified the dto package will be under the controllers package
mappers: gae.piaz.layer3gen.output.mappers # if this in not specified the mapper package will be under the services package
Checkout the example project with generated classes present in /demo and /demo-jakarta folder.
@Repository
public interface BooksRepository extends JpaRepository<Books, java.lang.Integer> {
}
@Service
public class BooksService implements CrudService<Books,java.lang.Integer> {
@Autowired
private BooksRepository repository;
@Override
public Books create(Books entity) {
return repository.save(entity);
}
@Override
public Books update(Books entity) {
return repository.save(entity);
}
@Override
public Page<Books> find(Books entity, Pageable pageable) {
Example<Books> example = Example.of(entity);
return repository.findAll(example,pageable);
}
@Override
public Optional<Book> getOne(java.lang.Integer primaryKey) {
return repository.findById(primaryKey);
}
@Override
public void delete(java.lang.Integer primaryKey) {
repository.deleteById(primaryKey);
}
}
@RestController
@RequestMapping("/books-dto/")
public class BooksControllerDTO implements CrudController<BooksDTO,java.lang.Integer>{
@Autowired
private BooksService service;
@Autowired
private BooksMapper mapper;
@Override
public ResponseEntity<BooksDTO> create(@RequestBody BooksDTO dto) {
Books entity = mapper.toEntity(dto);
entity = service.create(entity);
return ResponseEntity.ok(mapper.toDto(entity));
}
@Override
public ResponseEntity<BooksDTO> update(@RequestBody BooksDTO dto) {
Books entity = mapper.toEntity(dto);
entity = service.update(entity);
return ResponseEntity.ok(mapper.toDto(entity));
}
@Override
public ResponseEntity<Page<BooksDTO>> find(
@RequestBody BooksDTO dto,
@RequestParam("page") Integer page,
@RequestParam("size") Integer size) {
Pageable pageable = PageRequest.of(page,size);
Books entity = mapper.toEntity(dto);
Page<BooksDTO> pages = service.find(entity, pageable).map(mapper::toDto);
return ResponseEntity.ok(pages);
}
@Override
public ResponseEntity<BookDTO> getOne(@PathVariable("id") java.lang.Integer primaryKey) {
Optional<Book> entity = service.getOne(primaryKey);
return entity.map(e -> ResponseEntity.ok(mapper.toDto(e))).orElseGet(() -> ResponseEntity.notFound().build());
}
@Override
public void delete(java.lang.Integer primaryKey) {
service.delete(primaryKey);
}
}
Other insight can be found in this blog post: https://medium.com/p/49f3fbbc7b2d