Mybatis多条件In批量查询方法

Mybatis多条件In批量查询方法,第1张

需要注意的问题
  1. 在Oracle中in条件不能超过1000个 所以大批量数据需要分批查询
    Repository中做分批处理
1.xml文件
<select id="selectByMoIdAndMaterialId" resultMap="BaseResultMap" >
        select t.a, t.b, t.c ,t.d 
        from tableName t
        WHERE 
        (t.a,t.b) in
        <foreach collection="list"  item="item" open="(" close=")" separator=",">
            (#{item.attribute1},#{item.attribute2})
        </foreach>
    </select>
2.mapper文件
List<MoAssActual> selectByattribute1Andattribute2(                                           @Param("list") List<HpsMtMoAssActualQueryVo> list);
3.VO文件
public class HpsMtMoAssActualQueryVo implements Serializable {

	private static final long serialVersionUID = -3577726134433110319L;
	private Long attribute1;
	private Long attribute2;

	public Long getAttribute1() {
		return attribute1;
	}

	public void setAttribute1(Long attribute1) {
		this.attribute1 = attribute1;
	}

	public Long getAttribute2() {
		return attribute2;
	}

	public void setAttribute2(Long attribute2) {
		this.attribute2= attribute2;
	}
}
4.Repository层
@Component
public class MoAssActualRepositoryImpl extends BaseRepositoryImpl<MoAssActual> implements MoAssActualRepository {
	private static final int MaxInSize = 1000;

	private final MoAssActualMapper moAssActualMapper;

	@Autowired
	public MoAssActualRepositoryImpl(MoAssActualMapper moAssActualMapper) {
		this.moAssActualMapper = moAssActualMapper;
	}

	@Override
	public List<MoAssActual> selectByMoIdAndMaterialId(Long tenantId, List<HpsMtMoAssActualQueryVo> list){
		if(CollectionUtils.isEmpty(list)){
			return null;
		}
		List<MoAssActual> moAssActuals = new ArrayList<>(list.size());
		int valueSize = list.size();
		if (valueSize==1){
			moAssActuals = moAssActualMapper.selectByMoIdAndMaterialId(tenantId, list);
		}else {
			int batchSize = valueSize/MaxInSize+(valueSize % MaxInSize == 0 ? 0 : 1);
			for (int i = 0; i < batchSize; i++) {
				// 获取list中 最多1000条数据
				List<HpsMtMoAssActualQueryVo> queryVoList = list.stream()
						.skip(i*MaxInSize).limit(MaxInSize).collect(Collectors.toList());
				List<MoAssActual> resultList = moAssActualMapper.selectByMoIdAndMaterialId(tenantId, queryVoList);
				moAssActuals.addAll(resultList);
			}
		}
		return moAssActuals;
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/724580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存