为什么要在Java中重复两次数据 [英] Why iterate data twice in java

查看:17
本文介绍了为什么要在Java中重复两次数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,所有工作都正常,但问题是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>

以下是我的输出:

以下是预期输出:

调试:

推荐答案

谢谢@andrewjames非常感谢您的建议。

已解决

我在控制器中有likeCount通过HashMap(K,V)的模型...

从父循环receiver_id()Liketh:text="${likeCount.get(ExtractListOfSubData.receiver_id)}"中删除<span th:each="data: ${likeCount}">迭代并直接获取HashMap的值...

更改:

<span th:each="data: ${likeCount}">
   <span class='like-count' th:id='like-count + ${ExtractListOfSubData.post_id}' th:text="${data}"></span>
</span>

收件人:

<span class='like-count' th:id='like-count + ${ExtractListOfSubData.post_id}' th:text="${likeCount.get(ExtractListOfSubData.receiver_id}"></span> 

输出:

这篇关于为什么要在Java中重复两次数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆