在SQL中,使用字符串连接进行参数化查询非常简单。例如,我根据预设参数从DB表中获取必要的列(带有列'名称的字符串)
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();
在这个查询中,我得到了整个表格。
答案 0 :(得分:1)
最后,我找到了答案。解决方案位于此页面: 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;
});
}
}