首页 > 技术文章 > 仓库批量存储

dk2557 2020-03-26 15:45 原文

批量插入

定义字段,方便调用

 <sql id="MaterialsInColumns">
      materialsId,materialsName,materialsType,unitName,unitId,classfiyName,
      classfiyId,saveLocation,saveLocationId,supplierId,
      materialsPrice,materialsNum,comment,inOrOut,itemId,materialsNumber,
      state,createDate,updateDate
    </sql>

批量入库

<!--产品入库-->
    <insert id="MaterialsIn" parameterType="com.wanqun.wisdomsitecloud.v1.code.bean.Materials">
        insert into m_materials(
        <include refid="MaterialsInColumns"/>
        )
        VALUES
        <foreach collection="list" item="temp" separator="," close=";">
            (#{temp.materialsId},
            #{temp.materialsName},
            #{temp.materialsType},
            #{temp.unitName},
            #{temp.unitId},
            #{temp.classfiyName},

            #{temp.classfiyId},
            #{temp.saveLocation},
            #{temp.saveLocationId},
            #{temp.supplierId},

            #{temp.materialsPrice},
            #{temp.materialsNum},
            #{temp.comment},
            1,
            #{temp.itemId},
            #{temp.materialsNumber},
            1,
            now(),
            now())
        </foreach>
    </insert>

foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item集合中每一个元素进行迭代时的别名,
index表示在迭代过程中,每次迭代到的位置,
open该语句以什么开始,
separator在每次进行迭代之间以什么符号作为分隔 符,
close以什么结束,
在使用foreach的时候最关键的也是最容易出错的就是collection属性,
该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,
主要有一下3种情况:
1.     如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.     如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.     如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了

原文链接:https://blog.csdn.net/sz15732624895/article/details/82892283

批量插入加判断

 <insert id="insertExaminingDetails" parameterType="map">
        insert into m_examiningReportDetails(
        examiningReportDetailsId,
        fieldId,
        createDate,
        updateDate,
        fileClass,
        examiningReportId,
        resultId,
        resultInfo
        )
        VALUES
        <foreach collection="list" item="temp" separator="," close=";">
            (#{temp.examiningReportDetailsId},
            #{temp.fieldId},
            now(),
            now(),
            #{temp.fileClass},
            #{temp.examiningReportId}
            <choose>
                <when test="temp.resultId != null and temp.resultId !=''">
                   , #{temp.resultId}
                </when>
                <otherwise>
                  , null
                </otherwise>
            </choose>
            <choose>
                <when test="temp.resultInfo != null and temp.resultInfo !=''">
                   , #{temp.resultInfo}
                </when>
                <otherwise>
                   , null
                </otherwise>
            </choose>

            )
        </foreach>

    </insert>

 

<select id = "" resultMap = "">

   select * from table
   <where>
          <if test="type == 'x1' ">
               and  条件1;
         </if>
        <if test="type == 'x2' ">
               and  条件2;
         </if>
   </where>

</select>

 

推荐阅读