如何在Spring MyBatis Mapper中编写两个寄生表?

时间:2017-11-26 04:54:15

标签: spring spring-mvc mybatis spring-mybatis

我目前正在实施评论评论。

当会员查看某个帖子时,他们应该能够看到评论和评论的评论。

要满足上述条件,您需要帖子的索引值和注释的索引值。也就是说,需要两个参数值。

所以我写了以下逻辑。

boardDaoMapper.xml

<select id="selectCommentOfComment" parameterType="java.util.HashMap" resultType="java.util.HashMap">
    select * from board_comment_of_comment where board_idx = #{board_idx} and board_comm_idx = #{board_comm_idx}
</select>

BoardDao代码:

public List<HashMap<String, Object>> selectCommentOfComment(int board_idx, int board_comm_idx);

BoardService:

@Override
public List<HashMap<String, Object>> getBoardCommentOfComment(int board_idx, int board_comm_idx) 
{
    System.out.println("ServieImple getBoardCommentOfComment Function Call >> " + board_idx + " / " + board_comm_idx);
    List<HashMap<String, Object>> c_of_c_list = boardDao.selectCommentOfComment(board_idx, board_comm_idx);
    return c_of_c_list;
}

和BoardController代码:

@RequestMapping("viewPage.do")
public String viewPage(HttpSession session,HttpServletRequest request, HttpServletResponse response,Model model, 
        @RequestParam(value="board_idx", defaultValue="1") int board_idx) throws IOException,IllegalStateException
{
    checkLogin(request, response, session);


    // 해당 게시물에 대한 댓글들의 idx값 조회 (board_comm_idx)
    List<HashMap<String, Object>> commentList= bService.getBoardForComment(boardData.getBoard_idx());

    System.out.println("Board_idx Value was : " + board_idx);

    // 해당 게시물에 대한 댓글들의 개수만큼 for문을 돌고..
    for(int i = 0; i < commentList.size(); i++)
    {
        // board_comm_idx 값을 변수에 저장
        int board_comm_idx= (Integer) commentList.get(i).get("board_comm_idx");
        List<HashMap<String, Object>> getCommentOfComentList =  
                    bService.getBoardCommentOfComment(board_idx, board_comm_idx);
        System.out.println("GetCommentOfCommentList : " + getCommentOfComentList);
        model.addAttribute("cocList", getCommentOfComentList);
    }

    return "viewPage";
}

值适用于两个参数。(在BoardService)就像那样......

ServieImple getBoardCommentOfComment Function Call >> 2 / 3

但是,这样做效果不佳并导致错误。

org.apache.ibatis.binding.BindingException: Parameter 'board_idx' not found. Available parameters are [0, 1, param1, param2]
    at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:165)
    at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:44)
    at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:116)
    at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:186)
    at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:129)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:76)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:108)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:358)
    at com.sun.proxy.$Proxy11.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:198)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
    at com.sun.proxy.$Proxy13.selectCommentOfComment(Unknown Source)
    at service.BoardServiceImple.getBoardCommentOfComment(BoardServiceImple.java:367)
    at controller.BoardController.viewPage(BoardController.java:175)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:180)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

我认为Mapper存在问题。

为什么会发生这种情况,我应该如何解决?

您的意见很有价值。请告诉我你的意见。

1 个答案:

答案 0 :(得分:2)

当前方法的问题

您当前的映射设置方式不接受您的参数进入DAO图层parameterType="java.util.HashMap"

在这种情况下,iBatis希望传递一个HashMap。

您可以通过多种方式修复代码。

1.)使用命名参数(取决于您的版本)&gt; 3

public List<HashMap<String, Object>> selectCommentOfComment(@Param("board_idx") int board_idx, @Param("board_comm_idx") int board_comm_idx);

从映射xml中删除参数类型。

2.。传递预期对象

您可以修复DAO方法签名以反映您的映射。并在服务中进行相应的更改。

DAO:

public List<HashMap<String, Object>> selectCommentOfComment(HashMap<String, Object> parameter);

SERVICE:

public List<HashMap<String, Object>> getBoardCommentOfComment(int board_idx, int board_comm_idx) 
{
   HashMap<String, Object> parameters = new HashMap<String, Object>();
   parameters.put("board_idx", board_idx);
   parameters.put("board_comm_idx", board_comm_idx");
   List<HashMap<String, Object>> c_of_c_list = boardDao.selectCommentOfComment(board_idx, board_comm_idx);
   return c_of_c_list;
}

您可以从他们的文档中获取额外的灵感。 http://www.mybatis.org/mybatis-3/sqlmap-xml.html