gpt4 book ai didi

java - 带参数的QueryDSL请求,可以吗?

转载 作者:行者123 更新时间:2023-11-29 13:22:44 25 4
gpt4 key购买 nike

在 SQL 中,使用字符串连接进行参数化查询非常简单。例如,我根据预设参数(带有列名称的字符串)从数据库表中获取必要的列

String id_column = "id", city_column = "city", streetname_column ="streetname", housenumber_column = "housenumber"; //Parameters
stmt = c.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT "+id_column+","+streetname_column+" FROM address" ); //How do the same in QueryDSL?
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 0; i < rsmd.getColumnCount(); i++){
System.out.print(rsmd.getColumnName(i+1)+" ");
}
while ( rs.next() ) {
System.out.print("\n");
for (int i = 0; i < rsmd.getColumnCount(); i++){
System.out.print(
rs.getString(rsmd.getColumnName(i+1)) + " "
);
}
}

我怎样才能用这个做同样的事情(你也可以做你自己的例子)QueryDSL 请求:

List<Address> q1=queryFactory.selectFrom(address)
.fetch();

在这个查询中,我得到了整个表。

最佳答案

终于找到答案了。解决方案位于此页面: Examples of using play.db.jpa.JPAApi

根据我的问题,解决方案如下:

package controllers;
import play.db.jpa.JPAApi;
import play.db.jpa.Transactional;
import javax.inject.Inject;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

public class TestingReportsDep{

private JPAApi jpaApi;
@Inject
public TestingReportsDep(JPAApi jpaApi) {
this.jpaApi = jpaApi;
}
Object[] tempObj;

@Transactional
public void resolving_requests) {
String id_column = "id", city_column = "city", streetname_column ="streetname", housenumber_column = "housenumber"; //Parameters
String request="SELECT "+id_column+","+streetname_column+" FROM address;";
jpaApi.withTransaction(entityManager -> {
Query query = entityManager.createNativeQuery(request);
int ResultsAmount = query.getResultList().size();
List Result = query.getResultList();
System.out.print("\n\n\nNew super query by Bogdan:\n");

for(int i=0; i< ResultsAmount; i++){
tempObj = (Object[]) Result.get(i);

System.out.print(tempObj[0]+" "+tempObj[1]+"\n");
}
return null;
});
}
}

关于java - 带参数的QueryDSL请求,可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com