当前位置: 首页 > 后端技术 > Java

MyBatis中StatementHandler

时间:2023-04-02 00:58:43 Java

StatementHandler负责操作Statement对象和与数据库进行通信。ParameterHandler和ResultSetHandler也用于分配参数和映射结果集。1、StatementHandler中的主要方法是Statementprepare(Connectionconnection)throwsSQLException;voidparameterize(Statementstatement)抛出SQLException;int更新(语句语句)抛出SQLException;Listquery(Statementstatement,ResultHandlerresultHandler)throwsSQLException;prepare:用于创建Statement实例参数size:赋值给sql占位符update:用于通知Statement对象推送插入、更新、删除operationstothedatabasequery:用于通知Statement对象将select操作推送到数据库,并返回相应的查询结果RoutingStatementHandler:StatementHandler接口的默认实现,根据StatementType获取真正的StatementHandler,然后赋值给属性delegatepublicStatementHandlernewStatementHandler(Executorexecutor,MappedStatementmappedStatement,ObjectparameterObject,RowBoundsrowBounds,ResultHandlerresultHandler,BoundSqlboundSql){StatementHandlerstatementHandler=newRoutingStatementHandler(executor,mappedStatement,parameterObject,rowBounds,resultHandler,boundSql);StatementHandlerstatementHandler=(StatementHandler)this.interceptorChain.pluginAll(statementHandler);返回statementHandler;}publicRoutingStatementHandler(Executorexecutor,MappedStatementms,Objectparameter,RowBoundsrowBounds,ResultHandlerresultHandler,BoundSqlboundSql){switch(ms.getStatementType()){caseSTATEMENT:delegate=newSimpleStatementHandler(executor,ms,parameter,rowBounds,结果处理程序,boundSql);休息;casePREPARED:delegate=newPreparedStatementHandler(executor,ms,parameter,rowBounds,resultHandler,boundSql);休息;caseCALLABLE:delegate=newCallableStatementHandler(executor,ms,parameter,rowBounds,resultHandler,boundSql);休息;默认值:抛出新的ExecutorException("未知语句类型:"+ms.getStatementType());}}BaseStatementHandler的三个实现类:SimpleStatementHandler:处理不需要预编译的sql,(不需要处理参数的sql)PreparedStatementHandler:处理需要预编译的sql(解析xml时指定StatementType为Prepared,所以被使用default)CallableStatementHandler:调用存储过程只会在操作数据库(select、update)时得到StatementHandler对象,所以由Executor管理和创建。以SimpleExecutor为例分析生成StatementHandler和Statement的源码try{配置configuration=ms.getConfiguration();StatementHandlerhandler=configuration.newStatementHandler(wrapper,ms,parameter,rowBounds,resultHandler,boundSql);//获取Statement对象stmt=prepareStatement(handler,ms.getStatementLog());返回处理程序。查询(stmt,resultHandler);}最后{关闭语句(stmt);}}获取Statement实例源码分析privateStatementprepareStatement(StatementHandlerhandler,LogstatementLog)throwsSQLException{Statementstmt;连接connection=getConnection(statementLog);//获取Statement实例stmt=handler.prepare(connection,transaction.getTimeout());//stmt进行参数处理//SimpleStatementHandler中的parameterize是一个空方法,因为它只处理简单的sql,带参数的sql由PreparedStatementHandler处理handler.parameterize(stmt);返回stmt;}//调用RoutingStatementHandler方法中的方法,本质上是调用了属性delegate的prepare方法//delegate的赋值上面说了publicStatementprepare(Connectionconnection,IntegertransactionTimeout)throwsSQLException{returndelegate.prepare(连接,事务超时);}//在BaseStatementHandler中调用preparepublicStatementprepare(Connectionconnection,IntegertransactionTimeout)throwsSQLException{ErrorContext.instance().sql(boundSql.getSql());语句语句=无效的;try{//由子类提供实例statement=instantiateStatement(connection);setStatementTimeout(声明,交易超时);setFetchSize(声明);返回声明;}//SimpleStatementHandler提供的实际保护语句instantiateStatement(Connectionconnection)throwsSQLException{if(mappedStatement.getResultSetType()=null){returnconnection.createStatement(mappedStatement.getResultSetType().getValue(),ResultSet.CONCUR_READ_ONLY);}else{returnconnection.createStatement();}}