Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义SQL查询和Spring数据JPA_Java_Sql_Spring_Spring Data Jpa_Spring Data - Fatal编程技术网

Java 自定义SQL查询和Spring数据JPA

Java 自定义SQL查询和Spring数据JPA,java,sql,spring,spring-data-jpa,spring-data,Java,Sql,Spring,Spring Data Jpa,Spring Data,如何在Spring数据JPA中编写自定义查询? 有没有比下面更方便的方法?我以前是这样做的: public class TestCustomRepositoryImpl implements TestCustomRepository { @PersistenceContext private EntityManager entityManager; @Override public Double getValue(Long param) {

如何在Spring数据JPA中编写自定义查询? 有没有比下面更方便的方法?我以前是这样做的:

public class TestCustomRepositoryImpl implements TestCustomRepository {

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Double getValue(Long param) {
        // Simple SQL query for example.
        String queryString = "SELECT column_name1 FROM table_name1 " 
             + "WHERE column_name2 = " + param;
        Query query = entityManager.createNativeQuery(queryString);

        List resultList = query.getResultList();
        if (!resultList.isEmpty()) {
            Number value = (Number) resultList.get(0);
            if (value != null) {
                return value.doubleValue();
            }
        }

        return 0.0;
    }
}
然后,我将自定义接口添加到我的JPA存储库中:

public interface TestRepository extends JpaRepository<TestEntity, Long>, TestCustomRepository {

}
公共接口TestRepository扩展了JpaRepository、TestCustomRepository{ } 还有更方便的方法吗?例如,我可以将Spring数据用于CRUD并使用MyBatis实现TestCustomRepository吗如何实现自定义方法?

您可以在存储库中编写本机查询,如下所示:
You can write the native query in the repository like this:

1)
@Query("SELECT t.column_name1 FROM table_name1 t WHERE t.column_name2 =:param")
List<String> getValue(@Param("param") String param);

OR

2)@Query("SELECT t.column_name1 FROM table_name1 t WHERE t.column_name2 = 'param'", nativeQuery=true)
List<String> getValue();

Or you can use NamedQuery and add the query in the model only.
for eg: 
@Entity
@Table(name = "table_name1", schema="db_name")
@NamedQuery(name = "ENTITY.fetchParam",
        query = "SELECT t.column_name1 FROM table_name1 t WHERE t.column_name2 =:param "
)
public class Employee {
}

And have same method in the repository like:
@Repository
public interface TestRepository extends JpaRepository<Employee,Long>, TestRepositoryCustom {

    List<String> fetchParam(@Param("param") String param);
}
1) @查询(“从表_name1 t中选择t.column_name1,其中t.column_name2=:param”) 列表getValue(@Param(“Param”)字符串Param); 或 2) @Query(“从表_name1 t中选择t.column_name1,其中t.column_name2='param',nativeQuery=true) 列出getValue(); 或者,您可以使用NamedQuery并仅在模型中添加查询。 例如: @实体 @表(name=“Table\u name1”,schema=“db\u name”) @NamedQuery(name=“ENTITY.fetchParam”, query=“从表\u name1 t中选择t.column\u name1,其中t.column\u name2=:param” ) 公营雇员{ } 并在存储库中使用相同的方法,如: @存储库 公共接口TestRepository扩展了JpaRepository、TestRepositoryCustom{ 列表fetchParam(@Param(“Param”)字符串Param); }
TestRepository
中,您可以使用
选择NEW
并将DTO传递给对象:

@Query("SELECT NEW com.app.domain.EntityDTO(table.id, table.col2, table.col3)
FROM Table table")
public List<EntityDTO> findAll();
@Query(“选择NEW com.app.domain.EntityDTO(table.id、table.col2、table.col3))
从表格中选择“)
公共列表findAll();
在本例中,DTO必须具有具有这3个参数的构造函数

如果要选择属性,只需使用:

@Query("SELECT table.id)
FROM Table table")
public List<Long> findAllIds();
@Query(“选择table.id)
从表格中选择“)
公共列表findAllIds();

谢谢。我以前使用过这个方法,但后来DTO(不是JPA实体)和原语值(整数、双精度等)出现了问题。1) 如果我想要聚合数据(使用自定义查询搜索平均值、最大值、最小值、中值)并返回原语类型,我如何实现这一点?2) 如果我想使用自定义查询从数据库中的不同表中提取数据,并将它们组合到一个DTO中,我如何实现这一点?这就是我在这里问这个问题的主要原因。:)