mybatis 高级映射,关联嵌套配置

这里使用的是xml方式编写,提供两种方式进行映射封装数据

mybatis 基本表字段xml配置文件编写

用sql标签编写,不废话直接上代码

<mapper namespace="baseCoulmnSpace">

    <!-- 多表联查字段 -->
    <!-- 学生表字段 -->
    <sql id="student_column">
        ${item}.id,${item}.name,${item}.sex,${item}.student_kz
    </sql>

    <!-- 成绩字段 -->
    <sql id="student_cj_column">
        ${item}.cj_id,${item}.student_id,${item}.kc_id,${item}.pingfen,${item}.text
    </sql>

    <!-- 课程字段 -->
    <sql id="student_kc_column">
        ${item}.kc_id,${item}.kc_name
    </sql>

    <!-- 学生证字段 -->
    <sql id="student_xsz_column">
        ${item}.xsz_id,${item}.stu_id,${item}.jiguan,${item}.startDate,${item}.endDate
    </sql>

    <!-- 性别男健康字段 -->
    <sql id="student_sex_man_column">
        ${item}.id as sex_man_id,${item}.stu_id as
        sex_man_stu_id,${item}.date as sex_man_date,${item}.xin as
        sex_man_xin,${item}.gan as sex_man_gan,${item}.pi as sex_man_pi,
        ${item}.fei as sex_man_fei,${item}.shen as
        sex_man_shen,${item}.qianliexian
    </sql>

    <!-- 性别女健康字段 -->
    <sql id="student_sex_woman_column">
        ${item}.id as sex_woman_id,${item}.stu_id as
        sex_woman_stu_id,${item}.date as sex_woman_date,${item}.xin as
        sex_woman_gan,${item}.gan as sex_woman_gan,${item}.pi as
        sex_woman_pi,${item}.fei as sex_woman_fei,
        ${item}.shen as
        sex_woman_shen,${item}.zigong
    </sql>

    <!-- 表命字段 -->
    <sql id="table_name">
        et_student${suffix}
    </sql>

    <!-- 表基本字段 -->
    <sql id="student_base_column">
        id, name, sex, student_kz,text
    </sql>

    <sql id="student_cj_base_column">
        cj_id as cjId, student_id as stdId, kc_id as kcId,
        pingfen,text
    </sql>

    <sql id="student_kc_base_column">
        kc_id as kcId, kc_name as kcName,text
    </sql>

    <sql id="student_xsz_base_column">
        xsz_id as xszId, stu_id as stuId, jiguan , startDate,
        endDate,text
    </sql>

    <sql id="student_sex_man_base_column">
        id , stu_id as  stuId, date , xin , gan , pi ,
        fei , shen ,
        qianliexian,text
    </sql>

    <sql id="student_sex_woman_base_column">
        id , stu_id as stuId , date , xin , gan , pi
        , fei ,
        shen ,
        zigong,text
    </sql>

</mapper>

做完基本sql字段准备之后,开始编写查询xml,使用namespace调用对应的字段


高级映射xml编写方式一:连表查询数据封装

使用resultMap进行数据映射封装,结合collection和discriminator鉴别器使用,封装想要的数据
代码如下:

    <!-- 查询学生所有对应信息,flushCache="false":启用缓存,useCache="true":不刷新缓存;返回数据对应下面名为student_info的resultMap -->
    <select id="info" resultMap="student_info" flushCache="false"
        useCache="true">
        select
        <include refid="baseCoulmnSpace.student_column"><!-- 这里调用上面base字段xml中的sql字段,并传入对应的表别名 -->
            <property name="item" value="a" /><!-- 这里设置属性,对应上面sql字段xml中的参数 -->
        </include>
        ,
        <include refid="baseCoulmnSpace.student_cj_column">
            <property name="item" value="b" />
        </include>
        ,
        <include refid="baseCoulmnSpace.student_kc_column">
            <property name="item" value="c" />
        </include>
        ,
        <include refid="baseCoulmnSpace.student_xsz_column">
            <property name="item" value="d" />
        </include>
        ,
        <include refid="baseCoulmnSpace.student_sex_woman_column">
            <property name="item" value="e" />
        </include>
        ,
        <include refid="baseCoulmnSpace.student_sex_man_column">
            <property name="item" value="f" />
        </include>
        from et_student a left
        join
        <include refid="baseCoulmnSpace.table_name"><!-- 这里封装表命,和字段获取一样 -->
            <property name="suffix" value="_cj" />
        </include>
        b on
        a.id=b.student_id
        left
        join
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_kc" />
        </include>
        c on b.kc_id=c.kc_id
        left join
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_xsz" />
        </include>
        d on
        a.id=d.stu_id
        left join
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_sex_1" />
        </include>
        e on a.id=e.stu_id
        left join
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_sex_2" />
        </include>
        f on a.id=f.stu_id
        group
        by a.id,d.xsz_id,c.kc_id
    </select>

    <!-- 查询返回结果封装;返回类型type设置为HashMap,利于取值调用,如果有多条数据会自动封装成为ArrayList集合 -->
    <resultMap type="java.util.HashMap" id="student_info">
    <!-- 这里可以将property变为map的key值,利于后台数据获取遍历使用 -->
        <id column="id" property="studentId" javaType="java.lang.Integer"
            jdbcType="INTEGER" />
        <result column="name" property="studentName" javaType="java.lang.String"
            jdbcType="VARCHAR" />
        <result column="sex" javaType="java.lang.String" jdbcType="INTEGER"
            property="sex" />

        <!-- 封装课程:这里调用id为Student_KC的resultMap进行封装,让字段对应分开编写,利于修改;ofType:单条数据封装为指定的JavaBean类型; -->
        <collection property="listKC" javaType="java.util.ArrayList"
            ofType="StudentKC" resultMap="Student_KC"></collection>

        <!-- 封装方式一: 基本id和result基本属性封装 -->
        <collection property="listCJ" javaType="java.util.ArrayList"
            ofType="StudentCJ">
            <id column="cj_id" property="cjId" javaType="Integer" jdbcType="INTEGER" />
            <result column="student_id" property="stdId" javaType="Integer"
                jdbcType="INTEGER" />
            <result column="kc_id" property="kcId" javaType="Integer"
                jdbcType="INTEGER" />
            <result column="pingfen" property="pingfen" javaType="double"
                jdbcType="DECIMAL" />
        </collection>

        <!-- 封装方式二:使用collection中的resultMap可以映射另一个高级结果映射 -->
        <collection property="listCJ_TWO" javaType="java.util.ArrayList"
            ofType="StudentCJ" resultMap="StudentCJ_package_one"></collection>

        <!-- 封装方式三:关联嵌套封装 ,嵌套studentCJ_info查询语句使用;column:sql字段将被当做参数传入嵌套的sql中 -->
        <collection property="listCJ_FOUR" column="id"
            javaType="java.util.ArrayList" ofType="StudentCJ" select="studentCJ_info"
            fetchType="lazy"></collection>

        <!-- 封装健康数据集 -->
        <collection property="listJK_SEX" javaType="java.util.ArrayList"
            column="sex">
            <!-- 鉴别器使用,用于封装不同性别健康数据,根据性别值做区分,类似Java中的switch语法;这里column设置是用字段中的值作为条件,当值为2时封装成Student_sex_woman的resultMap -->
            <discriminator javaType="INTEGER" column="sex"
                jdbcType="TINYINT">
                <case value="2" resultMap="Student_sex_woman"></case>
                <case value="1" resultMap="Student_sex_man"></case>
            </discriminator>
        </collection>

    </resultMap>

    <!-- 性别男数据封装 -->
    <resultMap type="StudentSex2" id="Student_sex_man">
        <id column="sex_man_id" property="id" javaType="Integer" jdbcType="INTEGER" />
        <result column="sex_man_stu_id" javaType="Integer" jdbcType="INTEGER"
            property="stuId" />
        <result column="sex_man_date" property="date" javaType="java.util.Date"
            jdbcType="DATE" />
        <result column="sex_man_xin" property="xin" javaType="java.lang.String"
            jdbcType="VARCHAR" />
        <result column="sex_man_gan" property="gan" jdbcType="VARCHAR"
            javaType="java.lang.String" />
        <result column="sex_man_pi" property="pi" javaType="java.lang.String"
            jdbcType="VARCHAR" />
        <result column="sex_man_fei" javaType="java.lang.String"
            jdbcType="VARCHAR" property="fei" />
        <result column="sex_man_shen" javaType="java.lang.String"
            jdbcType="VARCHAR" property="shen" />
        <result column="qianliexian" javaType="java.lang.String"
            jdbcType="VARCHAR" property="qianliexian" />
    </resultMap>

    <!-- 性别女数据封装 -->
    <resultMap type="StudentSex1" id="Student_sex_woman">
        <id column="sex_woman_id" property="id" javaType="Integer"
            jdbcType="INTEGER" />
        <result column="sex_woman_stu_id" javaType="Integer" jdbcType="INTEGER"
            property="stuId" />
        <result column="sex_woman_date" property="date" javaType="java.util.Date"
            jdbcType="DATE" />
        <result column="sex_woman_xin" property="xin" javaType="java.lang.String"
            jdbcType="VARCHAR" />
        <result column="sex_woman_gan" property="gan" jdbcType="VARCHAR"
            javaType="java.lang.String" />
        <result column="sex_woman_pi" property="pi" javaType="java.lang.String"
            jdbcType="VARCHAR" />
        <result column="sex_woman_fei" javaType="java.lang.String"
            jdbcType="VARCHAR" property="fei" />
        <result column="sex_woman_shen" javaType="java.lang.String"
            jdbcType="VARCHAR" property="shen" />
        <result column="zigong" javaType="java.lang.String" jdbcType="VARCHAR"
            property="zigong" />
    </resultMap>

    <!-- 课程封装 -->
    <resultMap type="StudentKC" id="Student_KC">
        <id column="kc_id" javaType="Integer" jdbcType="INTEGER" property="kcId" />
        <result column="kc_name" javaType="String" jdbcType="VARCHAR"
            property="kcName" />
    </resultMap>

    <!-- 学生成绩数据映射方式一 ,使用id映射主键id,result映射基本属性 -->
    <resultMap type="StudentCJ" id="StudentCJ_package_one">
        <id column="cj_id" property="cjId" javaType="Integer" jdbcType="INTEGER" />
        <result column="student_id" property="stdId" javaType="Integer"
            jdbcType="INTEGER" />
        <result column="kc_id" property="kcId" javaType="Integer"
            jdbcType="INTEGER" />
        <result column="pingfen" property="pingfen" javaType="double"
            jdbcType="DECIMAL" />
    </resultMap>

    <!-- 学生成绩 -->
    <select id="studentCJ_info" resultType="StudentCJ" flushCache="false"
        useCache="true">
        select
        cj_id cjId, student_id stdId,kc_id kcId,pingfen,text
        from
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_cj" />
        </include>
        a
        <where>
            a.student_id = #{id,jdbcType=INTEGER}
        </where>
    </select>

这里基本是使用collection进行多条数据进行封装,查询sql为连表查询,下面将不使用连表查询将数据封装在一起


高级映射xml编写方式二:关联嵌套映射

通过使用association、collection进行sql条件查询传递,利用discriminator鉴别器区分封装的返回数据类型
代码如下:

    <!-- 查询学生信息,启用缓存,不刷新缓存 -->
    <select id="query_student" resultMap="paceage_student_all_data"
        flushCache="false" useCache="true">
        select
        <include refid="baseCoulmnSpace.student_base_column" />
        from
        <include refid="baseCoulmnSpace.table_name" />
    </select>

    <!-- 映射所有学生有关数据 -->
    <resultMap type="java.util.HashMap" id="paceage_student_all_data">
        <id column="id" property="studentId" javaType="java.lang.Integer"
            jdbcType="INTEGER" />
        <result column="name" property="studentName" javaType="java.lang.String"
            jdbcType="VARCHAR" />
        <result column="sex" javaType="java.lang.String" jdbcType="INTEGER"
            property="sex" />

        <!-- 映射学生课程和成绩; column设置: 传递查询参数名称=字段名称; fetchType:设置为懒加载lazy、即时加载eager; select:调用select语句-->
        <collection property="ListKC_CJ" column="{stdId=id}" javaType="java.util.ArrayList"
            ofType="java.util.HashMap" select="qeury_select_kc_cj" fetchType="lazy">
        </collection>

        <!-- 映射学生证 -->
        <collection property="ListXSZ" column="{stuId=id}"
            javaType="java.util.ArrayList" ofType="StudentXSZ" select="query_student_xsc"
            fetchType="lazy"></collection>

        <!-- 封装健康数据集 -->
        <collection property="listJK_SEX" javaType="java.util.ArrayList"
            fetchType="lazy">
            <!-- 鉴别器使用,用于封装不同性别健康数据,根据性别值做区分,类似Java中的switch语法; 这里学生表中性别数据为:1表示男,2表示女 -->
            <discriminator javaType="INTEGER" column="sex"
                jdbcType="TINYINT">
                <!-- 使用case中resultType设置返回结果为HashMap方式封装男学生健康数据 -->
                <case value="1" resultType="java.util.HashMap">
                    <!-- 封装男学生健康值 -->
                    <collection property="sex_man" column="{stuId=id}"
                        javaType="java.util.ArrayList" ofType="StudentSex2" select="query_student_sex_man"
                        fetchType="lazy"></collection>
                </case>
                <!-- 使用resultMap映射封装女学生健康数据 -->
                <case value="2" resultMap="Student_sex_woman_data"></case>
            </discriminator>
        </collection>

    </resultMap>

    <!-- 映射女学生健康数据 -->
    <resultMap type="java.util.HashMap" id="Student_sex_woman_data">
        <collection property="sex_woman" column="{stuId=id}"
            javaType="java.util.ArrayList" ofType="StudentSex1" select="query_student_sex_woman"
            fetchType="lazy"></collection>
    </resultMap>

    <!-- 查询学生课程及成绩 -->
    <select id="qeury_select_kc_cj" resultType="java.util.HashMap"
        flushCache="false" useCache="true">
        select
        <include refid="baseCoulmnSpace.student_kc_column">
            <property name="item" value="a" />
        </include>
        ,
        <include refid="baseCoulmnSpace.student_cj_column">
            <property name="item" value="b" />
        </include>
        from
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_kc" />
        </include>
        a
        left join
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_cj" />
        </include>
        b
        on a.kc_id=b.kc_id
        <where>
            1=1
            <if test="stuId != null and stuId !=0">
                and b.student_id = #{stdId,jdbcType=INTEGER}
            </if>
        </where>
    </select>

    <!-- 查询学生证 -->
    <select id="query_student_xsc" resultType="StudentXSZ"
        flushCache="false" useCache="true">
        select
        <include refid="baseCoulmnSpace.student_xsz_base_column" />
        from
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_xsz" />
        </include>
        <where>
            1=1
            <if test="stuId != null and stuId !=0">
                and stu_id = #{stuId,jdbcType=INTEGER}
            </if>
        </where>
    </select>

    <!-- 查询女学生 -->
    <select id="query_student_sex_woman" resultType="StudentSex1"
        flushCache="false" useCache="true">
        select
        <include refid="baseCoulmnSpace.student_sex_woman_base_column" />
        from
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_sex_1" />
        </include>
        <where>
            1=1
            <if test="stuId != null and stuId !=0">
                and stu_id = #{stuId,jdbcType=INTEGER}
            </if>
        </where>
    </select>

    <!-- 查询男学生 -->
    <select id="query_student_sex_man" resultType="StudentSex2"
        flushCache="false" useCache="true">
        select
        <include refid="baseCoulmnSpace.student_sex_man_base_column" />
        from
        <include refid="baseCoulmnSpace.table_name">
            <property name="suffix" value="_sex_2" />
        </include>
        <where>
            1=1
            <if test="stuId != null and stuId !=0">
                and stu_id = #{stuId,jdbcType=INTEGER}
            </if>
        </where>
    </select>

这里column查询配置方式为:传递查询参数名称=字段名称,可以通过 column=”{pro1=col1,pro2=col2}”方式配置多条参数;
在使用高级映射封装时,遇到无法使用constructor构造器方式进行值封装,如果你有更好的方式可以发送给我,constructor使用方式如下代码:

    <!-- 在使用时,出现找不到构造器错误,参数和类型全部匹配还是找不到,宁人费解,
            如果你有更好的解决方式可以联系我;qq:1119047192 -->
    <!-- 使用构造器映射结果 -->
    <resultMap type="StudentCJ" id="StudentCJ_package_two">
        <constructor>
            <arg column="cj_id" name="cjId" javaType="java.lang.Integer"
                jdbcType="INTEGER" />
            <arg column="student_id" javaType="java.lang.Integer" jdbcType="INTEGER"
                name="stdId" />
            <arg column="kc_id" javaType="java.lang.Integer" jdbcType="INTEGER"
                name="kcId" />
            <arg column="pingfen" javaType="double" jdbcType="DECIMAL" name="pingfen" />
            <arg column="text" javaType="java.lang.String" jdbcType="VARCHAR"
                name="text" />
        </constructor>
    </resultMap>

这里就是基本mybatis高级映射的配置方式了,希望可以帮到他们,提供源码,点击链接下载源码

mybatis 高级映射实例下载

猜你喜欢

转载自blog.csdn.net/qq_35525955/article/details/80890926