欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis xml文件中statementType类型

程序员文章站 2022-04-22 10:36:02
...
xml文件示例如下:

<select id="selectMultiObject" parameterType="java.lang.String" resultType="java.util.HashMap"
            statementType="PREPARED">
       ${parameter}
</select>


<select id="selectObject" parameterType="java.lang.String" resultType="java.util.HashMap" statementType="STATEMENT">
       ${parameter}
</select>

\org\apache\ibatis\executor\statement\RoutingStatementHandler.java源码如下:    

public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {

  switch (ms.getStatementType()) {
    case STATEMENT:
      delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
      break;
    case PREPARED:
      delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
      break;
    case CALLABLE:
      delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
      break;
    default:
      throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
  }

}

由xml配置文件和源码文件可知,根据statementType来判断执行哪个方法。

取值说明: 
1、STATEMENT:直接操作sql,不进行预编译,获取数据:$—Statement ,如果这个时候使用占位符的话,就会报错,执行sql异常,因为不能翻译占位符,如select * from student limit ? ,不能正确执行,应该换成PREPARED这种就好了
2、PREPARED:预处理,参数,进行预编译,获取数据:#—–PreparedStatement:默认 
3、CALLABLE:执行存储过程————CallableStatement