自动添加order by 问题
项目中引入 了PageHelper 组件, 然后 查询的时候,明明没有指定 PageHelper.orderBy();但是在执行sql的时候,sql的结尾却莫名其妙多了 order by id
例子
// mapper
Integer searchCount(QueryBean querybean);
<select id = "searchCount">
select * from s left join b on b.id = s.p_id where s = #{temp}
</select>
然后代码执行之后;报错 提示
*order by id asc ~~~~~
Column ‘id’ in order clause is ambiguous*
明明 没有使用PageHelper.orderBy 功能,也没有自己手写添加order by ,但是依旧在后面添加了order by id , 找了很久 ,自己的分析原因如下
首先看实体类 QueryBean 信息
public class QueryBean {
private int page =1;
private int limit = 20;
private String orderField = "id";
private String orderDirection = "asc";
// 注意此处有 orderBy 字段,且有默认的初始值
private String orderBy = "id asc";
}
注意上面的实体类中包含了 orderBy 字段,且有默认的初始化值;
@Intercepts(@Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
public class PageHelper implements Interceptor {
/**
* Mybatis拦截器方法;当执行查询的时候,此方法会执行
*
* @param invocation 拦截器入参
* @return 返回执行结果
* @throws Throwable 抛出异常
*/
public Object intercept(Invocation invocation) throws Throwable {
if (autoRuntimeDialect) {
SqlUtil sqlUtil = getSqlUtil(invocation);
return sqlUtil.processPage(invocation)