org.apache.ibatis.builder.SqlSourceBuilder Java Examples

The following examples show how to use org.apache.ibatis.builder.SqlSourceBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DynamicSqlSource.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Override
 public BoundSql getBoundSql(Object parameterObject) {
   //生成一个动态上下文
   DynamicContext context = new DynamicContext(configuration, parameterObject);
//这里SqlNode.apply只是将${}这种参数替换掉,并没有替换#{}这种参数
   rootSqlNode.apply(context);
//调用SqlSourceBuilder
   SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
   Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
//SqlSourceBuilder.parse,注意这里返回的是StaticSqlSource,解析完了就把那些参数都替换成?了,也就是最基本的JDBC的SQL写法
   SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
//看似是又去递归调用SqlSource.getBoundSql,其实因为是StaticSqlSource,所以没问题,不是递归调用
   BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
   for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
     boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
   }
   return boundSql;
 }
 
Example #2
Source File: ProviderSqlSource.java    From mybaties with Apache License 2.0 6 votes vote down vote up
public ProviderSqlSource(Configuration config, Object provider) {
  String providerMethodName = null;
  try {
    this.sqlSourceParser = new SqlSourceBuilder(config);
    this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);
    providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);

    for (Method m : this.providerType.getMethods()) {
      if (providerMethodName.equals(m.getName())) {
        if (m.getParameterTypes().length < 2
            && m.getReturnType() == String.class) {
          this.providerMethod = m;
          this.providerTakesParameterObject = m.getParameterTypes().length == 1;
        }
      }
    }
  } catch (Exception e) {
    throw new BuilderException("Error creating SqlSource for SqlProvider.  Cause: " + e, e);
  }
  if (this.providerMethod == null) {
    throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"
        + providerMethodName + "' not found in SqlProvider '" + this.providerType.getName() + "'.");
  }
}
 
Example #3
Source File: DynamicSqlSource.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Override
 public BoundSql getBoundSql(Object parameterObject) {
   //生成一个动态上下文
   DynamicContext context = new DynamicContext(configuration, parameterObject);
//这里SqlNode.apply只是将${}这种参数替换掉,并没有替换#{}这种参数
   rootSqlNode.apply(context);
//调用SqlSourceBuilder
   SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
   Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
//SqlSourceBuilder.parse,注意这里返回的是StaticSqlSource,解析完了就把那些参数都替换成?了,也就是最基本的JDBC的SQL写法
   SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
//看似是又去递归调用SqlSource.getBoundSql,其实因为是StaticSqlSource,所以没问题,不是递归调用
   BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
   for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
     boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
   }
   return boundSql;
 }
 
Example #4
Source File: ProviderSqlSource.java    From mybatis with Apache License 2.0 6 votes vote down vote up
public ProviderSqlSource(Configuration config, Object provider) {
  String providerMethodName = null;
  try {
    this.sqlSourceParser = new SqlSourceBuilder(config);
    this.providerType = (Class<?>) provider.getClass().getMethod("type").invoke(provider);
    providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);

    for (Method m : this.providerType.getMethods()) {
      if (providerMethodName.equals(m.getName())) {
        if (m.getParameterTypes().length < 2
            && m.getReturnType() == String.class) {
          this.providerMethod = m;
          this.providerTakesParameterObject = m.getParameterTypes().length == 1;
        }
      }
    }
  } catch (Exception e) {
    throw new BuilderException("Error creating SqlSource for SqlProvider.  Cause: " + e, e);
  }
  if (this.providerMethod == null) {
    throw new BuilderException("Error creating SqlSource for SqlProvider. Method '"
        + providerMethodName + "' not found in SqlProvider '" + this.providerType.getName() + "'.");
  }
}
 
Example #5
Source File: SqlSourceBuilderTest.java    From mybatis-test with Apache License 2.0 5 votes vote down vote up
@Test
    public void test() {
//        String sql = "SELECT * FROM Author WHERE name = #{name} AND age = #{age}";
        String sql = "SELECT * FROM Author WHERE age = #{age,javaType=int,jdbcType=NUMERIC}";
        SqlSourceBuilder sqlSourceBuilder = new SqlSourceBuilder(new Configuration());
        SqlSource sqlSource = sqlSourceBuilder.parse(sql, Author.class, new HashMap<>());
        BoundSql boundSql = sqlSource.getBoundSql(new Author());

        System.out.println(String.format("SQL: %s\n", boundSql.getSql()));
        System.out.println(String.format("ParameterMappings: %s", boundSql.getParameterMappings()));
    }
 
Example #6
Source File: AutoMapperInterceptor.java    From mybatis.flying with Apache License 2.0 4 votes vote down vote up
private SqlSource buildSqlSource(Configuration configuration, String originalSql, Class<?> parameterType) {
	SqlSourceBuilder builder = new SqlSourceBuilder(configuration);
	return builder.parse(originalSql, parameterType, null);
}
 
Example #7
Source File: RawSqlSource.java    From mybaties with Apache License 2.0 4 votes vote down vote up
public RawSqlSource(Configuration configuration, String sql, Class<?> parameterType) {
  SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
  Class<?> clazz = parameterType == null ? Object.class : parameterType;
  sqlSource = sqlSourceParser.parse(sql, clazz, new HashMap<String, Object>());
}
 
Example #8
Source File: RawSqlSource.java    From mybatis with Apache License 2.0 4 votes vote down vote up
public RawSqlSource(Configuration configuration, String sql, Class<?> parameterType) {
  SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
  Class<?> clazz = parameterType == null ? Object.class : parameterType;
  sqlSource = sqlSourceParser.parse(sql, clazz, new HashMap<String, Object>());
}