繁体   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