最近在开发一个页面分页的后端代码,使用的分页是mybatis的pagehelper jar组件,其中用到的版本是5.0.1 即 pagehelper-5.0.1.jar;分页时查询SQL语句
select a.* from (SELECT b.id,b.cid,b.customerId,c.opportunityId,b.customerName,b.detailAddress,b.stageId,b.visitDate,b.isIntention,b.productIds,b.creatorId,b.creatorName,b.contactsId,b.contactsName,b.visitItems,b.visitType,b.accompanyVisitorIds,b.createTime,b.modifyTime,c.longitude as cusLongitude,c.latitude as cusLatitude,2 as customerType,c.salesId from tCustomerVisitPlan b inner join tCustomer c on b.customerId = c.id WHERE b.cid=? and b.creatorId=? and b.visitDate=? AND b.isDel=0 UNION ALL SELECT b.id,b.cid,c.customerId,b.opportunityId,b.opportunityName as customerName,b.detailAddress,b.stageId,b.visitDate,1 as isIntention,b.productIds,b.creatorId,b.creatorName,b.contactsId,b.contactsName,b.visitItems,b.visitType,b.accompanyVisitorIds,b.createTime,b.modifyTime,c.longitude as cusLongitude,c.latitude as cusLatitude,1 as customerType,IIf (ISNULL(c.customerId, 0) > 0,d.salesId,c.salesId) AS salesId from tOpportunityVisitPlan b inner join tOpportunity c on b.opportunityId = c.id left join tCustomer d on c.customerId = d.id WHERE b.cid=? and b.creatorId=? and b.visitDate=? AND b.isDel=0) as a order by a.modifyTime desc,a.createTime desc
看到此SQL貌似除了语句长了一些,其他么有什么问题,但是在具体执行查询时竟然报错了~~,以下是错误截图:
**
通过错误截图分析在执行分页查询SQL时,pagehelper中一个PageInterceptor类拦截器会拦截处理sql语句**
@Override
public Object intercept(Invocation invocation) throws Throwable {
try {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
Executor executor = (Executor) invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
//由于逻辑关系,只会进入一次
if(args.length == 4){
//4 个参数时
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
//6 个参数时
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
List resultList;
//调用方法判断是否需要进行分页,如果不需要,直接返回结果
if (!dialect.skip(ms, parameter, rowBounds)) {
//反射获取动态参数
Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql);
//判断是否需要进行 count 查询
if (dialect.beforeCount(ms, parameter, rowBounds)) {
//创建 count 查询的缓存 key
CacheKey countKey = executor.createCacheKey(ms, parameter, RowBounds.DEFAULT, boundSql);
countKey.update(MSUtils.COUNT);
MappedStatement countMs = msCountMap.get(countKey);
if (countMs == null) {
//根据当前的 ms 创建一个返回值为 Long 类型的 ms
countMs = MSUtils.newCountMappedStatement(ms);
msCountMap.put(countKey, countMs);
}
//调用方言获取 count sql
String countSql = dialect.getCountSql(ms, boundSql, parameter, rowBounds, countKey);
countKey.update(countSql);
BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
//当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中
for (String key : additionalParameters.keySet()) {
countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
}
//执行 count 查询
Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
Long count = (Long) ((List) countResultList).get(0);
//处理查询总数
//返回 true 时继续分页查询,false 时直接返回
if (!dialect.afterCount(count, parameter, rowBounds)) {
//当查询总数为 0 时,直接返回空的结果
return dialect.afterPage(new ArrayList(), parameter, rowBounds);
}
}
//判断是否需要进行分页查询
if (dialect.beforePage(ms, parameter, rowBounds)) {
//生成分页的缓存 key
CacheKey pageKey = cacheKey;
//处理参数对象
parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
//调用方言获取分页 sql
String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter);
//设置动态参数
for (String key : additionalParameters.keySet()) {
pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
}
//执行分页查询
resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);
} else {
//不执行分页的情况下,也不执行内存分页
resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
}
} else {
//rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页
resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
}
return dialect.afterPage(resultList, parameter, rowBounds);
} finally {
dialect.afterAll();
}
}
继续往下追踪PageHelper的getCountSql
@Override
public String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {
return autoDialect.getDelegate().getCountSql(ms, boundSql, parameterObject, rowBounds, countKey);
}
继续往下追踪SqlServerDialect的getCountSql
@Override
public String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {
String sql = boundSql.getSql();
String cacheSql = CACHE_COUNTSQL.get(sql);
if (cacheSql != null) {
return cacheSql;
} else {
cacheSql = sql;
}
cacheSql = cacheSql.replaceAll("((?i)with\\s*\\(nolock\\))", WITHNOLOCK);
cacheSql = countSqlParser.getSmartCountSql(cacheSql);
cacheSql = cacheSql.replaceAll(WITHNOLOCK, " with(nolock)");
CACHE_COUNTSQL.put(sql, cacheSql);
return cacheSql;
}
**继续往下追踪CountSqlParser的getSmartCountSql**
/**
* 获取智能的countSql
*
* @param sql
* @param name 列名,默认 0
* @return
*/
public String getSmartCountSql(String sql, String name) {
//校验是否支持该sql
isSupportedSql(sql);
//解析SQL
Statement stmt = null;
//特殊sql不需要去掉order by时,使用注释前缀
if (sql.indexOf(KEEP_ORDERBY) >= 0) {
return getSimpleCountSql(sql);
}
try {
stmt = CCJSqlParserUtil.parse(sql);
} catch (Throwable e) {
//无法解析的用一般方法返回count语句
return getSimpleCountSql(sql);
}
Select select = (Select) stmt;
SelectBody selectBody = select.getSelectBody();
try {
//处理body-去order by
processSelectBody(selectBody);
} catch (Exception e) {
//当 sql 包含 group by 时,不去除 order by
return getSimpleCountSql(sql);
}
//处理with-去order by
processWithItemsList(select.getWithItemsList());
//处理为count查询
sqlToCount(select, name);
String result = select.toString();
return result;
}
继续往下追踪CCJSqlParserUtil的parse方法时出现sql转换错误
public static Statement parse(String sql) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StringReader(sql));
try {
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
}
抛出的JSQLParserException异常:
net.sf.jsqlparser.parser.ParseException: Encountered " “(” "( “” at line 1, column 856.
Was expecting one of:
“AS” …
“DO” …
“ANY” …
“KEY” …
“PERCENT” …
“INTO” …
“FROM” …
“OPEN” …
“WHERE” …
“FOR” …
“XML” …
“UNION” …
“GROUP” …
“ORDER” …
“VALUE” …
“HAVING” …
“VALUES” …
“REPLACE” …
“TRUNCATE” …
“INTERSECT” …
“CAST” …
“EXCEPT” …
“MINUS” …
“OVER” …
“PARTITION” …
“EXTRACT” …
“MATERIALIZED” …
“START” …
“CONNECT” …
“PRIOR” …
“SIBLINGS” …
“COLUMN” …
“NULLS” …
“FIRST” …
“LAST” …
“ROWS” …
“RANGE” …
“FOLLOWING” …
“ROW” …
“SEPARATOR” …
“CASCADE” …
“NO” …
“ACTION” …
<S_IDENTIFIER> …
<S_QUOTED_IDENTIFIER> …
通过分析SQL语句定位到是因为使用了IIF函数解析时遇到特殊字符转换失败导致的,而IIF函数是SQLServer 2012版本之后的发布的函数,目前采用的5.0.1版本暂不支持该函数,但是需要注意的是并不是所有函数都不支持,比如支持FORMAT、CONVERT、ISNULL等函数
那么我们通过错误日志分析到了问题的原因了,那怎么解决呢?
**处理方案如下:**
第一、升级pagehelper jar包的版本到最新的5.1.11
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
第二、保留现有pagehelper jar包的版本不变,删除IIF函数引用,改为case when做逻辑判断
select a.* from (SELECT b.id,b.cid,b.customerId,c.opportunityId,b.customerName,b.detailAddress,b.stageId,b.visitDate,b.isIntention,b.productIds,b.creatorId,b.creatorName,b.contactsId,b.contactsName,b.visitItems,b.visitType,b.accompanyVisitorIds,b.createTime,b.modifyTime,c.longitude as cusLongitude,c.latitude as cusLatitude,2 as customerType,c.salesId from tCustomerVisitPlan b inner join tCustomer c on b.customerId = c.id WHERE b.cid=? and b.creatorId=? and b.visitDate=? AND b.isDel=0 UNION ALL SELECT b.id,b.cid,c.customerId,b.opportunityId,b.opportunityName as customerName,b.detailAddress,b.stageId,b.visitDate,1 as isIntention,b.productIds,b.creatorId,b.creatorName,b.contactsId,b.contactsName,b.visitItems,b.visitType,b.accompanyVisitorIds,b.createTime,b.modifyTime,c.longitude as cusLongitude,c.latitude as cusLatitude,1 as customerType,(case when c.customerId is null or c.customerId=0 then c.salesId else d.salesId end) AS salesId from tOpportunityVisitPlan b inner join tOpportunity c on b.opportunityId = c.id left join tCustomer d on c.customerId = d.id WHERE b.cid=? and b.creatorId=? and b.visitDate=? AND b.isDel=0) as a order by a.modifyTime desc,a.createTime desc