使用 Java 接口从本机查询获取结果
Get result from native query using Java Interface
我有这些 Postgres 表:
create table deals_new
(
id bigserial primary key,
slip_id text,
deal_type integer,
timestamp timestamp,
employee_id bigint
constraint employee_id_fk
references common.employees
);
create table twap
(
id bigserial primary key,
deal_id varchar not null,
employee_id bigint
constraint fk_twap__employee_id
references common.employees,
status integer
);
create table common.employees
(
id bigint primary key,
first_name varchar(150),
last_name varchar(150)
);
我创建了这个 JPA 存储库:
public interface DealsRepository extends JpaRepository<Employee, Long> {
@Query (value =
"SELECT e.first_name, e.last_name " +
"FROM common.deals_new d " +
"JOIN common.employees e ON e.id = d.employee_id " +
"LEFT OUTER JOIN common.twap t on " +
" t.deal_id = d.slip_id AND " +
" d.timestamp between '11-11-2010' AND '11-11-2011' AND " +
" d.deal_type in (1, 2) " +
"OFFSET :offset " +
"LIMIT :limit ",
nativeQuery = true)
List<IdsOnly> getHistoryAllPairsSearchParam(@Param("offset") int offset,
@Param("limit") int limit);
}
如您所见,我使用此接口获得了结果:
public interface IdsOnly {
String getFirstName();
String getLastName();
}
private DealsList getResponseParams(List<IdsOnly> deals) {
return new DealsList( // get here the values from the interface and convert them);
}
我不清楚我是如何从接口中获取值并进行一些处理的。你能给我一些建议吗?例如如何打印它们?
您必须在 select 子句中使用别名
SELECT e.first_name as firstName, e.last_name as lastName
它们对应于IdsOnly
接口中的getFirstName()
和getLastName()
方法名
public interface IdsOnly {
String getFirstName();
String getLastName();
}
我有这些 Postgres 表:
create table deals_new
(
id bigserial primary key,
slip_id text,
deal_type integer,
timestamp timestamp,
employee_id bigint
constraint employee_id_fk
references common.employees
);
create table twap
(
id bigserial primary key,
deal_id varchar not null,
employee_id bigint
constraint fk_twap__employee_id
references common.employees,
status integer
);
create table common.employees
(
id bigint primary key,
first_name varchar(150),
last_name varchar(150)
);
我创建了这个 JPA 存储库:
public interface DealsRepository extends JpaRepository<Employee, Long> {
@Query (value =
"SELECT e.first_name, e.last_name " +
"FROM common.deals_new d " +
"JOIN common.employees e ON e.id = d.employee_id " +
"LEFT OUTER JOIN common.twap t on " +
" t.deal_id = d.slip_id AND " +
" d.timestamp between '11-11-2010' AND '11-11-2011' AND " +
" d.deal_type in (1, 2) " +
"OFFSET :offset " +
"LIMIT :limit ",
nativeQuery = true)
List<IdsOnly> getHistoryAllPairsSearchParam(@Param("offset") int offset,
@Param("limit") int limit);
}
如您所见,我使用此接口获得了结果:
public interface IdsOnly {
String getFirstName();
String getLastName();
}
private DealsList getResponseParams(List<IdsOnly> deals) {
return new DealsList( // get here the values from the interface and convert them);
}
我不清楚我是如何从接口中获取值并进行一些处理的。你能给我一些建议吗?例如如何打印它们?
您必须在 select 子句中使用别名
SELECT e.first_name as firstName, e.last_name as lastName
它们对应于IdsOnly
接口中的getFirstName()
和getLastName()
方法名
public interface IdsOnly {
String getFirstName();
String getLastName();
}