Implementing a simple Web REST API server with Spring Boot + MySQL --Qiita
Outline Implement the infrastructure layer according to the following design. Consider the architecture of Web API implemented by Spring Boot --Qiita
Create the following 3 classes.
├── infrastructure
│ ├── entity
│ │ └── UserEntity.java
│ └── repository
│ ├── UserJpaRepository.java
│ └── UserRepositoryImpl.java
UserEntity.java The class to which MySQL records are mapped. Define a method to convert bidirectionally with Domain Object (User.java).
UserEntity.java
package com.example.springapi.infrastructure.entity;
import com.example.springapi.domain.object.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*RDB record mapping class
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "test_users")
public class UserEntity {
@Id
@Column(name = "id")
private String id;
@Column(name = "value")
private String value;
/**
*Create an instance from a domain object
*
* @param user domain object
* @return UserEntity
*/
public static UserEntity build(User user) {
return UserEntity.builder()
.id(user.getId())
.value(user.getValue())
.build();
}
/**
*Convert to domain object
*
* @return domain object
*/
public User toDomainUser() {
return User.builder()
.id(this.id)
.value(this.value)
.build();
}
}
@Entity This expresses the JPA Entity (mapped class). @Table Specify the table name. @id Specifies that it is a primary key. In the case of a compound key, the writing method changes. @Column Specify the column name of the table.
@NoArgsConstructor Automatically create a constructor with no arguments. A constructor with no arguments is required due to the life cycle of JPA Entity. Not required if the configuration has a constructor with no arguments. (If there is no final field, etc. It is absolutely necessary if you use @Builder) @AllArgsConstructor Create a constructor that takes all fields as arguments. If NoArgsConstructor is specified, @Builder will not work unless this is also specified (@Builder automatically generates a constructor that takes all fields as arguments, but does not create a constructor for a class with xxxArgsConstructor. And it doesn't work.)
UserJpaRepository.java Interface required to use JPA implementation. Inherit JpaRepository and specify Entity class and primary key type. At runtime, the standard implementation provided by spring-data-jpa called SimpleJpaRepository is injected by the DI container, so there is no need to write an implementation class.
UserJpaRepository.java
package com.example.springapi.infrastructure.repository;
import com.example.springapi.infrastructure.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
/**
*Interface for using JPA
*/
public interface UserJpaRepository extends JpaRepository<UserEntity, String> {
}
UserRepositoryImpl.java Implementation class of UserRepository interface defined in domain layer. The actual processing is done by SimpleJpaRepository.
UserRepositoryImpl.java
package com.example.springapi.infrastructure.repository;
import com.example.springapi.domain.object.User;
import com.example.springapi.domain.repository.UserRepository;
import com.example.springapi.infrastructure.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/**
*Persistence implementation class
*Convert domain object to Entity and wrap JPA
*/
@Repository
@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepository {
@NonNull
private final UserJpaRepository userJpaRepository;
/**
* {@inheritDoc}
*/
@Override
public Optional<User> findById(String id) {
return this.userJpaRepository.findById(id)
.map(UserEntity::toDomainUser);
}
/**
* {@inheritDoc}
*/
@Override
public User save(User user) {
return this.userJpaRepository.save(UserEntity.build(user))
.toDomainUser();
}
/**
* {@inheritDoc}
*/
@Override
public void deleteById(String id) {
this.userJpaRepository.deleteById(id);
}
}
@Repository Another name for @Compornent. With @Service.
Recommended Posts