正在阅读:
Mybatis Method queryTotal execution error of sql
Mybatis Method queryTotal execution error of sql
问题背景mybatis + sql2000的查询, 其中主要是查询参数和表字段类型需要匹配,否则可能会出现以上问题。
但是执行SQL直接在sql查询工具中是没问题的,带入项目,使用mybatis注入就有可能有问题。
如 字段 >= 1 and 字段 < 10
此时如果字段为string 那 需要改成 字段 >= '1' and 字段 < '10'
1.将用于in 条件的 数据按照一定数量进行分组
public <T> List<List<T>> getSumArrayList(List<T> list){
List<List<T>> objectlist = new ArrayList<>();
int iSize = list.size()/1000;
int iCount = list.size()%1000;
for(int i=0;i<=iSize;i++){
List<T> newObjList = new ArrayList<>();
if(i==iSize){
for(int j =i*1000;j<i*1000+iCount;j++ ){
newObjList.add(list.get(j));
}
}else{
for(int j =i*1000;j<(i+1)*1000;j++ ){
newObjList.add(list.get(j));
}
}
if(newObjList.size()>0){
objectlist.add(newObjList);
}
}
return objectlist;
}方法二
List<Integer> numList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8); List<List<Integer>> lists=Lists.partition(numList,3);
2 调用分组后的数组
xml:
例:
<if test="deviceId.size()>0" >
and
<foreach collection="deviceId" item="item" open="(" separator="or" close=")">
DEVICE_ID in
<foreach collection="item" item="item2" open="(" separator="," close=")">
#{item2}
</foreach>
</foreach>
</if>QueryWrapper:
LambdaQueryWrapper<EntiyName> qwrapper = new LambdaQueryWrapper();
for (int i=0;i<tempList.size();i++) {
if (i<(tempList.size()-1)) {
qwrapper .in(EntiyName::getId,tempList.get(i) ).or();
}
else {
qwrapper .in(EntiyName::getId,tempList.get(i) );
}
}这里的if 做了or的处理。
使用以上方式处理完成之后,在最后查询SQL数据库的时候的sql就如:
select * from table where id in (1,2,3...1000) or id in (10001,10002,...)
该日志由 bemender 于 2020年09月20日 发表
转载请注明文本地址:http://www.bemhome.com/post/5.html
