簡體   English   中英

如何使用Java在存儲庫Springboot中執行將由某些符號分隔的查詢

[英]How to execute query that will separate by certain symbol in Repository Springboot using java

我想在同一實體中用"~"分隔帶有查詢的結果數據,用","分隔另一個實體數據

我的密碼

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:

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);

為了顯示結果,您只需要使用所需的格式來覆蓋Error實體中的toString方法。

@Entity
public class Error {

    private String app;
    private String name;
    private String email;

    //getter setter

    @Override
    public String toString() {
        return app + "~" + name + "~" + email;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM