[JAVA] Spring with Kotorin --8 Repository layer

Overview / Description

Spring with Kotorin --7 Service has been modified to avoid concentrating responsibilities on a single module. The reason is to avoid becoming a ** Fat Controller ** with low maintainability, extensibility, and readability. For that purpose, we separated the application logic from the Controller layer and provided a ** Service ** layer.

service

This makes it possible to separate the functional areas to be focused on in the Controller layer and the Service layer.

layer role
Controller layer Accepting requests
Service layer Processing of business logic other than request acceptance

Now consider the newly created responsibilities of the Service layer.

The role of the Service layer is to handle business logic other than accepting requests. In other words, as it is, the persistence processing of the result of business logic will also be performed in this Sevice layer.

By the way, if you think about data persistence, if you are aware of the target area to be persisted in the Service layer, It is necessary to design according to the type of target area.

In addition, the dependency on the type of external area becomes stronger, and the range of influence becomes larger when making changes. For example, taking a database as an example, changing from Oracle DB to MySQL requires a design that is aware of the dialect differences.

service-db.png

Considering the role of the service layer, ** business logic processing **, it is easier to design and maintainability if the result can be processed transparently without being aware of the persistence destination.

A ** Repository ** layer is provided as a mechanism to separate the persistence process from this Service layer.

repository.png

Assumptions / Environment

Runtime version

Spring Dependencies

Development environment

When setting up the Repository layer, this time we will target the database as a persistence area. Also, for the sake of brevity, we will use the H2 database as the database type and run it in embedded mode.

Procedure / Explanation

Dependency Add the following Dependency to build.gradle for database access.

Also, add the following Dependency to use H2 Database.

Database definition

Add the following definitions for the H2 database to application.yml.

spring:
  datasource:
    tomcat:
      test-on-borrow: true
      validation-interval: 30000
      validation-query: SELECT 1
      remove-abandoned: true
      remove-abandoned-timeout: 10000
      log-abandoned: true
      log-validation-errors: true
      max-age: 1800000
      max-active: 50
      max-idle: 10
    driver-class-name: org.h2.Driver
    url: jdbc:h2:mem:app;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE
    username: guest
    password: guest
  h2:
    console:
      enabled: true
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Repository layer

The following interface definition was made to provide a layer for persistence processing.

interface MessageRepository : CrudRepository<Message, String>

Makes you work with entities that persist data through this interface.

Service layer

From the Service layer, the entity is operated via the injected Repository layer instance.

@Autowired
lateinit var repository: MessageRepository

fun getMessages() : Iterable<MessageDTO> = repository.findAll().map { it -> MessageDTO(it) }

fun insertMessage(messageDto: MessageDTO) = MessageDTO(
        repository.save(Message(
                                title = messageDto.title,
                                message = messageDto.message
        ))
)

Summary / Looking back

You can see that the code has become much simpler than before the separation by designing the roles as Controller / Service / Repository separately. By designing in this way, it is possible to prevent the module from becoming bloated, spaghetti coded, and reduced in scalability.

This source

Recommended Posts

Spring with Kotorin --8 Repository layer
Spring with Kotorin ―― 7. Service layer
Spring with Kotorin --8 Repository layer --Supplement: H2 Database
Spring with Kotorin --- 5. Actuator
Spring with Kotorin ―― 1. SPRING INITIALIZR
Spring with Kotorin --6 Asynchronous processing
Spring with Kotorin --4 REST API design
Spring with Kotorin --9 Database migration --Flyway
Spring with Kotorin --2 RestController and Data Class
Creating a common repository with Spring Data JPA
Self-made Validation with Spring
With Kotorin ―― 7. Scoping Function
Download with Spring Boot
Spring with Kotorin --3. Omitting curly braces from the function
Generate barcode with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Login function with Spring Security
Using Mapper with Java (Spring)
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Link API with Spring + Vue.js
Implement REST API with Spring Boot and JPA (domain layer)
Create microservices with Spring Boot
Send email with spring boot
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Implemented authentication function with Spring Security ③
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Implemented authentication function with Spring Security ①
Get validation results with Spring Boot
Implement file download with Spring MVC
Oauth2 authentication with Spring Cloud Gateway
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
[Spring] Autowired multiple beans. (With bonus)
DB access using repository with SpringData.
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Authentication / authorization with Spring Security & Thymeleaf
Test Spring framework controller with Junit