我想通过 "~"
将结果数据与查询分开在同一个实体中,用 ","
分隔另一个实体数据
我的代码
mainRepository.java
public interface mainRepository extends CrudRepository<Error, Long> {
@Query(value= "SELECT * FROM Error t where t.applicationID = :applicationid", nativeQuery= true)
List<Error> findListByApp(@Param("applicationid") String applicationid);
}
在另一个类中我调用该函数
String cb = errorRepository.findListByApp("application1").toString();
System.out.println(cb);
如果我执行cb的结果是
[com.info.main.Error@6ec8b40e, com.info.main.Error@6ec8b40e, com.info.main.Error@6ec8b40e]
我想先按应用程序排序,然后是名称,然后是电子邮件。
我想要达到的效果是这样的:
[app1~name1~email1, app2~name2~email2, app3~name3~email3]
最佳答案
为什么要对结果列表使用 toString?
String cb = errorRepository.findListByApp("application1").toString();//why toString?
相反,您必须使用这样的列表:
List<Error> result = errorRepository.findListByApp("application1");
//this will print the list or Error it will call to String for each error
System.out.println(result);
要显示结果,您只需使用您想要的格式覆盖错误实体中的 toString
方法即可。
@Entity
public class Error {
private String app;
private String name;
private String email;
//getter setter
@Override
public String toString() {
return app + "~" + name + "~" + email;
}
}
关于java - 如何使用java在Repository Spring Boot中执行将由特定符号分隔的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50798785/