| | |
| | | import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
| | | import org.apache.ibatis.executor.Executor; |
| | | import org.apache.ibatis.executor.statement.StatementHandler; |
| | | import org.apache.ibatis.mapping.BoundSql; |
| | | import org.apache.ibatis.mapping.MappedStatement; |
| | | import org.apache.ibatis.mapping.ParameterMapping; |
| | | import org.apache.ibatis.plugin.Intercepts; |
| | | import org.apache.ibatis.plugin.Signature; |
| | | import org.apache.ibatis.session.ResultHandler; |
| | | import org.apache.ibatis.session.RowBounds; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.sql.SQLException; |
| | | import java.sql.Statement; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Intercepts({ |
| | | @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}) |
| | | }) |
| | | public class MybatisHmkInterceptor implements InnerInterceptor { |
| | | @Override |
| | | public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { |
| | |
| | | } |
| | | } |
| | | if(parameter instanceof Map){ |
| | | List<ParameterMapping> parameterMappingList=new ArrayList<>(); |
| | | if(sql.contains("community_id = ?")&&((Map<?, ?>) parameter).containsValue(10086L)){ |
| | | List<ParameterMapping> parameterMappings=boundSql.getParameterMappings(); |
| | | parameterMappings.forEach(parameterMapping -> { |
| | | if (parameterMapping.getProperty().equals("communityId")){ |
| | | ParameterMapping parameterMapping1=new ParameterMapping.Builder(ms.getConfiguration(),"communityId",Object.class).build(); |
| | | parameterMappingList.add(parameterMapping1); |
| | | if(sql.contains("community_id = ?")){ |
| | | List<ParameterMapping> parameterMappingList=new ArrayList<>(); |
| | | if(((Map<?, ?>) parameter).containsValue(10086L)){ |
| | | List<ParameterMapping> parameterMappings=boundSql.getParameterMappings(); |
| | | parameterMappings.forEach(parameterMapping -> { |
| | | if (parameterMapping.getProperty().equals("communityId")){ |
| | | ParameterMapping parameterMapping1=new ParameterMapping.Builder(ms.getConfiguration(),parameterMapping.getProperty(),Object.class).build(); |
| | | parameterMappingList.add(parameterMapping1); |
| | | } |
| | | else { |
| | | parameterMappingList.add(parameterMapping); |
| | | } |
| | | }); |
| | | sql=sql.replace("community_id = ?","community_id in (?)"); |
| | | ((Map<?,?>) parameter).replaceAll((k,v)->{ |
| | | if(v.toString().contains("10086")){ |
| | | boundSql.setAdditionalParameter(k.toString(),"30,31"); |
| | | } |
| | | return v; |
| | | }); |
| | | PluginUtils.mpBoundSql(boundSql).parameterMappings(parameterMappingList); |
| | | PluginUtils.mpBoundSql(boundSql).sql(sql); |
| | | } |
| | | else{ |
| | | if(((Map<?, ?>) parameter).containsKey("arg0")){ |
| | | Map<?, ?> param= (Map<?, ?>) boundSql.getAdditionalParameter("_parameter"); |
| | | String finalSql = sql; |
| | | param.forEach((k, v)->{ |
| | | try { |
| | | Field field= v.getClass().getDeclaredField("communityId"); |
| | | if(field!=null){ |
| | | field.setAccessible(true); |
| | | if((long)field.get(v)==10086L){ |
| | | String sql1= finalSql.replace("community_id = ?","community_id in (30,31)"); |
| | | boundSql.getParameterMappings().forEach(parameterMapping -> { |
| | | if(!parameterMapping.getProperty().contains("communityId")){ |
| | | parameterMappingList.add(parameterMapping); |
| | | } |
| | | }); |
| | | PluginUtils.mpBoundSql(boundSql).parameterMappings(parameterMappingList); |
| | | PluginUtils.mpBoundSql(boundSql).sql(sql1); |
| | | } |
| | | } |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | } catch (NoSuchFieldException e) { |
| | | |
| | | } |
| | | }); |
| | | } |
| | | else { |
| | | parameterMappingList.add(parameterMapping); |
| | | } |
| | | }); |
| | | sql=sql.replace("community_id = ?","community_id in (?)"); |
| | | ((Map<?,?>) parameter).replaceAll((k,v)->{ |
| | | if(v.toString().equals("10086")){ |
| | | boundSql.setAdditionalParameter(k.toString(),"30,31"); |
| | | } |
| | | return v; |
| | | }); |
| | | PluginUtils.mpBoundSql(boundSql).parameterMappings(parameterMappingList); |
| | | PluginUtils.mpBoundSql(boundSql).sql(sql); |
| | | } |
| | | } |
| | | } |
| | | InnerInterceptor.super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql); |