gpt4 book ai didi

java - 如何从spring boot jpa的两个表中获取数据

转载 作者:行者123 更新时间:2023-12-02 03:04:18 24 4
gpt4 key购买 nike

我有 user_infotopics 表,如下所示:

包含列的 user_info 表:

id, username, userImage, token, role

包含列的主题表:

id, topicId, title, details, dayPosted, username, userImage

用户登录时,我想从 topics 表获取信息,并从 user_role 表获取 role 信息。

目前我正在获取这样的数据,但这不包括角色信息。

@RequestMapping(path = "/get_data_on_login", method = RequestMethod.GET)
public ResponseEntity get_data_on_login(@RequestParam(value="username") String username) throws Exception {
List<TopicBean> topicBean = topicService.findAllTopics();
return new ResponseEntity(topicBean, HttpStatus.OK);
}

如何从 user_role 表以及上述数据中获取用户角色?

最佳答案

有什么问题吗:

@RequestMapping(path = "/get_data_on_login", method = RequestMethod.GET)
public ResponseEntity get_data_on_login(
@RequestParam(value="username") String userName) throws Exception {
List<TopicBean> topics = topicService.findAllTopics( userName );
List<UserRoleBean> roles = roleService.findAllRoles( userName );
return new ResponseEntity( new LoginData( topics, roles ), HttpStatus.OK );
}

您的LoginData类将是:

public class LoginData {
private final List<TopicBean> topics;
private final List<UserRoleBean> roles;

public LoginData(List<TopicBean> topics, List<UserRoleBean> roles) {
this.topics = topics;
this.roles = roles;
}

public List<TopicBean> getTopics() { return topics; }
public List<UserRoleBean> getRoles() { return roles; } }
}

您将得到如下所示的 JSON 响应:

{ "topics": [{topic1},{topic2}], "roles": [{role1},{role2}] }

关于java - 如何从spring boot jpa的两个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41965190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com