MyBatis流式查询

mapper.java里使用对象org.apache.ibatis.cursor.Cursor 接收查询结果

  1. Cursor<xxPO> streamQuery();

mapper.xml文件里,指定一次获取的数据量

<select id="streamQuery"
        resultType="xxPO"
        resultSetType="FORWARD_ONLY"
        fetchSize="100">
    SELECT
    *
    FROM
    xx
</select>

service层代码调用

try (Cursor<xxPO> cursor = xxMapper.streamQuery()) {
    cursor.forEach(po -> {
        //进行操作
    });
} catch (IOException e) {
    log.error("流式查询失败");
}

流式查询 在完成操作前不能关闭sqlsession,可以在方法上加事务注解**@Transactional**解决

MyBatis批量条件更新

虽然mybatisplus也提供了批量更新,但仅只是根据主键id来更新
使用mybatis的方式可以更灵活
在mapper.xml里使用foreach标签循环update语句,相当于一次sqlsession执行多条更新语句

    <update id="updateBatch">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update xxx
            <set>
                <if test="item.id!=null">
                    id=#{item.id} ,
                </if>

            </set>
            <where>
                <if test="item.id!=null and item.id!=''">
                    id=#{item.id}
                </if>
            </where>
        </foreach>
    </update>

日期区间查询xml写法

不能直接在xml中写大于等于 需要转义

<if test="params.startTime != null">
  and `start_time` <![CDATA[>=]]> #{params.startTime}
</if>
<if test="params.endTime != null">
  and `start_time` <![CDATA[<=]]> #{params.endTime}
</if>