Java Code Examples for org.apache.ibatis.mapping.SqlSource#getBoundSql()

The following examples show how to use org.apache.ibatis.mapping.SqlSource#getBoundSql() . 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: RecordDelete.java    From Aooms with Apache License 2.0 6 votes vote down vote up
@Override
public void process() {
    MappedStatement mappedStatement = MetaObjectAssistant.getMappedStatement(metaObject);
    Object parameterObject = MetaObjectAssistant.getParameterObject(metaObject);
    Record record = (Record) parameterObject;

    String tableName = record.getGeneral(MyBatisConst.TABLE_NAME_PLACEHOLDER);
    String pkName = record.getGeneralOrDefault(MyBatisConst.TABLE_PK_NAME_PLACEHOLDER, AoomsVar.ID);
    Object pkValue = record.get(pkName);

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(" delete from ");
    stringBuilder.append(tableName); // tableName
    stringBuilder.append(" where "+ pkName +" = #{"+ pkName +"} ");
    String sql = stringBuilder.toString();

   // SqlSource sqlSource = new XMLLanguageDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class);
    Configuration configuration = MetaObjectAssistant.getConfiguration(metaObject);
    SqlSource sqlSource = configuration.getLanguageRegistry().getDefaultDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class);
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);

    MetaObjectAssistant.setDelegateBoundSql(metaObject,boundSql);
    MetaObjectAssistant.setDelegateParameterHandlerBoundSql(metaObject,boundSql);
}
 
Example 2
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 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: RecordInsert.java    From Aooms with Apache License 2.0 5 votes vote down vote up
@Override
public void process() {
    MappedStatement mappedStatement = MetaObjectAssistant.getMappedStatement(metaObject);
    Object parameterObject = MetaObjectAssistant.getParameterObject(metaObject);
    Record record = (Record) parameterObject;
    String tableName = record.getGeneral(MyBatisConst.TABLE_NAME_PLACEHOLDER);
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(" insert into ");
    stringBuilder.append(tableName); // tableName
    stringBuilder.append(" ({}) ");
    stringBuilder.append(" values ");
    stringBuilder.append(" ({}) ");

    StringBuilder columns = new StringBuilder();
    StringBuilder values = new StringBuilder();

    int index = 0;
    Iterator<String> keyIterator = record.keySet().iterator();
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();
        if (index > 0) {
            columns.append(",");
            values.append(",");
        }
        columns.append(key);
        values.append("#{").append(key).append("}");
        index++;
    }

    String sql = StrUtil.format(stringBuilder, columns, values);
    Configuration configuration = MetaObjectAssistant.getConfiguration(metaObject);
    SqlSource sqlSource = configuration.getLanguageRegistry().getDefaultDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class);
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    //metaObject.setValue("delegate.boundSql", boundSql);
    //metaObject.setValue("delegate.parameterHandler.boundSql", boundSql);

    MetaObjectAssistant.setDelegateBoundSql(metaObject,boundSql);
    MetaObjectAssistant.setDelegateParameterHandlerBoundSql(metaObject,boundSql);
}
 
Example 5
Source File: RecordUpdate.java    From Aooms with Apache License 2.0 5 votes vote down vote up
@Override
public void process() {
    MappedStatement mappedStatement = MetaObjectAssistant.getMappedStatement(metaObject);
    Object parameterObject = MetaObjectAssistant.getParameterObject(metaObject);
    Record record = (Record) parameterObject;

    String tableName = record.getGeneral(MyBatisConst.TABLE_NAME_PLACEHOLDER);
    String pkName = String.valueOf(record.getOrDefault(MyBatisConst.TABLE_PK_NAME_PLACEHOLDER, AoomsVar.ID));
    Object pkValue = record.get(pkName);

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(" update ");
    stringBuilder.append(tableName); // tableName
    stringBuilder.append(" set {} ");
    stringBuilder.append(" where "+ pkName +" = #{"+ pkName +"} ");

    StringBuilder columns = new StringBuilder();
    int index = 0;
    Iterator<String> keyIterator = record.keySet().iterator();
    while (keyIterator.hasNext()) {
        String key = keyIterator.next();
        if (index > 0) {
            columns.append(",");
        }
        columns.append(key).append(" = ").append("#{").append(key).append("}");
        index++;
    }

    String sql = StrUtil.format(stringBuilder, columns);
    //SqlSource sqlSource = new XMLLanguageDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class);
    Configuration configuration = MetaObjectAssistant.getConfiguration(metaObject);
    SqlSource sqlSource = configuration.getLanguageRegistry().getDefaultDriver().createSqlSource(mappedStatement.getConfiguration(), sql, Map.class);
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);

    MetaObjectAssistant.setDelegateBoundSql(metaObject,boundSql);
    MetaObjectAssistant.setDelegateParameterHandlerBoundSql(metaObject,boundSql);
}
 
Example 6
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 7
Source File: VelocitySqlSource.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public BoundSql getBoundSql(Object parameterObject) {
  Map<String, Object> bindings = createBindings(parameterObject, configuration);
  VelocityContext context = new VelocityContext(bindings);
  StringWriter sw = new StringWriter();
  script.merge(context, sw);
  VelocitySqlSourceBuilder sqlSourceParser = new VelocitySqlSourceBuilder(configuration);
  Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
  SqlSource sqlSource = sqlSourceParser.parse(sw.toString(), parameterType);
  BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
  for (Map.Entry<String, Object> entry : bindings.entrySet()) {
    boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
  }
  return boundSql;

}
 
Example 8
Source File: VelocitySqlSource.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public BoundSql getBoundSql(Object parameterObject) {
  Map<String, Object> bindings = createBindings(parameterObject, configuration);
  VelocityContext context = new VelocityContext(bindings);
  StringWriter sw = new StringWriter();
  script.merge(context, sw);
  VelocitySqlSourceBuilder sqlSourceParser = new VelocitySqlSourceBuilder(configuration);
  Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
  SqlSource sqlSource = sqlSourceParser.parse(sw.toString(), parameterType);
  BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
  for (Map.Entry<String, Object> entry : bindings.entrySet()) {
    boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
  }
  return boundSql;

}
 
Example 9
Source File: ProviderSqlSource.java    From mybaties with Apache License 2.0 4 votes vote down vote up
@Override
public BoundSql getBoundSql(Object parameterObject) {
  SqlSource sqlSource = createSqlSource(parameterObject);
  return sqlSource.getBoundSql(parameterObject);
}
 
Example 10
Source File: ProviderSqlSource.java    From mybatis with Apache License 2.0 4 votes vote down vote up
@Override
public BoundSql getBoundSql(Object parameterObject) {
  SqlSource sqlSource = createSqlSource(parameterObject);
  return sqlSource.getBoundSql(parameterObject);
}