第一步
package com.dtt.fileservice.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import java.io.Serializable;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @Auther: yangxiong
* @Date: 2019/05/29/13:48
* @Description:
*/
@NoRepositoryBean
public interface BaseJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
List<Object[]> listBySQL(String sql);
}
第二步
package com.dtt.fileservice.repo;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import javax.persistence.EntityManager;
import java.io.Serializable;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @Auther: yangxiong
* @Date: 2019/05/29/13:50
* @Description:
*/
@NoRepositoryBean
public class BaseJpaRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
implements BaseJpaRepository<T, ID> {
private final EntityManager entityManager;
private final JpaEntityInformation entityInformation;
private final Class<T> clazz;
public BaseJpaRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
this.entityInformation = entityInformation;
this.clazz = entityInformation.getJavaType();
}
@Override
public List<Object[]> listBySQL(String sql) {
System.out.println(sql);
return null;
}
}
第三步
@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = BaseJpaRepositoryImpl.class)
public class FileserviceApplication {
public static void main(String[] args) {
SpringApplication.run(FileserviceApplication.class, args);
}
}
网友评论