复杂的联合查询,mybatis的例子。

举例一个复杂的查询mybatis,在mybatis的关联属性里,可以级联关联。在关联属性里,主association里的property是类的引用字段。再在里面的id是数据库查询的列id,对应的是新类的属性。result的列也是数据库的列名,对应的属性也是新子类的属性。

内容表,需要作者表,和爻表--》和卦表,爻表又和卦表联合,查询出卦的中文名字,在这个联合查询中,用left jion不好用,用联合查询好用。

mybatis的xml文件如下

<resultMap type="cn.taotao.bean.ZhouyiContent" id="WithAuthorYaoResultMap">
      <id column="aid" jdbcType="INTEGER" property="id" />
    <result column="orderId" jdbcType="INTEGER" property="orderId" />
    <result column="Content" jdbcType="VARCHAR" property="content" />
    <result column="info" jdbcType="VARCHAR" property="info"/>
    
    <!-- 指定联合查询出的作者和爻的封装 -->
    <association property="zhouyiAuthor" javaType="cn.taotao.bean.ZhouyiAuthor">
        <id column="bid" property="id"/>
        <result column="bname" property="name"/>
        
    </association>
      
    <association property="zhouyiYao" javaType="cn.taotao.bean.ZhouyiYao" >
        <id column="cid" property="id"></id>
        <result column="yaoId" property="yaoId"></result>

        <association property="zhouyiIndex"  javaType ="cn.taotao.bean.ZhouyiIndex">
            <id column = "did" property="id"/>
            <result column ="dname" property="name"></result>
        </association>
    </association>
 </resultMap>
 

<select id="getZhouyiContentsByAuthor" resultMap="WithAuthorYaoResultMap" > select a.id aid,a.orderId,a.authorId,a.zhouyiId,a.content,a.info, b.id bid,b.name bname, c.id cid,c.yaoId,c.yaoIndex, d.id did,d.name dname from tbl_content a, tbl_author b ,tbl_yao c ,tbl_index d where a.authorId = b.id and a.zhouyiId = c.id and c.yaoIndex = d.id and a.authorId = #{zhouyiAuthor.id,jdbcType=INTEGER} </select>

dao层

public List<ZhouyiContent> getZhouyiContentsByAuthor(Integer authorId);

controller层

    @RequestMapping("getContentsByAuthor/{authorId}")
    public ModelAndView getContentsByAuthor(@PathVariable("authorId") Integer authorId,@RequestParam(value="pn",defaultValue = "1") Integer pn) {
        ModelAndView mv = new ModelAndView();
        PageHelper.startPage(pn, 12);
        List<ZhouyiContent> zhouyiContentsByAuthor = zhouyiContentService.getZhouyiContentsByAuthor(authorId);
        PageInfo page = new PageInfo<ZhouyiContent>(zhouyiContentsByAuthor, 7);
        mv.addObject("pageinfo", page);
        mv.setViewName("zhouyiContent");
        return mv;
    }

service层

public List<ZhouyiContent> getZhouyiContentsByAuthor(Integer authorId){
         return zhouyiContentMapper.getZhouyiContentsByAuthor(authorId);
    }

jsp页面

<c:forEach items="${pageinfo.list }" var="zhouyiContent">
                <tr>
                    <td>${zhouyiContent.id }</td>
                    <td>${zhouyiContent.orderId }</td>
                    <td>${zhouyiContent.zhouyiYao.zhouyiIndex.name }</td>
                    <td>${zhouyiContent.zhouyiYao.yaoId }</td>
                    <td>${zhouyiContent.zhouyiAuthor.name }</td>
                    <td>${zhouyiContent.content }</td>
                    <td>${zhouyiContent.info }</td>
                    <td><a href="${APP_PATH }/zhouyiContentModify/${zhouyiContent.id}"><button type="button" class="btn btn-primary btn-xs btn_edit" edit_id="${zhouyiContent.id }">修改</button></a>
                        <a href="${APP_PATH }/zhouyiContentDel/${zhouyiContent.id}"><button type="button" class="btn btn-primary btn-xs" onclick="return confirm('确定要删除吗?')">删除</button></a></td>
                </tr>
            </c:forEach>

 

posted @ 2020-01-15 15:03  琴声清幽  阅读(778)  评论(0编辑  收藏  举报