不知道最新jfinal的paginate方法替换了,正则表达式替换复杂SQL语句字符中的特殊字符,导致的sql语句语法错误。
例如以下这个SQL语句,我从jfinal 2.2 更换到了3.8,都一直报错。
SELECT tab.*, tab1.intCount, tab2.orderCount FROM ( SELECT *, count(DISTINCT(shareId)) shareCount, count(DISTINCT(intUserId)) viewCount FROM v_wxapp_user_share_intention WHERE otherConfig LIKE concat('%' ,?, '%') ORDER BY userId, shareId ) tab LEFT JOIN ( SELECT userId, shareId, count(intentionId) intCount FROM wx_app_intention WHERE deleteSign = ? AND `status` = ? GROUP BY shareId ) tab1 ON tab.shareId = tab1.shareId LEFT JOIN ( SELECT userId, shareId, count(intentionId) orderCount FROM wx_app_intention WHERE deleteSign = ? AND `status` = ? GROUP BY shareId ) tab2 ON tab.shareId = tab2.shareId LIMIT 0, 10
为了能够原封不动地执行原始SQL的分页查询,需要重新实现分页方法,可以使用以下方法。
/** * 重写分页查询方法,采用原生SQL语句 * @param m Modal实例化对象 * @param pageNo 当前页码 * @param pageSize 每页数量 * @param sqlSelect select语句 * @param sqlFrom from语句 * @param paras sql参数 * @return Page对象 */ public Page<M> overidePaginate(M m, int pageNo, int pageSize, String sqlSelect, String sqlFrom, Object[] paras) { int limitBf = (pageNo - 1) * pageSize; int limitAf = pageSize; int totalPage = 0; String sql = sqlSelect + sqlFrom; // 查询所有语句数量 Long allCount = Db.queryLong("select count(1) " + sqlFrom, paras); if (allCount == null) { allCount = new Long(0); } totalPage = (allCount.intValue() + pageSize - 1)/pageSize; List<M> mList = m.find(sql + " limit " + limitBf + "," + limitAf, paras); Page<M> msPage = new Page<>(mList, pageNo, pageSize, totalPage, allCount.intValue()); return msPage; }
留在这儿做个记录,供小伙伴参考。
项目:JFinal