|
@@ -1,6 +1,7 @@
|
|
package edu.travel.datasource;
|
|
package edu.travel.datasource;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
@@ -29,38 +30,40 @@ public class ProjectInterceptor implements InnerInterceptor {
|
|
@Override
|
|
@Override
|
|
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|
|
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|
|
- String originSql = boundSql.getSql();
|
|
|
|
- // 获取 Statement 执行sql的对象
|
|
|
|
- Statement statement = null;
|
|
|
|
- try {
|
|
|
|
- statement = CCJSqlParserUtil.parse(originSql);
|
|
|
|
- } catch (JSQLParserException e) {
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
- }
|
|
|
|
- Select select = (Select) statement;
|
|
|
|
-
|
|
|
|
- SelectBody selectBody = select.getSelectBody();
|
|
|
|
- if (selectBody == null) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- String targetSql = "";
|
|
|
|
- if (selectBody instanceof PlainSelect) {
|
|
|
|
- PlainSelect plainSelect = (PlainSelect) selectBody;
|
|
|
|
- Expression where = plainSelect.getWhere();
|
|
|
|
- EqualsTo projectEq = new EqualsTo();
|
|
|
|
- projectEq.setLeftExpression(new Column("project"));
|
|
|
|
- projectEq.setRightExpression(new StringValue(request.getHeader("project")));
|
|
|
|
- Parenthesis parenthesis = new Parenthesis();
|
|
|
|
- parenthesis.setExpression(where);
|
|
|
|
- AndExpression andExpression = new AndExpression(where, parenthesis);
|
|
|
|
- plainSelect.setWhere(andExpression);
|
|
|
|
- targetSql = plainSelect.toString();
|
|
|
|
- System.out.println(targetSql);
|
|
|
|
- }
|
|
|
|
- // 修改完成的sql 再设置回去
|
|
|
|
- PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
|
|
|
|
- mpBoundSql.sql(targetSql);
|
|
|
|
|
|
+ String project = request.getHeader("project");
|
|
|
|
+ if (StringUtils.isNotBlank(project)) {
|
|
|
|
+ String originSql = boundSql.getSql();
|
|
|
|
+ // 获取 Statement 执行sql的对象
|
|
|
|
+ Statement statement = null;
|
|
|
|
+ try {
|
|
|
|
+ statement = CCJSqlParserUtil.parse(originSql);
|
|
|
|
+ } catch (JSQLParserException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }
|
|
|
|
+ Select select = (Select) statement;
|
|
|
|
|
|
|
|
+ SelectBody selectBody = select.getSelectBody();
|
|
|
|
+ if (selectBody == null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String targetSql = "";
|
|
|
|
+ if (selectBody instanceof PlainSelect) {
|
|
|
|
+ PlainSelect plainSelect = (PlainSelect) selectBody;
|
|
|
|
+ Expression where = plainSelect.getWhere();
|
|
|
|
+ EqualsTo projectEq = new EqualsTo();
|
|
|
|
+ projectEq.setLeftExpression(new Column("project"));
|
|
|
|
+ projectEq.setRightExpression(new StringValue(project));
|
|
|
|
+ Parenthesis parenthesis = new Parenthesis();
|
|
|
|
+ parenthesis.setExpression(where);
|
|
|
|
+ AndExpression andExpression = new AndExpression(where, parenthesis);
|
|
|
|
+ plainSelect.setWhere(andExpression);
|
|
|
|
+ targetSql = plainSelect.toString();
|
|
|
|
+ System.out.println(targetSql);
|
|
|
|
+ }
|
|
|
|
+ // 修改完成的sql 再设置回去
|
|
|
|
+ PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
|
|
|
|
+ mpBoundSql.sql(targetSql);
|
|
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|