2013-04-22 6 views
6

में गतिशील रूप से एसक्यूएल क्वेरी को अवरुद्ध करने और बदलने के लिए कैसे मैं अपनी परियोजना में एसक्यूएल क्वेरी करने के लिए mybatis का उपयोग करता हूं। गतिशील रूप से कुछ लागू करने के लिए मुझे निष्पादन से पहले एसक्यूएल क्वेरी को अवरुद्ध करने की आवश्यकता है। मैं @Interseptors के बारे में इस तरह पढ़ा है:mybatis

@Intercepts({@Signature(type= Executor.class, method = "query", args = {...})}) 
public class ExamplePlugin implements Interceptor { 
    public Object intercept(Invocation invocation) throws Throwable { 
    return invocation.proceed(); 
    } 
    public Object plugin(Object target) { 
    return Plugin.wrap(target, this); 
    } 
    public void setProperties(Properties properties) { 
    } 
} 

और यह वास्तव में फांसी को बीच में रोक है, लेकिन वहाँ एसक्यूएल क्वेरी को बदलने के लिए के बाद से उपयुक्त क्षेत्र लिखने योग्य नहीं है कोई रास्ता नहीं है। क्या मुझे एसक्यूएल क्वेरी को प्रतिस्थापित करने के लिए मैन्युअल रूप से पूरे ऑब्जेक्ट का नया उदाहरण बनाना चाहिए? इसे गतिशील रूप से बदलने के लिए क्वेरी निष्पादन को रोकने के लिए सही जगह कहां है? धन्यवाद।

उत्तर

0

आप एक स्ट्रिंग टेम्पलेट लायब्रेरी (जैसे वेग, हैंडल, मूंछें) का उपयोग कर आप

तारीख करने के लिए के रूप में मदद करने के लिए, वहाँ भी MyBatis-वेग (http://mybatis.github.io/velocity-scripting/) है मदद करने के लिए एसक्यूएल के लिए पटकथा करने के लिए विचार कर सकते हैं ।

0

परिवर्तन आप बनाना चाहते आधार पर, आप mybatis की dynamic sql सुविधा का उपयोग कर सकते हैं 3

4

मुझे आशा है कि यह तुम्हारी मदद करेगा:

@Intercepts({ @Signature(type = Executor.class, method = "query", args = { 
     MappedStatement.class, Object.class, RowBounds.class, 
     ResultHandler.class 
    }) 
}) 
public class SelectCountSqlInterceptor2 implements Interceptor 
{ 
    public static String COUNT = "_count"; 
    private static int MAPPED_STATEMENT_INDEX = 0; 
    private static int PARAMETER_INDEX = 1; 
    @Override 
    public Object intercept(Invocation invocation) throws Throwable 
    { 
     processCountSql(invocation.getArgs()); 
     return invocation.proceed(); 
    } 
    @SuppressWarnings("rawtypes") 
    private void processCountSql(final Object[] queryArgs) 
    { 
     if (queryArgs[PARAMETER_INDEX] instanceof Map) 
     { 
      Map parameter = (Map) queryArgs[PARAMETER_INDEX]; 
      if (parameter.containsKey(COUNT)) 
      { 
       MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX]; 
       BoundSql boundSql = ms.getBoundSql(parameter); 
       String sql = ms.getBoundSql(parameter).getSql().trim(); 
       BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), 
                getCountSQL(sql), boundSql.getParameterMappings(), 
                boundSql.getParameterObject()); 
       MappedStatement newMs = copyFromMappedStatement(ms, 
             new OffsetLimitInterceptor.BoundSqlSqlSource(newBoundSql)); 
       queryArgs[MAPPED_STATEMENT_INDEX] = newMs; 
      } 
     } 
    } 
    // see: MapperBuilderAssistant 
    @SuppressWarnings({ "unchecked", "rawtypes" }) 
    private MappedStatement copyFromMappedStatement(MappedStatement ms, 
      SqlSource newSqlSource) 
    { 
     Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms 
       .getId(), newSqlSource, ms.getSqlCommandType()); 
     builder.resource(ms.getResource()); 
     builder.fetchSize(ms.getFetchSize()); 
     builder.statementType(ms.getStatementType()); 
     builder.keyGenerator(ms.getKeyGenerator()); 
     // setStatementTimeout() 
     builder.timeout(ms.getTimeout()); 
     // setParameterMap() 
     builder.parameterMap(ms.getParameterMap()); 
     // setStatementResultMap() 
     List<ResultMap> resultMaps = new ArrayList<ResultMap>(); 
     String id = "-inline"; 
     if (ms.getResultMaps() != null) 
     { 
      id = ms.getResultMaps().get(0).getId() + "-inline"; 
     } 
     ResultMap resultMap = new ResultMap.Builder(null, id, Long.class, 
       new ArrayList()).build(); 
     resultMaps.add(resultMap); 
     builder.resultMaps(resultMaps); 
     builder.resultSetType(ms.getResultSetType()); 
     // setStatementCache() 
     builder.cache(ms.getCache()); 
     builder.flushCacheRequired(ms.isFlushCacheRequired()); 
     builder.useCache(ms.isUseCache()); 
     return builder.build(); 
    } 
    private String getCountSQL(String sql) 
    { 
     String lowerCaseSQL = sql.toLowerCase().replace("\n", " ").replace("\t", " "); 
     int index = lowerCaseSQL.indexOf(" order "); 
     if (index != -1) 
     { 
      sql = sql.substring(0, index); 
     } 
     return "SELECT COUNT(*) from (select 1 as col_c " + sql.substring(lowerCaseSQL.indexOf(" from ")) + ") cnt"; 
    } 
    @Override 
    public Object plugin(Object target) 
    { 
     return Plugin.wrap(target, this); 
    } 
    @Override 
    public void setProperties(Properties properties) 
    { 
    } 
} 
संबंधित मुद्दे