Service Level Crud Techniques Using Spring JPA Data

I am creating a small application using Spring and Spring JPA data, and I need to use CrudRepository methods at the service level, so I made 2 classes: GenericService and GenericServiceImpl. But I do not know if this is the right or even the best approach.

Here is an example:

POJO:

@Entity public class User { @Id private Long id; private String username; } 

DAO:

 public interface UserDAO extends CrudRepository<User, Long> { User findOneByUsername(String username); } 

General service

 public interface GenericService<T, ID extends Serializable> { <S extends T> S save(S entity); } 

Service

 public interface UserService extends GenericService<User, Long> { User findOneByUsername(String username); } 

General service.

 public class GenericServiceImpl<T, ID extends Serializable> implements GenericService<T, ID> { @Autowired private CrudRepository<T, ID> repository; @Override public <S extends T> S save(S entity) { return repository.save(entity); } } 

Service Impl.

 @Service @Transactional public class UserServiceImpl extends GenericServiceImpl<User, Long> implements UserService { @Autowired private UserDAO userDAO; @Override public User findOneByUsername(String username) { userDAO.findOneByUsername(username); } } 
+6
source share
1 answer

Have you seen this sample project inside Spring Boot? This is a good starting place.

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa

+5
source

Source: https://habr.com/ru/post/972548/


All Articles