MyBatis一对多和多对多xml配置

Posted upuptop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis一对多和多对多xml配置相关的知识,希望对你有一定的参考价值。

MyBatis一对多和多对多xml配置

<?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="com.ktcx.models.business.busfreighttemplate.dao.BusFreightTemplateDao">


    <sql id="BaseColumn">
        id,
        partner_id,
        create_by,
        create_date,
        code,
        is_del,
        end_update_date,
        name,
        type,
        area,
        is_default
    </sql>


    <resultMap id="myResultMap"
               type="com.ktcx.models.business.busfreighttemplate.model.BusFreightTemplate">
        <id property="id" column="id"></id>

        <result property="partnerId" column="partnerId"/>
        <result property="code" column="code"/>
        <result property="partnerId" column="partnerId"/>
        <result property="endUpdateDate" column="endUpdateDate"/>
        <result property="name" column="name"/>
        <result property="type" column="type"/>
        <result property="area" column="area"/>
        <result property="isDefault" column="isDefault"/>

        <collection property="freightProvices"
                    ofType="com.ktcx.models.bq.freightprovice.model.FreightProvice">
            <id column="fpId" property="id"/>

            <result property="proviceText" column="fpProviceText"/>
            <result property="code" column="fpCode"/>
            <result property="increaseFee" column="fpIncreaseFee"/>
            <result property="increaseNum" column="fpIncreaseNum"/>
            <result property="startFee" column="fpStartFee"/>
            <result property="startNum" column="fpStartNum"/>
            <result property="proviceName" column="fpProviceName"/>
            <result property="proviceId" column="fpProviceId"/>
            <result property="shopId" column="fpShopId"/>
            <result property="templateId" column="fpTemplateId"/>
        </collection>


    </resultMap>


    <!--一对多的查询-->
    <select id="selectTemplateAndFreight" resultMap="myResultMap">
    SELECT
        ft.id,
        ft.partner_id AS partnerId,
        ft.code ,
        ft.end_update_date AS  endUpdateDate,
        ft.name ,
        ft.type ,
        ft.area ,
        ft.is_default AS  isDefault,
        fp.id fpId,
        fp.shop_id AS fpShopId  ,
        fp.provice_id AS fpProviceId ,
        fp.provice_name AS fpProviceName ,
        fp.start_num  AS fpStartNum ,
        fp.start_fee  AS fpStartFee ,
        fp.increase_num AS fpIncreaseNum ,
        fp.increase_fee AS fpIncreaseFee ,
        fp.code AS fpCode ,
        fp.provice_text AS  fpProviceText ,
        fp.template_id AS fpTemplateId
    FROM
     (
       SELECT
        f.id,
        f.partner_id ,
        f.code ,
        f.end_update_date  ,
        f.name ,
        f.type ,
        f.area ,
        f.is_default ,
        f.is_del,
        f.create_date
        FROM
        bus_freight_template f
        LEFT JOIN bus_freight_provice p
        ON f.id = p.template_id
        WHERE f.code = p.code
        AND f.code = #code
        GROUP BY f.id
        LIMIT #startPage,#pageSize

     ) ft

      LEFT JOIN bus_freight_provice fp
        ON ft.id = fp.template_id
    WHERE ft.code = fp.code
      AND fp.template_id IS NOT NULL
      AND ft.code =  #code
      AND fp.is_del = '0'
      AND ft.is_del = '0'

      order by ft.create_date desc
    </select>


    <select id="getALLTemplate" resultType="int">
        SELECT COUNT(0) FROM bus_freight_template WHERE is_del = '0' AND CODE= #code
    </select>


    <insert id="saveData">
        insert into bus_freight_template(
        <include refid="BaseColumn"/>
        )
        VALUES
        (
        #id,
        #partnerId,
        #createBy,
        #createDate,
        #code,
        #isDel,
        #endUpdateDate,
        #name,
        #type,
        #area,
        #isDefault
        )

    </insert>

</mapper>

以上是关于MyBatis一对多和多对多xml配置的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis入门---一对多多对多

SpringBoot使用Mybatis注解进行一对多和多对多查询

MyBatis一对多,多对一,多对多

毕设的学习(22)springboot使用mybatis注解进行一对多和多对多查询/

如何判断一对一对多和多对多的关系

多表关系一对多和多对多