簡體   English   中英

spring 啟動,JpaRepository 自定義查詢內部連接和查看 thymeleaf,連接列未顯示

[英]spring boot, JpaRepository Custom query inner join and view on thymeleaf, joined column is not showing

表 - > UserProfile(user_id,username,status_id)

表 -> 狀態(status_id,名稱)

status_id 是表之間的外鍵

這是 JpaRepository

@Repository
public interface RepoUserProfile extends JpaRepository<UserProfile, Long> {  
    @Query("SELECT u,s.name FROM UserProfile u INNER JOIN Status s on s.status_id=u.status_id")
    public List<UserProfile> listUserProfile();

}

在這里它已被添加到服務中

@Service
public class ServiceUserProfile {
    @Autowired
    private RepoUserProfile repo;

    public List<UserProfile> listUserProfile() {
        return repo.listUserProfile();
    }
}

這是 controller

@Controller
public class UserController {

    @Autowired
    private ServiceUserProfile servicerepo;

    @RequestMapping("/user/list")
    public String Index(Model model) {
        List<UserProfile> listUserProfile = servicerepo.listUserProfile();
        model.addAttribute("listUser", listUserProfile);
        return "user/index";
    }
} 

這里我想在 index.html 中查看

<tbody>
    <tr th:each="litOfUser: ${listUser}">
        <td th:text="${litOfUser.username}"></td>
        <td th:text="${litOfUser.name}"></td>  // the name is not showing
    </tr>
</tbody>

為什么當我刪除它時沒有顯示 listofUser.name 系統工作正常但如果我保留它則不會。

表示我無法通過 {status_id} 的外鍵查看從表 STATUS 加入的 {name}

謝謝

在使用 jpa 等技術時,最好考慮實體而不是數據庫中的傳統表。 在您的實體 UserProfile 中,您應該具有具有適當 jpa 關系映射的實體狀態。 假設一個UserProfile只能有一個Status並且一個Status可以低於一個UserProfile ,它將如下所示。

@OneToOne
@JoinColumn(name="status_id")
Status status;

在您的存儲庫中,您將返回一個UserProfile列表,並且 jpa 正在忽略status中的name 您必須創建一個 DTO 並向其中添加來自UserProfileStatus的數據。

示例 DTO:

package com.example.projectx;

@Getter // from lombok
public class UserDto{
    private String name;
    private UserProfile userProfile;

    public UserDto(UserProfile userProfile, String name){
        this.userProfile = userProfile;
        this.name = name;
    }
}

現在在您的存儲庫中,您的查詢將如下所示:

@Repository
public interface RepoUserProfile extends JpaRepository<UserProfile, Long> {  
    @Query("SELECT new com.example.projectx.UserDto(u,s.name) FROM UserProfile u INNER JOIN u.status s")
    public List<UserDto> getUserDtos();
}

在上面的select語句中,我正在調用 DTO 的構造函數,它采用UserProfileString 需要 DTO class 的完全限定名稱。

根據需要重構服務;

@Service
public class ServiceUserProfile {
    @Autowired
    private RepoUserProfile repo;

    public List<UserDto> getUserDtos() {
        return repo.getUserDtos();
    }
}

重構 controller;

@Controller
public class UserController {

    @Autowired
    private ServiceUserProfile servicerepo;

    @RequestMapping("/user/list")
    public String Index(Model model) {
        List<UserDto> userDtos = servicerepo.getUserDtos();
        model.addAttribute("userDtos ", userDtos );
        return "user/index";
    }
} 

在 thymeleaf 模板中,它將類似於:

<tr th:each="userDto : ${userDtos }">
    <td th:text="${userDto.userProfile.username}"></td>
    <td th:text="${userDto.name}"></td>
</tr> 

如果它幫助您解決了您的問題並更好地了解所涉及的技術,請接受該答案。

快樂編碼!

暫無
暫無

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

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