Spring Data JPA stepped in pit

To be honest, Spring Data JPA useful, or was last used in 2013, it was just completed mapping and database tables in the Java Bean.

Recently think the cause with Spring Data JPA is a project at hand, source code is written in the native SQL + JDBC achieve, in the first deployment to initialize the database, also hardcode a lot of database configuration parameters. Just recently available, it is intended to transform what used Spring Boot Data JPA (spring-boot-starter-data-jpa), carefully looked at the source code has been found and the bad days to do a few years ago, if your business logic is not particularly or rational design of complex table structures, SQL logic implementation is not really a line to write, conscience ah, really for the sake of lazy people like me ah.

First started writing Repository, commonly used in two ways, one is to write an interface inheritance JpaRepository, code is as follows:

package com.company.inventory.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import comcompany.inventory.model.Device;
public interface DeviceRepository extends JpaRepository<Device, Long> {
    List<Device> findBySnOrderByGmtCreatedDesc(String sn);
}

Another is inherited CrudRepository, code is as follows:

package com.company.inventory.repository;

import java.util.List;
import org.springframework.data.repository.CrudRepository;
import comcompany.inventory.model.Device;
public interface DeviceRepository extends CrudRepository<Device, Long> {
    List<Device> findBySnOrderByGmtCreatedDesc(String sn);
}

JpaRepository and CrudRepository relationship as follows:

public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> 
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> 

public interface CrudRepository<T, ID> extends Repository<T, ID>

From the above relationship can be seen JpaRepository addition to all the things to do CrudRepository can do, but also more paging and sorting features and functions QueryByExampleExecutor QueryByExample provided. But also JpaRepository and JPA persistence technology is bound. http://jtuts.com/2014/08/26/difference-between-crudrepository-and-jparepository-in-spring-data-jpa/

It is proposed to make use of CrudRepository or PagingAndSortingRepository

 

Guess you like

Origin www.cnblogs.com/siodoon/p/11407666.html