【发布时间】:2022-04-20 14:13:38
【问题描述】:
在我的程序中一切正常,但问题是total like 为一个用户获得两次我尝试正确迭代但我没有成功我可以尝试从 2 天开始解决这个问题...
我的输出:
个人资料(1):1 2 喜欢 |个人资料(2):1 2 个赞
预期输出:
个人资料(1):1 个赞 |个人资料(2):2 个赞
存储库:
// all query work fine
public interface postlikeRepo extends JpaRepository<Likepost, Integer>{
@Query(nativeQuery = true, value = "SELECT COUNT(*) FROM like_master WHERE post_id = ?")
public int getTotalLike(Integer Id);
}
public interface requestRepo extends JpaRepository<Request, Integer> {
@Query(nativeQuery = true, value="SELECT * FROM request_master WHERE sender_id = ? AND status = ?")
List<requestEntity> getAcceptRequestFrnd(Integer Sender_id, String Status);
@Query(nativeQuery = true, value="SELECT rgm.u_id,pm.profile, rsm.sender_id, rgm.username, upm.post_id, upm.post, upm.date FROM registration_master AS rgm INNER JOIN profile_master AS pm ON rgm.u_id = pm.user_id INNER JOIN uploadpost_master AS upm ON rgm.u_id = upm.user_id INNER JOIN request_master AS rsm ON rgm.u_id = rsm.receiver_id WHERE rsm.receiver_id = ? ORDER BY upm.date DESC LIMIT 1")
List<ProfileDto> getPostWithAccount(Integer Receiver_id);
}
服务:
@Service
public class pojoServiceImpl implements pojoService {
@Autowired
private requestRepo requestRepo;
@Autowired
private postlikeRepo postlikeRepo;
// get user with newest post
@Override
public List<ProfileDto> getPostWithAccount(Integer Receiver_id) {
return this.requestRepo.getPostWithAccount(Receiver_id);
}
// get totle like
@Override
public int getTotalLike(int Id) {
return this.postlikeRepo.getTotalLike(Id);
}
}
控制器:
@Controller
public class meetzenController {
@Autowired
private pojoService pojoService;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model mdl, HttpSession session, Request Request){
Integer SessionId = Integer.parseInt(session.getAttribute("Userid").toString());
Map<Integer, List<ProfileDto>> ListOfPost = new HashMap<>();
Map<Integer, Integer> LikeCount = new HashMap<>();
List<Request> GetUser = this.pojoService.getAcceptRequestFrnd(SessionId, "Accept");
for(Request GetUserForPost : GetUser)
{
List<ProfileDto> GetUserWithPost = this.pojoService.getPostWithAccount(GetUserForPost.getReceiver_id());
for(ProfileDto GetLikeCount: GetUserWithPost)
{
Integer GetTotalLike = this.pojoService.getTotalLike(GetLikeCount.getPost_id());
// Problem Here
LikeCount.put(GetTotalLike, GetLikeCount.getPost_id());
}
ListOfPost.put(GetUserForPost.getReceiver_id(), GetUserWithPost);
}
mdl.addAttribute("likeCount", LikeCount);
mdl.addAttribute("ListOfPost", ListOfPost);
return "post";
}
}
百里香:
<div th:each="ExtractListOfData :${ListOfPost}">
<div th:each="ExtractListOfSubData: ${ExtractListOfData.value}">
<div class="media">
<div class="row js-masonry"
data-masonry='{ "itemSelector": ".grid-item", "columnWidth": ".grid-sizer", "percentPosition": true }'>
<div class="grid-item col-md-12 col-sm-12">
<div class="post clearfix">
<div class="media-grid">
<div class="user-info">
<a th:href="@{/profile/{uid}(uid=${ExtractListOfSubData.u_id})}">
<img th:src="${ExtractListOfSubData.profile}" alt=" " class="profile-photo-sm pull-left" />
</a>
<div class="user">
<h6>
<a th:href="@{/profile/{uid}(uid=${ExtractListOfSubData.u_id})}" class="profile-link" th:text="${ExtractListOfSubData.username}"></a>
</h6>
</div>
</div>
<div class="img-wrapper" data-toggle="modal" data-target=".modal-1">
<img th:src="${ExtractListOfSubData.post}" alt=" " class="img-responsive post-image" />
</div>
<div class="media-info">
<div class="reaction">
<div class="button-toggle">
<a type='submit' class='like-button-style' th:id="'unlike' + ${ExtractListOfSubData.post_id}" th:onclick="'btnunlike('+${ExtractListOfSubData.post_id}+')'">
<img src="https://img.icons8.com/material-outlined/24/000000/like--v1.png"/>
</a>
</div>
<button type="submit" class="comment">
<i class="fa fa-comment-o"></i>
</button>
<div class='like-style'>
<!-- My problem is here -->
<span th:each="data: ${likeCount}">
<span class='like-count' th:id='like-count + ${ExtractListOfSubData.post_id}' th:text="${data}"></span>
</span>
<label>like</label>
</div>
<div class="row comment-top">
<div class="col-sm-12">
<div class="comment-box">
<input type="text" class="Comment-Textbox"
autocomplete="off" placeholder="Add your comment..." />
<input type="submit" class="postComment-button blur"
disabled="disabled" value="Post" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
这是我的输出:
这是预期的输出:
调试:
【问题讨论】:
-
如果你在你的
select中使用group by? -
我认为您的 Thymeleaf 代码存在问题,因为模型数据并不像应有的那么简单。您的模型(传递给 Tymeleaf 模板的数据)由两个独立的
Maps 组成。相反,您的 Java 代码应该构建一个单独的List- 其中列表中的每个条目都是一个对象,其中仅包含一张卡片所需的数据(图片、“喜欢”计数等。然后。迭代Thymeleaf 变得简单。更进一步,也许您可以返回已经从存储库中聚合的数据 - 这样 Java 要做的工作就更少了。 -
对不起,我不明白您的评论与我的建议有何关联。
-
我仍然不确定我是否理解。我的观点是:您应该能够使用 Java 将两个地图(包含您需要的所有内容)中的数据合并到一个列表中。两张地图中的数据之间必须存在隐含关系 - 否则无论如何您想在问题中使用的基于 Thymeleaf 的方法都是不可能的。
-
好的,明白了,谢谢。但如果这是真的,那么 Thymeleaf 也无法解决你的问题。
标签: java spring-boot thymeleaf