首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Mybatis Plus打包批量查询错误

使用Mybatis Plus打包批量查询错误
EN

Stack Overflow用户
提问于 2019-02-25 17:37:15
回答 1查看 372关注 0票数 0

使用Mybatis Plus打包批量查询错误

场景

我需要封装一个通用的方法,可以根据对象列表进行查询,所以我写了以下方法。注意:这个类继承IServiceImpl

代码语言:javascript
复制
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);
    }
}

但是,在运行时发生了异常。

代码语言:javascript
复制
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

有没有人遇到过这种情况?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-30 09:08:18

抱歉,回复晚了。

问题是:您调用了batchSqlSession.selectList并直接使用了参数,但在baseMapper.selectList(@Param("ew") Wrapper ew)中的方法mybatis-plus(2.x)中,参数具有注释@Param,它将由mybatis 121 @mybatis-3.4.6处理并包装为ParamMap。

所以解决方法很简单:

一些代码更改:

代码语言:javascript
复制
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);

完整代码如下:

代码语言:javascript
复制
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的成员:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54863195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档