mybatis关联查询xml文件简写,复用BaseResultMap和sql

关联查询班级和班主任 association 或者学生 collection

*:利用<include> property属性自动配置别名,映射结果由columnPrefix匹配。 也可手动全部写出[别名_字段]替换该标签中的内容,

 和关联查询标签中的 columnPrefix="stu_" 属性,达成复用。

一、班级clz

<?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="cc.ash.ClzMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cc.ash.Clz">
        <id column="ID" property="id"/>
        <result column="CLZ_NO" property="clzNo"/>
        <result column="CLZ_NAME" property="ClzName"/>
    </resultMap>

    <resultMap id="ClzStuResultMap" type="com.asion.business.module.station.model.Station" extends="BaseResultMap">
        <collection property="stu" resultMap="cc.ash.StuMapper.BaseResultMap"
            columnPrefix="stu_"></association>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID, CLZ_NO, CLZ_NAME
    </sql>

    <!--  <include> 利用property属性自动配置别名,映射结果由columnPrefix匹配。
    也可手动全部写出[别名_字段]替换该标签中的内容 -->
    <select id="getClzStu" resultMap="ClzStuResultMap">
        SELECT
            clz.*
             ,
        <include refid="cc.ash.StuMapper.Alias_Column_List">
            <property name="alias" value="stu"/>
            <property name="alias_" value="stu_"/>
        </include>
        FROM
            tbl_clz clz
        LEFT JOIN tbl_stu stu ON clz.clz_no = stu.clz_no
    </select>
</mapper>

.

二、学生stu

<?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="cc.ash.StuMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cc.ash.Stu">
        <id column="ID" property="id" />
        <result column="NAME" property="name" />
        <result column="AGE" property="age" />
        <result column="CLZ_NO" property="clzNo" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID, NAME, AGE, CLZ_NO
    </sql>

    <!-- 别名查询结果列 -->
    <sql id="Alias_Column_List">
        ${alias}.ID             AS ${alias_}id,
        ${alias}.NAME           AS ${alias_}NAME,
        ${alias}.AGE            AS ${alias_}AGE,
        ${alias}.CLZ_NO         AS ${alias_}CLZ_NO,
    </sql>
</mapper>

.

posted @ 2021-02-03 17:47  毁乐乖狂,自有诪张  阅读(976)  评论(0编辑  收藏  举报