自定义JpaRepository

第一步

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);
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 陈氏太极拳老架一路讲义 前言 写给喜爱太极拳的武术朋友们 中华武术,源远流长,今虽门派繁多,实一脉相承。太极拳以它...
    阿德乐阅读 6,474评论 0 12
  • 《播种幸福》麦克格西 并不是最勤奋的人就最富有。 并不是博士更有机会成为亿万富翁。 有时候,似乎选择比勤奋更重要。...
    黄玉新阅读 4,745评论 1 11
  • 因为刚好遇见你,留下足迹才美丽。 初三结束的时候,忍痛焚烧了陪伴今年的日记本,那里面藏着几年的回忆,和隐隐...
    世界的另一个我阅读 264评论 0 0
  • 没有什么可能的,也没有什么不可能的。 今天真的很累,但是也很开心,开心的是终于能回家了。其实感觉每一天的生活都是一...
    雨山YS阅读 379评论 0 4
  • 看了一篇文章,和大家分享: 1,别总拉着老师的手,说:孩子就交给你了!有教无育,何谈教育?只教不养,何来教养?何况...
    董涛_1dcd阅读 172评论 0 0

友情链接更多精彩内容