mybatis查询foreach使用

时间:2024-02-19 13:52:18

1.mybatis传入map参数,map中包含list:

List<FukaModel> fukaModels = price.getSchemaPrice().getFukaList();

Map<String, Object> objectMap = new HashMap<>();

objectMap.put("schemaId", schemaId);

objectMap.put("list", fukaModels);

sqlSession.insert("insertFukaData",objectMap);

    <select id="insertFukaData" parameterType="map" >

        <foreach collection="list" item="item" index="index" open="" separator=";" close="">

            INSERT INTO jj_fokas_deduct (price_schema_id,min_num, max_num, user_grade)

            VALUES (#{schemaId},0,#{item.num},#{item.userGrade})

        </foreach>

    </select>

 

2.

List<JjActivityRecommendSku> activityRecommendSkuList = new ArrayList<>();

sqlSession.insert("insertActivityRecommendSkuList", activityRecommendSkuList);

  <insert id="insertActivityRecommendSkuList" parameterType="ActivityRecommendSku" >

    insert into activity_recommend_sku (rec_sku_id, activity_id, sku_id,

    sku_sort_order)

    values

    <foreach collection="list" item="item" index="index" separator=",">

      (#{item.recSkuId,jdbcType=INTEGER}, #{item.activityId,jdbcType=INTEGER}, #{item.skuId,jdbcType=VARCHAR},

      #{item.skuSortOrder,jdbcType=INTEGER})

    </foreach>

  </insert>

  3.

    <update id="unShelveGoodsSku" parameterType="java.util.List">

    update goods_sku set status = 5 where status = 4 and sku_id in

    <foreach item="item" index="index" collection="list"

             open="(" separator="," close=")">

      #{item}

    </foreach>

  </update>

  4.

  List<String> skuIds = s.selectList(statement,param);

        Map<String, Object> map = new HashMap<>();

        map.put("storeId",storeId);

        map.put("devType",devType);

        map.put("list",skuIds);

    WHERE SkuId In

    <foreach item="sku_id" collection="list" index="index" open="(" separator="," close=")">

        #{sku_id}

    </foreach>

5.lambda表达式

currencyDataListTemp.stream().collect(Collectors.groupingBy(o-> new CurrencyData(o.getCurrencyDtID(),o.getCurrencyAtID(),o.getCurrencyOriID(),o.getCurrencyName()),Collectors.summingInt(o->o.getReturnAmount()))).forEach((k,v)->{

    k.setReturnAmount(v);

    currencyDataList.add(k);

});

 

重写CurrencyDatahashequals方法:

@Override

public boolean equals(Object o) {

    if (this == o) return true;

    if (o == null || getClass() != o.getClass()) return false;

    CurrencyData currencyData = (CurrencyData) o;

    return Objects.equals(currencyDtID, currencyData.currencyDtID) &&

            Objects.equals(currencyAtID, currencyData.currencyAtID) &&

            Objects.equals(currencyOriID, currencyData.currencyOriID) &&

            Objects.equals(currencyName, currencyData.currencyName) &&

            Objects.equals(returnAmount, currencyData.returnAmount);

}

@Override

public int hashCode() {

    return Objects.hash(currencyDtID, currencyAtID, currencyOriID,currencyName);

}

6.

public static <T>List<T> checkListRepeat(List<T> list) {

    return list.stream()

            .collect(Collectors.toMap(k -> k, v -> 1, (a, b) -> a + b))

            .entrySet().stream()

            .filter(entry -> entry.getValue() > 1)

            .map(Map.Entry::getKey)

            .collect(Collectors.toList());

}

7.

List<Test> list = new ArrayList<>();

//        list.add(new Test("一年级二班", "小明"));

        list.add(new Test("一年级二班啊", "小芳"));

        list.add(new Test("一年级二班", "小华"));

        list.add(new Test("一年级三班啊", "翠花"));

        list.add(new Test("一年级三班", "香兰"));

        // 集合中对象属性转map

        Map<String, String> map = list.stream().collect(Collectors.toMap(Test :: getClassName, Test :: getStudentName));

8.

double sumRate = lotteryProbList.stream().mapToDouble(Double::doubleValue).sum();

9.

Map<String, List<CommodityInfo>> listMap = commodityInfoList.stream().collect(Collectors.groupingBy(model -> model.getCommodityId()));

 

 

Mybatis工作原理