使用 Mybatis Plus 封装批量查询出错

使用 Mybatis Plus 封装批量查询出错

环境

  • JDK 1.8
  • Spring Boot 2.0.6.RELEASE
  • mybatis-plus-boot-starter 2.3

场景

吾辈需要封装一个可以根据对象列表查询的通用方法,于是写了下面这样一个方法
注:这个类继承了 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

有人遇到过这种情况么?


附:吾辈看得懂错误,但不知道为什么 Mybatis Plus 查询列表会去找实体包装类 EntityWrapperew,而且 Google 上吾辈没有找到什么有用的答案 https://www.google.com/search...

在 Gitee 仓库那里有人遇到了类似的问题 关于自定义全局注入,wrapper 作为参数如何操作。,但方式不同,吾辈这边根本没在 BaseDao 定义接口

阅读 12.5k
3 个回答

StackOverflow 上有官方开发者进行了回复

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);
    }
}
附: 该答案吾辈并未实测

看报错

There is no getter for property named 'ew' in 'class com.baomidou.mybatisplus.mapper.EntityWrapper'

你配置文件中写了个属性叫ew,你找一下,但是对应的实体类中没有找到对应的getEw()方法

提供一下你使用的mybatis-plus版本号。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题