简体   繁体   English

spring 启动,JpaRepository 自定义查询内部连接和查看 thymeleaf,连接列未显示

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

table - > UserProfile(user_id,username,status_id)表 - > UserProfile(user_id,username,status_id)

table - > Status (status_id, name)表 -> 状态(status_id,名称)

the status_id is foreign key between tables status_id 是表之间的外键

this is the JpaRepository这是 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();

}

here it's been added to the service在这里它已被添加到服务中

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

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

this is the controller这是 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";
    }
} 

here i want view in the index.html这里我想在 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>

why the listofUser.name is not showing when i remove this the system is working fine but not if i keep it.为什么当我删除它时没有显示 listofUser.name 系统工作正常但如果我保留它则不会。

means i can not view the {name} which is join from the table STATUS by the foreign key of {status_id}表示我无法通过 {status_id} 的外键查看从表 STATUS 加入的 {name}

thanks谢谢

When using technologies like jpa, it is best to think of entities instead of traditional tables found in the database.在使用 jpa 等技术时,最好考虑实体而不是数据库中的传统表。 In your entity UserProfile, you should have the entity Status with the appropriate jpa relationship mapping.在您的实体 UserProfile 中,您应该具有具有适当 jpa 关系映射的实体状态。 It will look something like below assuming one UserProfile can have only one Status and one Status can below to one UserProfile .假设一个UserProfile只能有一个Status并且一个Status可以低于一个UserProfile ,它将如下所示。

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

In your repository, you're returning a list of UserProfile and jpa is ignoring name from status .在您的存储库中,您将返回一个UserProfile列表,并且 jpa 正在忽略status中的name You must create a DTO and add data from UserProfile and Status to it.您必须创建一个 DTO 并向其中添加来自UserProfileStatus的数据。

Sample DTO:示例 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;
    }
}

Now in your repository, your query will look like:现在在您的存储库中,您的查询将如下所示:

@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();
}

In the select statement above, I am invoking the constructor for the DTO which takes UserProfile and a String .在上面的select语句中,我正在调用 DTO 的构造函数,它采用UserProfileString The fully qualified name of the DTO class is required.需要 DTO class 的完全限定名称。

Refactor the service as needed;根据需要重构服务;

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

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

Refactor controller;重构 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";
    }
} 

In the thymeleaf template, it will be something like:在 thymeleaf 模板中,它将类似于:

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

Please accept the answer if it has helped you resolved your issue and have a better understanding of the technologies involved.如果它帮助您解决了您的问题并更好地了解所涉及的技术,请接受该答案。

Happy coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM