mybatis使用

當傳入list和String時候 或者 傳入String[] 類型和String時候
mapper:

    int batchDeleteCode(@Param("map")Map<String,Object> map);

    Set<String> selectInCode(@Param("map")Map<String,Object> map);

service
list[String]時

    public int batchDeleteCode(List<String> codes, String batch) {
        Map<String,Object> map=new HashMap<>();
        map.put("batch",batch);
        map.put("codes",codes);
        return pubNumberFamilyTempMapper.batchDeleteCode(map) ;
    }

String[]時

 public Set<String> selectInCode(String[] codes,String batch) {
        Map<String,Object> map=new HashMap<>();
        map.put("codes",codes);
        map.put("batch",batch);
        return pubNumberFamilyTempMapper.selectInCode(map);
    }

xml

  <delete id="batchDeleteCode" parameterType="java.util.Map">
    delete from pubnumber_family_temp where batch =#{map.batch} and  code in
    <foreach  collection="map.codes" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
  </delete>
  <select id="selectInCode"  resultType="java.lang.String" parameterType="java.util.Map">
    select DISTINCT(patent_number) from pubnumber_family_temp where batch =#{map.batch} and code IN
    <foreach  collection="map.codes" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
   </select>


當傳入set時,parameterType=”java.lang.String” 而不是java.lang.Set

當返回數據是List 《String》 時, resultType=”java.lang.String” 而不是java.util.List


類似於批量插入時候,是數組對象

<insert id="insertTempBatch" useGeneratedKeys="true" parameterType="java.util.List">
    <selectKey resultType="long" keyProperty="id" order="AFTER">
      SELECT
      last_insert_id()
    </selectKey>
    insert into pubnumber_family_temp(patent_number, kind, date1, date2, application_number, group_name, code,batch)
    VALUES
    <foreach collection="list" item="item" index="index" separator=",">
      (#{item.patentNumber},#{item.kind},#{item.date1},#{item.date2},#{item.applicationNumber},#{item.groupName},#{item.code},#{item.batch})
    </foreach>
  </insert>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章