zoukankan      html  css  js  c++  java
  • Mybatis的批量操作

    XML批量查询

    collection: 指定要遍历的集合(三种情况 list,array,map) !!!!在这种使用注解sql的情况下,这里请填写mapper方法中集合的名称      
    item:将当前遍历出的元素赋值给指定的变量 (相当于for循环中的i)
    separator:每个元素之间的分隔符 
    open:遍历出所有结果拼接一个开始的字符 
    close:遍历出所有结果拼接一个结束的字符 
    index:索引。遍历list的时候是index就是索引,item就是当前值 
    #{变量名}就能取出变量的值也就是当前遍历出的元素

    List在xml的方式

    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(3);
    TestDAO.getItermByList(list);
    
    
    xml中的sql:
    <select id="getItermByList" resultType="java.util.HashMap">
            SELECT id,display_name as name from t_recommendation_info where id in
            <foreach item="item" index="index" collection="list"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
    </select>

    Map在xml的方式

    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(3);
    HashMap<String,Object> parames = new HashMap<String,Object>();
    parames.put("mapList",list);
    TestDAO.getItermByList(parames);
    
     
    xml中的sql:
    <select id="getItermByList" resultType="java.util.HashMap">
            SELECT id,display_name as name from t_recommendation_info where id in
            <foreach item="item" index="index" collection="mapList"
                     open="(" separator="," close=")">
                #{item}
            </foreach></select>

    对象在xml的方式

    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(3);
     
    //TestModel  的实现
    public class TestModel {
     
        private List<Integer> searchList;
     
        public List<Integer> getSearchList() {
            return searchList;
        }
     
        public void setSearchList(List<Integer> searchList) {
            this.searchList = searchList;
        }
    }
     
    TestModel tm = new TestModel();
    tm.setSearchList(list);
    TestDAO.getItermByList(tm)
     
    
    //XML中的sql:
    <select id="getItermByList" resultType="java.util.HashMap">
            SELECT id,display_name as name from t_recommendation_info where id in
            <foreach item="item" index="index" collection="searchList"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
    </select>

    xml批量insert--mysql

    <!--批量增加测试-->
        <insert id="insertList" parameterType="java.util.List">
            insert into t_enterprise_water_ele
            (
            /*方法一*/
            -- WATER_ELE_ID,
            -- ENTERPRISE_ID,
            -- ENTERPRISE_USCC,
            -- ENTERPRISE_NAME,
            -- YEARMONTH,
            -- WATER_SIZE,
            -- WATER_AMOUNT,
            -- ELE_SIZE,
            -- ELE_AMOUNT,
            -- STATUS,
            -- OPERATOR,
            -- OPERATE_TIME
            /*方法二*/
            <include refid="Base_Column_List"/>
            )
            VALUES
            <foreach collection="list" item="item" index="index" separator=",">
                (
                #{item.waterEleId,jdbcType=VARCHAR},
                #{item.enterpriseId,jdbcType=VARCHAR},
                #{item.enterpriseUscc,jdbcType=VARCHAR},
                #{item.enterpriseName,jdbcType=VARCHAR},
                #{item.yearmonth,jdbcType=VARCHAR},
                #{item.waterSize,jdbcType=DECIMAL},
                #{item.waterAmount,jdbcType=VARCHAR},
                #{item.eleSize,jdbcType=DOUBLE},
                #{item.eleAmount,jdbcType=VARCHAR},
                #{item.status,jdbcType=INTEGER},
                #{item.operator,jdbcType=VARCHAR},
                #{item.operateTime,jdbcType=TIMESTAMP}
                )
            </foreach>
        </insert>
    对于foreach标签的解释参考了网上的资料,具体如下:
    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了,当然单参数也可以封装成map

    xml批量insert--oracle

      <insert id="insertForeach" parameterType="java.util.List" useGeneratedKeys="false">
        insert into m_db_task_step
        ( STEP_ID, COMP_NO, TASK_NO, STEP_NAME, STEP_SN, PRE_STEP_ID )
        SELECT A.*
        FROM(
        <foreach collection="list" item="item" index="index" separator="UNION ALL">
          select
          #{item.stepId} as STEP_ID,
          #{item.compNo} as COMP_NO,
          #{item.taskNo} as TASK_NO,
          #{item.stepName} as STEP_NAME,
          #{item.stepSn} as STEP_SN,
          #{item.preStepId} as PRE_STEP_ID
          FROM dual
        </foreach>
        )A
      </insert>

    xml批量删除

        <delete id="deleteByBatch" parameterType="java.lang.String">
            delete from t_enterprise_output_value
            where OUTPUT_ID IN
            <foreach collection="array" item="outputId" open="(" separator="," close=")">
                #{outputId}
            </foreach>
        </delete>

    xml批量更新

    <update id="WHOLESALE-UPDATE-REBATE-CONFIG-BY-PRODUCTCODE" >
        UPDATE 
            rebate_config
        SET
            rebate_type = 'BY_RATE',rebate_config_value = #rebateConfigValue#
        WHERE
            <iterate  property="list" conjunction="or" open="(" close=")">
              product_code =#list[]#
            </iterate>
    </update>

    标签说明:

    1. prepend 可被覆盖的 SQL 语句组成部分,添加在语句的前面(可选)
    2. property 类型为 java.util.List 的用于遍历的元素(必选)
    3. open 整个遍历内容体开始的字符串,用于定义括号(可选)
    4. close 整个遍历内容体结束的字符串,用于定义括号(可选)
    5. conjunction 每次遍历内容之间的字符串,用于定义 AND 或 OR(可选)

    注意

    1. 这里传递过来的参数是一个Map<string,object>

      Map<String, Object> conditions = new HashMap<String, Object>();
      conditions.put("rebateConfigValue", rebateValue); //rebateValue是一个固定的值
      conditions.put("list", productCodeList);
    2. 使用时,在List元素名后面包括方括号[]非常重要,方括号[]将对象标记为List,以防解析器简单地将List输出成String

    批量在注解的方式:

      @Select({
        "<script>" 
            + "SELECT "
            + "orders.orderId, product_sku.productId, product_sku.skuName, "
            + "orders.number, orders.orderPrice,product_sku.skuPrice, "
            + "orders.orderCreate, customer.mobile, shop_keeper.mobile1 as shopKeeperMobile, "
            + "shop.name, orders.shopId, shop.address, shop.cityCode "
            + "FROM orders, product_sku, customer, shop, shop_keeper "
            + "WHERE orders.skuId=product_sku.skuId "
            + "AND orders.customerId = customer.customerId "
            + "<if test='orderStatus != null'>"  
            +      "AND orders.orderStatus IN "
            +      "<foreach item='status' index='index' collection='orderStatus' open='(' separator=',' close=')'>"
            +           "#{status} "
            +      "</foreach>"
            + "</if>"
            + "AND orders.shopId = shop.shopId "
            + "AND orders.shopId = shop_keeper.shopId "
            + "ORDER BY customer.mobile DESC, orders.shopId DESC ,orders.orderCreate DESC"
        + "</script>"
        })
        List<Map<String, Object>> selectOrders(@Param(value="orderStatus")List<Short> orderStatus);
        
        @Update({
        "<script>"
            + "UPDATE orders SET orderStatus = #{orderStatus} WHERE orderId in "
            + "<foreach item='item' index='index' collection='orderId' open='(' separator=',' close=')'>"
            +       "#{item}"
            + "</foreach>" 
        +"</script>" 
        })
        int updateOrderStatus(@Param("orderStatus") Short orderStatus,@Param("orderId") String[] orderList);
  • 相关阅读:
    mongodb查询部分满足条件的列
    java属性文件读取,属性修改
    url中的jsessionid解释
    windows下mongodb设置用户名密码&用python连接
    Linux下Redis的安装和部署 详细
    Windows下手动安装redis服务
    Windows下安装Redis服务
    Python装饰器单例
    Django学习之全局变量
    Python3中 对local和nonlocal 关键字的改善认识(新手向)
  • 原文地址:https://www.cnblogs.com/linhongwenBlog/p/12764754.html
Copyright © 2011-2022 走看看