自定义ResultMap查询,这里的关联写法只能用于不分页

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.sang.mapper.MenuMapper">
    <resultMap id="BaseResultMap" type="org.sang.bean.Menu">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="url" property="url" jdbcType="VARCHAR"/>
        <result column="path" property="path" jdbcType="VARCHAR"/>
        <result column="component" property="component" javaType="java.lang.Object"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="iconCls" property="iconCls" jdbcType="VARCHAR"/>
        <result column="keepAlive" property="keepAlive" jdbcType="BIT"/>
        <result column="parentId" property="parentId" jdbcType="INTEGER"/>
        <association property="meta" javaType="org.sang.bean.MenuMeta">
            <result column="keepAlive" property="keepAlive"/>
            <result column="requireAuth" property="requireAuth"/>
        </association>
        <collection property="roles" ofType="org.sang.bean.Role">
            <id column="rid" property="id"/>
            <result column="rname" property="name"/>
            <result column="rnamezh" property="nameZh"/>
        </collection>
        <collection property="children" ofType="org.sang.bean.Menu">
            <id column="id2" property="id"/>
            <result column="path2" property="path" jdbcType="VARCHAR"/>
            <result column="component2" property="component" jdbcType="VARCHAR"/>
            <result column="name2" property="name" jdbcType="VARCHAR"/>
            <result column="iconCls2" property="iconCls" jdbcType="VARCHAR"/>
            <association property="meta" javaType="org.sang.bean.MenuMeta">
                <result column="keepAlive2" property="keepAlive"/>
                <result column="requireAuth2" property="requireAuth"/>
            </association>
            <collection property="children" ofType="org.sang.bean.Menu">
                <id column="id3" property="id"/>
                <result column="name3" property="name" jdbcType="VARCHAR"/>
            </collection>
        </collection>
    </resultMap>
    <select id="getAllMenu" resultMap="BaseResultMap">
        select m.*,r.`id` as rid,r.`name` as rname,r.`nameZh` as rnamezh from menu m left join menu_role mr on m.`id`=mr.`mid` left join role r on mr.`rid`=r.`id` WHERE m.`enabled`=true order by m.`id` desc
    </select>
    <select id="getMenusByHrId" parameterType="Long" resultMap="BaseResultMap">
        select m1.`id`,m1.`path`,m1.`component`,m1.`iconCls`,m1.`name`,m1.`requireAuth`,m2.`component` as component2,m2.`iconCls` as iconCls2,m2.`keepAlive` as keepAlive2,m2.`name` as name2,m2.`path` as path2,m2.`requireAuth` as requireAuth2 from menu m1,menu m2 where m1.`id`=m2.`parentId` and m1.`id`!=1 and m2.`id` in(select mr.`mid` from hr_role h_r,menu_role mr where h_r.`rid`=mr.`rid` and h_r.`hrid`=#{hrId}) and m2.`enabled`=true order by m1.`id`,m2.`id`
    </select>
    <select id="menuTree" resultMap="BaseResultMap">
            select m1.`id`,m1.`name`,m2.`id` as id2,m2.`name` as name2,m3.`id` as id3,m3.`name` as name3 from menu m1,menu m2,menu m3 where m1.`id`=m2.`parentId` and m2.`id`=m3.`parentId` and m3.`enabled`=true
        </select>
    <select id="getMenusByRid" resultType="Long">
        SELECT mid from menu_role WHERE rid=#{rid}
    </select>
</mapper>

  

posted on 2019-09-05 15:25  小帅豹  阅读(386)  评论(0编辑  收藏  举报