mybatis示例

1.简单resultmap

<select id="selectBlog" parameterType="int" resultMap="blogResult">
    select
        B.id as blog_id,
        B.title as blog_title,
        B.author_id as blog_author_id,
        A.id as author_id,
        A.username as author_username,
        A.password as author_password,
        A.email as author_email,
        A.bio as author_bio
    From Blog B left outer join Author A on B.author_id = A.id
    where B.id = #{id}
</select>
<resultMap id="blogResult" type="Blog">
    <id property=”blog_id” column="id" />
    <result property="title" column="blog_title"/>
    <association property="author" column="blog_author_id"
    javaType="Author" resultMap=”authorResult”/>
</resultMap>
<resultMap id="authorResult" type="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
</resultMap>

或者:
<resultMap id="blogResult" type="Blog">
    <id property=”blog_id” column="id" />
    <result property="title" column="blog_title"/>
    <association property="author" column="blog_author_id"
        javaType="Author">
        <id property="id" column="author_id"/>
        <result property="username" column="author_username"/>
        <result property="password" column="author_password"/>
        <result property="email" column="author_email"/>
        <result property="bio" column="author_bio"/>
        </association>
</resultMap>

2集合的嵌套查询

    <resultMap type="Blog" id="Blog_result">
        <association property="author" column="author_id"
            javaType="Author" select="selectAuthorById" />

        <collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectBlogPost" />
    </resultMap>

    <select id="selectBlogPost" resultType="Post" parameterType="int">
        select * from Post where blog_id=#{id}
    </select>   

    <select id="selectAuthorById" parameterType="int" resultType="Author">
        select * from Author where id = #{id}
    </select>

    <!--
        select关联,建议在一对一的情况下使用。
        在此处,如果selectBlogById返回多个Blog,将会带来N+1问题
     -->
    <select id="selectBlogById" parameterType="int" resultMap="Blog_result">
        select * from Blog where id = #{id}
    </select>

3.集合的嵌套结果

<select id="selectBlog" parameterType="int" resultMap="blogResult">
    select
        B.id as blog_id,
        B.title as blog_title,
        B.author_id as blog_author_id,
        P.id as post_id,
        P.subject as post_subject,
        P.body as post_body,
    from Blog B
        left outer join Post P on B.id = P.blog_id
    where B.id = #{id}
</select>
<resultMap id="blogResult" type="Blog">
    <id property=”id” column="blog_id" />
    <result property="title" column="blog_title"/>
    <collection property="posts" ofType="Post">
        <id property="id" column="post_id"/>
        <result property="subject" column="post_subject"/>
        <result property="body" column="post_body"/>
    </collection>
</resultMap>
也可以这么写
<resultMap id="blogResult" type="Blog">
    <id property=”id” column="blog_id" />
    <result property="title" column="blog_title"/>
    <collection property="posts" ofType="Post" resultMap=”blogPostResult”/>
</resultMap>
<resultMap id="blogPostResult" type="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <result property="body" column="post_body"/>
</resultMap>

4.discriminator鉴别器

<resultMap id="vehicleResult" type="Vehicle">
    <id property=”id” column="id" />
    <result property="vin" column="vin"/>
    <result property="year" column="year"/>
    <result property="make" column="make"/>
    <result property="model" column="model"/>
    <result property="color" column="color"/>
    <discriminator javaType="int" column="vehicle_type">
        <case value="1" resultMap="carResult"/>
        <case value="2" resultMap="truckResult"/>
        <case value="3" resultMap="vanResult"/>
        <case value="4" resultMap="suvResult"/>
    </discriminator>
</resultMap>
<resultMap id="carResult" type="Car">
    <result property=”doorCount” column="door_count" />
</resultMap>

简洁风格

<resultMap id="vehicleResult" type="Vehicle">
    <id property=”id” column="id" />
    <result property="vin" column="vin"/>
    <result property="year" column="year"/>
    <result property="make" column="make"/>
    <result property="model" column="model"/>
    <result property="color" column="color"/>
    <discriminator javaType="int" column="vehicle_type">
        <case value="1" resultType="carResult">
            <result property=”doorCount” column="door_count" />
        </case>
        <case value="2" resultType="truckResult">
            <result property=”boxSize” column="box_size" />
            <result property=”extendedCab” column="extended_cab" />
        </case>
        <case value="3" resultType="vanResult">
            <result property=”powerSlidingDoor” column="power_sliding_door" />
        </case>
        <case value="4" resultType="suvResult">
            <result property=”allWheelDrive” column="all_wheel_drive" />
        </case>
    </discriminator>
</resultMap>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值