package com.taover.repository; import java.util.HashMap; import java.util.Map; import org.springframework.util.StringUtils; public class UtilsSql { public static String[] splitCoreSql(String coreSql) throws Exception { //去除''内的信息 String coreSqlRemoveQuate = replaceSelectAndFromBetweenTopCornerMark(coreSql.toUpperCase()); int fromIndex = calcFromIndex(coreSqlRemoveQuate); if(fromIndex > -1) { return new String[] {coreSql.substring(0, fromIndex), coreSql.substring(fromIndex, coreSql.length())}; }else { throw new Exception("未找到FROM子句"); } } private static int calcFromIndex(String coreSqlUpper) { //计算位置 int selectSubSqlNum = 0; int currWindowIndex = 0; for(currWindowIndex=0; (currWindowIndex+6) -1) { result.append(sql.substring(iMark, currIndex)); } return result.toString(); } /** * 获取排序字符串 * @param sort * @param order * @param columnPreffix * @return */ public static String getSortCondition(String sort, String order, String columnPreffix){ String underScoreSort = UtilsString.underscoreName(sort).toLowerCase(); if(StringUtils.isEmpty(columnPreffix)){ columnPreffix = ""; } String sortCondition = " "; if(!StringUtils.isEmpty(underScoreSort)){ sortCondition = " order by "; String[] sortArr = underScoreSort.split(","); if(StringUtils.isEmpty(order)){ sortCondition += columnPreffix+sortArr[0]+" DESC "; for(int i=1; i createPage(int page, int size, int total, Object data){ Map pageData = new HashMap(); pageData.put("page", page); pageData.put("rows", data); pageData.put("size", size); pageData.put("total", total); return pageData; } public static void main(String[] args) { try { String[] data = splitCoreSql("select t,(select * as 'fromCActio', dd as 'seelctsele\'ctd' from t) from www where 223"); System.out.println(data[0]); System.out.println(data[1]); } catch (Exception e) { e.printStackTrace(); } } }