• mybatis关联三张表查询对应字段名


    Mapper.xml
    //查询总记录数
    <select id="getPagerList" parameterType="java.util.Map" resultMap="map">
    SELECT
    <choose>
    <!--查询总记录数-->
    <when test="iscount!=null and iscount!=''">
    COUNT(*) id
    </when>
    <otherwise>
    c.*,
    cs.name as cname,
    i.realname as iname,
    s.realname as sname
    </otherwise>
    </choose>
    FROM comment c
    LEFT JOIN course cs ON cs.id = c.cid
    LEFT JOIN instructor i ON i.id = c.iid
    LEFT JOIN student s ON s.id = c.sid
    WHERE 1=1

    <if test="cid != null and cid != ''">
    AND c.cid like concat('%', #{cid}, '%')
    </if>
    <if test="iid != null and iid != ''">
    AND c.iid like concat('%', #{iid}, '%')
    </if>
    <if test="sid != null and sid != ''">
    AND c.sid like concat('%', #{sid}, '%')
    </if>

    <!--条件查询-->
    <select id="getAllWhere" parameterType="java.util.Map" resultMap="map">
    SELECT
    c.*,
    cs.name as cname,
    i.realname as iname,
    s.realname as sname
    FROM comment c
    LEFT JOIN course cs ON cs.id = c.cid
    LEFT JOIN instructor i ON i.id = c.iid
    LEFT JOIN student s ON s.id = c.sid
    WHERE 1=1

    <if test="cid != null and cid != ''">
    AND c.cid like concat('%', #{cid}, '%')
    </if>
    <if test="iid != null and iid != ''">
    AND c.iid like concat('%', #{iid}, '%')
    </if>
    <if test="sid != null and sid != ''">
    AND c.sid like concat('%', #{sid}, '%')
    </if>
    </select>

    <resultMap id="map" type="cn.educate.model.commentModel" autoMapping="true">
    <association property="course" javaType="cn.educate.model.courseModel">
    <result property="name" column="cname" />
    </association>
    <association property="instructor" javaType="cn.educate.model.instructorModel">
    <result property="realname" column="iname"/>
    </association>
    <association property="student" javaType="cn.student.model.studentModel">
    <result property="realname" column="sname"/>
    </association>
    </resultMap>





  • 相关阅读:
    源码学习之Yii-去掉magic_quote里的反斜线
    PHP中传递回调函数的方法
    mac里的terminal环境下如何跳转行首和行末
    mac下切换输入法
    nginx上配置vhosts
    MySQL学习之查询优化(一)
    MySQL学习之索引(三)
    在LINUX下为自己加上sudo权限的方法
    MySQL学习之索引(二)
    MySQL学习之索引(一)
  • 原文地址:https://www.cnblogs.com/wuqiaoqun/p/13705990.html
Copyright © 2020-2023  润新知