美文网首页
自定义JpaRepository

自定义JpaRepository

作者: MrY_124d | 来源:发表于2019-05-29 16:44 被阅读0次

第一步

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);
    }
}

相关文章

网友评论

      本文标题:自定义JpaRepository

      本文链接:https://www.haomeiwen.com/subject/ipwytctx.html