在Spring Boot中使用投影和自定义SQL



我的问题是,我想选择一个特定的集合从我的数据库与Spring Boot, Crudrepository和Dto对象。

因此,我读了很多文章,如何做到这一点,我认为这可能与Postgres有关?

这是错误信息:

org.postgresql.util。PSQLException: ERROR:语法错误在"("位置:29日

这是我的方法:

Dto对象

@AllArgsConstructor
@NoArgsConstructor
@Data
public class IncomeOutgoTotal {
String person;
float gesamt;
String location;
}

@Repository
public interface IncomeOutgoTotalRepository extends CrudRepository<IncomeOutgo, Long> {
@Query(value = "select new IncomeOutgoTotal (PERSON, SUM(INCOME)-SUM(OUTGO), LOCATION) from incomeoutgo where location = 'Food' group by person, location", nativeQuery = true)
List<IncomeOutgoTotal> getTotal();
}

在我的存储库中,我也尝试使用小写字母的字段,但到目前为止没有任何帮助。

也许有人能帮我一下。

提前感谢。

我现在能够解决我的问题,并且它与CrudRepository一起工作。互联网上的其他人也是这样做的,要么使用CrudRepository,要么使用jparerepository。

这是我的解决方案

public interface IncomeOutgoTotalRepository extends CrudRepository<IncomeOutgo, Long> {
@Query(value = "select new at.co.netconsulting.incomesandexpenses.domain.IncomeOutgoTotal (person, SUM(income)-SUM(outgo) as total, location) from IncomeOutgo where location = 'Food' group by person, location")
List<IncomeOutgoTotal> getTotal();
}
@AllArgsConstructor
@NoArgsConstructor
@Data
public class IncomeOutgoTotal {
String person;
double total;
String location;
}

Dto类成员和@Query字段必须以相同的方式编写,并且包也必须写在Repository中。

然后成功了。

最新更新