mybatis-book / book

MyBatis 从入门到精通
1.33k stars 579 forks source link

mybatis添加分页拦截器后出现的取值问题 #18

Closed 460541922github closed 7 years ago

460541922github commented 7 years ago

问题代码如下:

#{item}

由于项目中用到了分页拦截器,一旦有分页的sql中,用#{}获取值都会报错( org.apache.ibatis.binding.BindingException: Parameter '__frch_item_0' not found.),换成${}这种方式就好了,请问什么原因?

abel533 commented 7 years ago

这个错只有 foreach 会出现,你分页用的什么版本?

460541922github commented 7 years ago
if(page.getAutoCount() == 0) {
                    queryArgs[0] = this.clone(ms, ms.getBoundSql(parameterObject), page);
                }

                Future listFuture1 = this.call(new Callable() {
                    public List call() throws Exception {
                        return (List)invocation.proceed();
                    }
                }, this.asyncCount);
                page.setResult((List)listFuture1.get());
                if(page.getAutoCount() == 0) {
                    Callable countTask = new Callable() {
                        public Object call() throws Exception {
                            Configuration configuration = ms.getConfiguration();
                            String countSql = PageLimitInterceptor.this.dialect.getCountSql(boundSql.getSql());
                            if(PageLimitInterceptor.logger.isDebugEnabled()) {
                                PageLimitInterceptor.logger.debug("生成count语句: " + countSql);
                            }

                            PreparedStatement countStmt = executor.getTransaction().getConnection().prepareStatement(countSql);
                            BoundSql countBS = PageLimitInterceptor.this.dialect.getBoundSql(configuration, countSql, boundSql);
                            PageLimitInterceptor.this.setParameters(countStmt, configuration, countBS, parameterObject);
                            ResultSet rs = countStmt.executeQuery();
                            long count = 0L;
                            if(rs.next()) {
                                count = rs.getLong(1);
                            }

                            rs.close();
                            countStmt.close();
                            return Long.valueOf(count);
                        }
                    };
                    Future countFuture = this.call(countTask, this.asyncCount);
                    page.setTotalCount(((Long)countFuture.get()).longValue());
                }

                return listFuture1.get();

问题就在这里,你所说的分页版本我暂时不清楚,mybatis 是3.4.1 的,拦截器是自己写的,没有用第三方的

abel533 commented 7 years ago

分页插件的版本?

abel533 commented 7 years ago

不用提供版本了,这个插件不是我写的,有这个问题很正常。。

我写的分页插件地址:https://github.com/pagehelper/Mybatis-PageHelper

460541922github commented 7 years ago

是的

460541922github commented 7 years ago

请问我这个啥原因么,我想知道

abel533 commented 7 years ago

没有处理foreach生成的动态参数。

460541922github commented 7 years ago

是mybatis的问题 还是我的拦截器的问题?这个是我之前网上找的!另外处理foreach生成的动态参数的代码能告诉我位置么

abel533 commented 7 years ago

拦截器的问题

460541922github commented 7 years ago

请教你另外一个问题,前天下载了一个阿里的开发人员注意手册,里面说到update同一个表的的同一条记录时候更新的字段的数目多少影响效率,和我自己理解的就不一样(应该是和数目没关系),update实际做的就是delete 和insert

abel533 commented 7 years ago

这个问题只有大量测试才能看出来效果,不清楚数据库底层如何实现的update。

460541922github commented 7 years ago

哦哦,谢谢了哈