美文网首页
2019-07-31 mybatis.3.4.5 注解使用(批量

2019-07-31 mybatis.3.4.5 注解使用(批量

作者: 东_11f3 | 来源:发表于2019-07-31 19:24 被阅读0次

    参考链接

    文档
    文档2
    文档3
    (https://slnddd.iteye.com/blog/2347586
    )
    (https://segmentfault.com/a/1190000018084851
    )
    (https://blog.csdn.net/hykwhjc/article/details/95979617
    )

    批量新增

    批量新增1
    @InsertProvider(type = SmsProvider.class,method = "saveList")
        void saveList(List<Sms> sms);
    
    public String saveList(Map map) {
            List<Sms> regions = (List<Sms>) map.get("list");
            StringBuilder sb = new StringBuilder();
            sb.append("INSERT INTO  t_sms");
            sb.append("(id, phone, batch_id, delivery_status,long_link,create_time,update_time) ");
            sb.append("VALUES ");
            MessageFormat mf = new MessageFormat("(#'{'list[{0}].id}, #'{'list[{0}].phone}, #'{'list[{0}].batchId}, #'{'list[{0}].deliveryStatus}, #'{'list[{0}].longLink}, #'{'list[{0}].createTime}, #'{'list[{0}].updateTime})");
            for (int i = 0; i < regions.size(); i++) {
                sb.append(mf.format(new Object[]{i}));
                if (i < regions.size() - 1) {
                    sb.append(",");
                }
            }
            return sb.toString();
        }
    

    批量新增2

    @Insert("<script>" +
            "INSERT INTO expression_key_word(expression_key_word_id,key_word_type,source_id,expression_id,key_word_value,create_user_id,create_time)"+
            " VALUES"+
            "<foreach collection=\"list\" item=\"item\" index=\"index\" separator=\",\">" +
            "(#{item.baseId},#{item.keyWordType},#{item.sourceId},#{item.expressionId},#{item.keyWordValue},#{item.createUserId},#{item.createTime})"+
            "</foreach>" +
            "</script>")
    

    批量更新

    多条件更新

    @Update("<script>" +
                "update t_sms ts , t_sms_batch tsb " +
                " SET " +
                "   ts.delivery_status = #{deliveryStatus}, ts.delivery_time = #{deliveryTime}, ts.update_time = now(), " +
                "   ts.sms_platform_status = "  +
                "     <foreach collection=\"list\" item=\"item\" open=\"case \" close=\" end\"> " +
                "            when ts.phone =#{item.phone} and tsb.batch_number=#{item.batchNumber} then #{item.smsPlatformStatus} " +
                "     </foreach> " +
                " WHERE " +
                "    <foreach collection=\"list\" item=\"item\" open=\"( \" separator=\") or (\" close=\" )\">\n" +
                "      ts.phone =#{item.phone}" +
                "       AND ts.batch_id = tsb.id " +
                "       AND tsb.batch_number=#{item.batchNumber}\n" +
                "    </foreach>\n" +
                "</script>" )
        Integer updateListByParams(@Param("list") List<SmsCallBackListVo.SmsCallBackVo> smsCallBackVos ,
                                   @Param("deliveryStatus") Integer deliveryStatus,
                                   @Param("deliveryTime") Date deliveryTime);
    

    批量更新,set值 确定 ,where 不确定

    
    
    public String updateIfMainByBaseIds(Map map){
            List<Integer> ids = (List<Integer>) map.get("ids");
            StringBuilder sb = new StringBuilder();
            sb.append("UPDATE t_product set if_main = #{ifMain},update_user_id = #{user},update_time = #{currentTime} WHERE id IN (");
            for (int i = 0; i < ids.size(); i++) {
                sb.append(ids.get(i));
                if (i < ids.size() - 1){
                    sb.append(",");
                }
    
            }
            sb.append(")");
            return sb.toString();
        }
     /**
         * 批量更新主推状态
         * @param ids
         * @param ifMain
         * @return
         */
        @UpdateProvider(type = ProductProvider.class,method = "updateIfMainByBaseIds")
        int updateIfMainByBaseIds(@Param("ids") List<Integer> ids,@Param("ifMain") Integer ifMain,
                                  @Param("user")Integer user,@Param("currentTime") Date currentTime);
    
    
    

    相关文章

      网友评论

          本文标题:2019-07-31 mybatis.3.4.5 注解使用(批量

          本文链接:https://www.haomeiwen.com/subject/nbqxdctx.html