使用Mybatis Plus打包批量查询错误
Using Mybatis Plus to package batch query errors
使用Mybatis Plus打包批量查询错误
场景
我需要封装一个可以根据对象列表进行查询的通用方法,所以写了如下方法。
注意:这个class继承了IServiceImpl
Public List<T> listBatchByEntityList(List<T> entityList) {
Try (final SqlSession batchSqlSession = sqlSessionBatch()) {
Final int size = entityList.size();
Final int batchSize = 30;
Final List<T> result = new ArrayList<>();
For (int i = 0; i < size; i++) {
Final String sqlStatement = sqlStatement(SqlMethod.SELECT_LIST);
Final List<T> list = batchSqlSession.selectList(sqlStatement, new EntityWrapper<>(entityList.get(i)));
result.addAll(list);
If (i >= 1 && i % batchSize == 0) {
batchSqlSession.flushStatements();
}
}
batchSqlSession.flushStatements();
Return result;
} catch (Exception e) {
Throw new GlobalException("Error: Cannot execute listBatchByEntityList Method. Cause", e);
}
}
但是运行时出现异常
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.mapper.EntityWrapper'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.mapper.EntityWrapper'
At org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
At org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
At org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
At com.zx.idc.ds.common.service.impl.BaseServiceImpl.listBatchByEntityList(BaseServiceImpl.java:54)
... 37 more
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.mapper.EntityWrapper'
At org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:419)
At org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
At org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
At org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
At org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
At org.apache.ibatis.scripting.xmltags.DynamicContext$ContextMap.get(DynamicContext.java:94)
At org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:108)
At org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2685)
At org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114)
At org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
At org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
At org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50)
At org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
At org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
At org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61)
At org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
At org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
At org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:470)
At org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:434)
At org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44)
At org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
At org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
At org.apache.ibatis.scripting.xmltags.ChooseSqlNode.apply(ChooseSqlNode.java:35)
At org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33)
At org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41)
At org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292)
At org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:134)
At org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 39 more
有没有人遇到过这种情况?
抱歉回复晚了。
问题是:您调用了 batchSqlSession.selectList 并直接使用了参数,
但是在mybatis-plus(2.x)baseMapper.selectList(@Param("ew")Wrapper ew)中的方法,参数有注解@Param,会被[=35处理=](第 121 行 @mybatis-3.4.6)并包装为 ParamMap。
所以解析很简单:
一些代码更改:
final Map<String, Object> param = new MapperMethod.ParamMap<Object>();
EntityWrapper<T> ew = new EntityWrapper<>(entityList.get(i));
param.put("ew", ew);
param.put("param1", ew);
final List<T> list = batchSqlSession.selectList(sqlStatement, param);
完整代码如下:
public List<T> listBatchByEntityList(List<T> entityList) {
try (final SqlSession batchSqlSession = sqlSessionBatch()) {
final int size = entityList.size();
final int batchSize = 30;
final List<T> result = new ArrayList<>();
for (int i = 0; i < size; i++) {
final String sqlStatement = sqlStatement(SqlMethod.SELECT_LIST);
final Map<String, Object> param = new MapperMethod.ParamMap<Object>();
EntityWrapper<T> ew = new EntityWrapper<>(entityList.get(i));
param.put("ew", ew);
param.put("param1", ew);
final List<T> list = batchSqlSession.selectList(sqlStatement, param);
result.addAll(list);
if (i >= 1 && i % batchSize == 0) {
batchSqlSession.flushStatements();
}
}
batchSqlSession.flushStatements();
return result;
} catch (Exception e) {
throw new GlobalException("Error: Cannot execute listBatchByEntityList Method. Cause", e);
}
}
测试正常。
顺便说一句,请随时在 https://github.com/baomidou/mybatis-plus/issues 上提出问题,无论使用 mybatis-plus
遇到什么错误
此致
来自Mybatis-plus的会员:)
使用Mybatis Plus打包批量查询错误
场景
我需要封装一个可以根据对象列表进行查询的通用方法,所以写了如下方法。 注意:这个class继承了IServiceImpl
Public List<T> listBatchByEntityList(List<T> entityList) {
Try (final SqlSession batchSqlSession = sqlSessionBatch()) {
Final int size = entityList.size();
Final int batchSize = 30;
Final List<T> result = new ArrayList<>();
For (int i = 0; i < size; i++) {
Final String sqlStatement = sqlStatement(SqlMethod.SELECT_LIST);
Final List<T> list = batchSqlSession.selectList(sqlStatement, new EntityWrapper<>(entityList.get(i)));
result.addAll(list);
If (i >= 1 && i % batchSize == 0) {
batchSqlSession.flushStatements();
}
}
batchSqlSession.flushStatements();
Return result;
} catch (Exception e) {
Throw new GlobalException("Error: Cannot execute listBatchByEntityList Method. Cause", e);
}
}
但是运行时出现异常
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.mapper.EntityWrapper'
### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.mapper.EntityWrapper'
At org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
At org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
At org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
At com.zx.idc.ds.common.service.impl.BaseServiceImpl.listBatchByEntityList(BaseServiceImpl.java:54)
... 37 more
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.mapper.EntityWrapper'
At org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:419)
At org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:164)
At org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162)
At org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
At org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
At org.apache.ibatis.scripting.xmltags.DynamicContext$ContextMap.get(DynamicContext.java:94)
At org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:108)
At org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2685)
At org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114)
At org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
At org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
At org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50)
At org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
At org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
At org.apache.ibatis.ognl.ASTAnd.getValueBody(ASTAnd.java:61)
At org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
At org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
At org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:470)
At org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:434)
At org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44)
At org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
At org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
At org.apache.ibatis.scripting.xmltags.ChooseSqlNode.apply(ChooseSqlNode.java:35)
At org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33)
At org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41)
At org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292)
At org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:134)
At org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
... 39 more
有没有人遇到过这种情况?
抱歉回复晚了。
问题是:您调用了 batchSqlSession.selectList 并直接使用了参数, 但是在mybatis-plus(2.x)baseMapper.selectList(@Param("ew")Wrapper ew)中的方法,参数有注解@Param,会被[=35处理=](第 121 行 @mybatis-3.4.6)并包装为 ParamMap。
所以解析很简单:
一些代码更改:
final Map<String, Object> param = new MapperMethod.ParamMap<Object>();
EntityWrapper<T> ew = new EntityWrapper<>(entityList.get(i));
param.put("ew", ew);
param.put("param1", ew);
final List<T> list = batchSqlSession.selectList(sqlStatement, param);
完整代码如下:
public List<T> listBatchByEntityList(List<T> entityList) {
try (final SqlSession batchSqlSession = sqlSessionBatch()) {
final int size = entityList.size();
final int batchSize = 30;
final List<T> result = new ArrayList<>();
for (int i = 0; i < size; i++) {
final String sqlStatement = sqlStatement(SqlMethod.SELECT_LIST);
final Map<String, Object> param = new MapperMethod.ParamMap<Object>();
EntityWrapper<T> ew = new EntityWrapper<>(entityList.get(i));
param.put("ew", ew);
param.put("param1", ew);
final List<T> list = batchSqlSession.selectList(sqlStatement, param);
result.addAll(list);
if (i >= 1 && i % batchSize == 0) {
batchSqlSession.flushStatements();
}
}
batchSqlSession.flushStatements();
return result;
} catch (Exception e) {
throw new GlobalException("Error: Cannot execute listBatchByEntityList Method. Cause", e);
}
}
测试正常。
顺便说一句,请随时在 https://github.com/baomidou/mybatis-plus/issues 上提出问题,无论使用 mybatis-plus
遇到什么错误此致
来自Mybatis-plus的会员:)