|
@@ -0,0 +1,66 @@
|
|
|
+package edu.travel.datasource;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
|
+import net.sf.jsqlparser.JSQLParserException;
|
|
|
+import net.sf.jsqlparser.expression.Expression;
|
|
|
+import net.sf.jsqlparser.expression.Parenthesis;
|
|
|
+import net.sf.jsqlparser.expression.StringValue;
|
|
|
+import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
|
|
+import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
|
|
+import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
|
+import net.sf.jsqlparser.schema.Column;
|
|
|
+import net.sf.jsqlparser.statement.Statement;
|
|
|
+import net.sf.jsqlparser.statement.select.PlainSelect;
|
|
|
+import net.sf.jsqlparser.statement.select.Select;
|
|
|
+import net.sf.jsqlparser.statement.select.SelectBody;
|
|
|
+import org.apache.ibatis.executor.Executor;
|
|
|
+import org.apache.ibatis.mapping.BoundSql;
|
|
|
+import org.apache.ibatis.mapping.MappedStatement;
|
|
|
+import org.apache.ibatis.session.ResultHandler;
|
|
|
+import org.apache.ibatis.session.RowBounds;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.sql.SQLException;
|
|
|
+
|
|
|
+public class ProjectInterceptor implements InnerInterceptor {
|
|
|
+ @Override
|
|
|
+ public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
|
|
|
+ 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);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|