如何从 Spring Boot 项目中的子子表中检索数据?
Posted
技术标签:
【中文标题】如何从 Spring Boot 项目中的子子表中检索数据?【英文标题】:How do I retrive data from sub chield table in my Spring Boot Project? 【发布时间】:2021-08-31 17:00:15 【问题描述】:我有两个实体“User”和“UserOtp”,它们具有一对多的关系。我想在一张表中显示它们,我的用户将显示所有字段,并且只有一个字段来自“UserOtp”。以下是详细代码。
“用户”
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "users")
public class Users
@Id
@Column(name = "userid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonIgnore
private int userid;
private String msisdn;
private String userPin;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "iduser_role")
private UserRole roleId;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "ud_id")
private DeviceProfile deviceProfile;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "pp_id")
private PersonalProfile personalProfile;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "pa_id")
private PersonalAccount personalAccount;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "userid")
List<UserOtp> userOtp;
@JsonIgnore
private int status;
@JsonIgnore
private Date createDate;
@JsonIgnore
private Date updateDate;
public Users()
public Users(String msisdn, String userPin, UserRole roleId, DeviceProfile deviceProfile,
PersonalProfile personalProfile, PersonalAccount personalAccount, List<UserOtp> userOtp, int status,
Date createDate, Date updateDate)
super();
this.msisdn = msisdn;
this.userPin = userPin;
this.roleId = roleId;
this.deviceProfile = deviceProfile;
this.personalProfile = personalProfile;
this.personalAccount = personalAccount;
this.userOtp = userOtp;
this.status = status;
this.createDate = createDate;
this.updateDate = updateDate;
--- Getter and Setter ---
“用户OTP”
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_otp")
public class UserOtp
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "iduser_otp")
private int iduserOtp;
private String otp;
private Date createDate;
private Date updateDate;
private int status;
public UserOtp()
public UserOtp(String otp, Date createDate, Date updateDate, int status)
super();
this.otp = otp;
this.createDate = createDate;
this.updateDate = updateDate;
this.status = status;
------ Getter and Setter -----
“控制器”
@RequestMapping("/admin/index")
public String Login(Model model)
long countUsers = userRepositoryService.countUsers();
Users user = userRepositoryService.getUsers(1);
List<Users> users = userRepositoryService.getUsers();
model.addAttribute("usersCount", countUsers);
model.addAttribute("users", users);
System.out.println(user.getUserOtp().toString());
return "/admin/index";
百里香页面
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>OTP</th>
<th>Gen. Time</th>
<th>Update Time</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr class="odd gradeX" th:each="user : $users">
<td th:text="$user.userid">01</td>
<td th:text="$user.msisdn">01791631664</td>
<td th:text="$user.userOtp">6566</td>
<td th:text="$user.CreateDate">July 12, 2012 12:13:00 PM</td>
<td th:text="$user.UpdateDate">July 12, 2012 12:13:00 PM</td>
<th th:text="$user.status">Active</th>
</tr>
</tbody>
</table>
我得到的输出如下:
我只想显示“otp”字段的最后一个数据。
【问题讨论】:
缺少实际输出。 【参考方案1】:您可以使用句点(“.”)字符在 Spring EL 中导航对象。
因此,在您的情况下,如果您只想显示状态字段,您可以使用:
<td th:text="$user.userOtp.status">6566</td>
有关更多信息,您可以查看Spring MVC & Thymeleaf 文档的第 1 部分以了解 Spring EL。
此外,Spring documentation 的第 4.3.2 章也很有用,其中描述了如何访问嵌套属性以及如何使用安全导航。
【讨论】:
以上是关于如何从 Spring Boot 项目中的子子表中检索数据?的主要内容,如果未能解决你的问题,请参考以下文章
mySQL 从一个表中选择,该表在另一个表中不存在,并且不是第三个表中的子表
如何使用 Java Spring Boot 在不插入新值的情况下更新表中的现有值