mybatis中的操作

1、两表查询,且不能做内外连接, in的数量超过1000,利用java分隔

一个示例:

    public static void main(String[] args) {
        ArrayList<Integer> resultList = new ArrayList<>();
        // 模拟一个需要in查询的数组
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < 2003; i++) {
            arrayList.add(i);
        }
        // 获得倍数,也就是循环查询次数。
        Integer a =  (int) Math.ceil(new Double(arrayList.size())/new Double(1000));
        for (int i = 0; i < a; i++) {
            ArrayList<Integer> tempRkIdList = new ArrayList<>();
            for (int j = 0; j < 1000; j++) {
                if ((i*1000 +j) >= arrayList.size()){
                    break;
                }
                tempRkIdList.add(i*1000 + j);
            }
            // 查询
            System.out.println(tempRkIdList.size());
            resultList.addAll(tempRkIdList);
        }
        System.out.println(resultList.size());
    }
   

抽出方法:

   /**
    * @desc 将一个大的list 拆分成多个小list
    * @author lizhipeng
    * @date 2022-01-30 10:20
    * @Param id list
    * @return java.util.List<java.util.List<java.lang.String>>
    */
    public static List<List<String>> getSmallListArray(List<String> bigList){
        List<List<String>> resultList = new ArrayList<>();
        // 获得倍数,也就是循环查询次数。
        Integer num =  (int) Math.ceil(new Double(bigList.size())/new Double(1000));
        for (int i = 0; i < num; i++) {
            List<String> tempRkIdList = new ArrayList<>();
            for (int j = 0; j < 1000; j++) {
                if ((i*1000 +j) >= bigList.size()){
                    break;
                }
                tempRkIdList.add(bigList.get((i*1000 + j)));
            }
            resultList.add(tempRkIdList);
        }
        return resultList;
    }

重新运行:

    public static void main(String[] args) {
        ArrayList<String> resultList = new ArrayList<>();
        // 模拟一个需要in查询的数组
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < 2003; i++) {
            arrayList.add(String.valueOf(i));
        }
        List<List<String>> smallListArray = getSmallListArray(arrayList);
        for (List<String> list : smallListArray) {
            System.out.println(list.size());
            // 查询代码,获得list
        	//resultList.addAll(list)
        }
        //System.out.println(resultList.size());
    }

2、批量保存模板

注意 () 不能省
数据原配置要加上:&allowMultiQueries=true

<insert id="saveList">
        INSERT INTO SQ_CONTROL_INFO
        (
            ID,
            RK_ID
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id},
            #{item.rkId}
            )
        </foreach>
</insert>

3、批量更新模板

数据原配置要加上:&allowMultiQueries=true

<foreach collection="list" item="controlInfo" separator=";">
    update CONTROL_INFO set  <更新代码> where id = #{controlInfo.id}
</foreach>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值