@SuppressWarnings("rawtypes") @Override protected BoundSql getPageBoundSql(Object parameterObject) { DynamicContext context; // 由于增加分页参数后会修改parameterObject的值,因此在前面处理时备份该值 // 如果发现参数是Map并且包含该KEY,就使用备份的该值 // 解决bug#25:http://git.oschina.net/free/Mybatis_PageHelper/issues/25 if (parameterObject != null && parameterObject instanceof Map && ((Map) parameterObject).containsKey(ORIGINAL_PARAMETER_OBJECT)) { context = new DynamicContext(configuration, ((Map) parameterObject).get(ORIGINAL_PARAMETER_OBJECT)); } else { context = new DynamicContext(configuration, parameterObject); } rootSqlNode.apply(context); SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings()); sqlSource = new OrderByStaticSqlSource((StaticSqlSource) sqlSource); BoundSql boundSql = sqlSource.getBoundSql(parameterObject); sqlSource = new StaticSqlSource( configuration, parser.getPageSql(boundSql.getSql()), parser.getPageParameterMapping(configuration, boundSql)); boundSql = sqlSource.getBoundSql(parameterObject); // 设置条件参数 for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) { boundSql.setAdditionalParameter(entry.getKey(), entry.getValue()); } return boundSql; }
private SqlSource getSqlSource( MappedStatement ms, SqlSource sqlSource, Object parameterObject, boolean isCount) { if (ms.getSqlSource() instanceof DynamicSqlSource) { MetaObject msObject = MetaObjectUtils.forObject(ms); SqlNode sqlNode = (SqlNode) msObject.getValue("sqlSource.rootSqlNode"); MixedSqlNode mixedSqlNode = null; if (sqlNode instanceof MixedSqlNode) { mixedSqlNode = (MixedSqlNode) sqlNode; } else { List<SqlNode> contents = new ArrayList<SqlNode>(1); contents.add(sqlNode); mixedSqlNode = new MixedSqlNode(contents); } return new CustomerDynamicSqlSource(ms.getConfiguration(), mixedSqlNode, isCount, dialect); } else if (sqlSource instanceof ProviderSqlSource) { return new CustomerProviderSqlSource( ms.getConfiguration(), (ProviderSqlSource) sqlSource, isCount, dialect); } else if (!isCount) { BoundSql boundSql = sqlSource.getBoundSql(parameterObject); return new StaticSqlSource( ms.getConfiguration(), dialect.generatePageSQL(boundSql.getSql()), MybatisUtils.plusTwoParameterToMapping(ms.getConfiguration(), boundSql)); } else { BoundSql boundSql = sqlSource.getBoundSql(parameterObject); return new StaticSqlSource( ms.getConfiguration(), dialect.generateCountSQL(boundSql.getSql()), boundSql.getParameterMappings()); } }
@Override protected BoundSql getDefaultBoundSql(Object parameterObject) { DynamicContext context = new DynamicContext(configuration, parameterObject); rootSqlNode.apply(context); SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings()); sqlSource = new OrderByStaticSqlSource((StaticSqlSource) sqlSource); BoundSql boundSql = sqlSource.getBoundSql(parameterObject); // 设置条件参数 for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) { boundSql.setAdditionalParameter(entry.getKey(), entry.getValue()); } return boundSql; }
@Override protected BoundSql getPageBoundSql(Object parameterObject) { String tempSql = sql; String orderBy = OrderByHelper.getOrderBy(); if (orderBy != null) { tempSql = OrderByParser.converToOrderBySql(sql, orderBy); } tempSql = parser.getPageSql(tempSql); return new BoundSql( configuration, tempSql, parser.getPageParameterMapping(configuration, original.getBoundSql(parameterObject)), parameterObject); }
/** * 修改SqlSource * * @param ms * @param parser * @throws Throwable */ public static void processMappedStatement(MappedStatement ms, Parser parser) throws Throwable { SqlSource sqlSource = ms.getSqlSource(); MetaObject msObject = SystemMetaObject.forObject(ms); SqlSource tempSqlSource = sqlSource; if (sqlSource instanceof OrderBySqlSource) { tempSqlSource = ((OrderBySqlSource) tempSqlSource).getOriginal(); } SqlSource pageSqlSource; if (tempSqlSource instanceof StaticSqlSource) { pageSqlSource = new PageStaticSqlSource((StaticSqlSource) tempSqlSource, parser); } else if (tempSqlSource instanceof RawSqlSource) { pageSqlSource = new PageRawSqlSource((RawSqlSource) tempSqlSource, parser); } else if (tempSqlSource instanceof ProviderSqlSource) { pageSqlSource = new PageProviderSqlSource((ProviderSqlSource) tempSqlSource, parser); } else if (tempSqlSource instanceof DynamicSqlSource) { pageSqlSource = new PageDynamicSqlSource((DynamicSqlSource) tempSqlSource, parser); } else { throw new RuntimeException("无法处理该类型[" + sqlSource.getClass() + "]的SqlSource"); } msObject.setValue("sqlSource", pageSqlSource); // 由于count查询需要修改返回值,因此这里要创建一个Count查询的MS msCountMap.put(ms.getId(), MSUtils.newCountMappedStatement(ms)); }
@Override public BoundSql getBoundSql(Object parameterObject) { return sqlSource.getBoundSql(parameterObject); }