本章中分析update元素的解析。
1 配置文件
1<update id="updateByPrimaryKeySelective" parameterType="cn.vansky.schedule.time.menu.bo.Menu"> 2 <!-- 3 WARNING - @mbggenerated 4 This element is automatically generated by MyBatis Generator, do not modify. 5 This element was generated on Fri Aug 14 16:08:36 CST 2015. 6 --> 7 update tb_menu 8 <set> 9 <if test="menuName != null"> 10 menu_name = #{menuName,jdbcType=VARCHAR}, 11 </if> 12 <if test="menuRemark != null"> 13 menu_remark = #{menuRemark,jdbcType=VARCHAR}, 14 </if> 15 <if test="menuParentId != null"> 16 menu_parent_id = #{menuParentId,jdbcType=INTEGER}, 17 </if> 18 <if test="menuUrl != null"> 19 menu_url = #{menuUrl,jdbcType=VARCHAR}, 20 </if> 21 <if test="isShow != null"> 22 is_show = #{isShow,jdbcType=TINYINT}, 23 </if> 24 <if test="isDelete != null"> 25 is_delete = #{isDelete,jdbcType=TINYINT}, 26 </if> 27 <if test="operationUserName != null"> 28 operation_user_name = #{operationUserName,jdbcType=VARCHAR}, 29 </if> 30 <if test="operationTime != null"> 31 operation_time = #{operationTime,jdbcType=TIMESTAMP}, 32 </if> 33 </set> 34 where Id = #{id,jdbcType=INTEGER} 35</update>
2 方法parseStatementNode
1public void parseStatementNode() { 2 // updateByPrimaryKeySelective 3 String id = context.getStringAttribute("id"); 4 // null 5 String databaseId = context.getStringAttribute("databaseId"); 6 // 第一次检查这里是不通过的,直接跳过 7 if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) return; 8 // null 9 Integer fetchSize = context.getIntAttribute("fetchSize"); 10 // null 11 Integer timeout = context.getIntAttribute("timeout"); 12 // null 13 String parameterMap = context.getStringAttribute("parameterMap"); 14 // cn.vansky.schedule.time.menu.bo.Menu 15 String parameterType = context.getStringAttribute("parameterType"); 16 // class cn.vansky.schedule.time.menu.bo.Menu 17 Class<?> parameterTypeClass = resolveClass(parameterType); 18 // null 19 String resultMap = context.getStringAttribute("resultMap"); 20 // null 21 String resultType = context.getStringAttribute("resultType"); 22 // null 23 String lang = context.getStringAttribute("lang"); 24 // 获取默认的处理对象 25 // org.apache.ibatis.scripting.xmltags.XMLLanguageDriver 26 LanguageDriver langDriver = getLanguageDriver(lang); 27 // null 28 Class<?> resultTypeClass = resolveClass(resultType); 29 // null 30 String resultSetType = context.getStringAttribute("resultSetType"); 31 // PREPARED 32 StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString())); 33 // null 34 ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType); 35 36 // update 37 String nodeName = context.getNode().getNodeName(); 38 // UPDATE 39 SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH)); 40 // false 41 boolean isSelect = sqlCommandType == SqlCommandType.SELECT; 42 // true 43 boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect); 44 // false 45 boolean useCache = context.getBooleanAttribute("useCache", isSelect); 46 // false 47 boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false); 48 49 // Include Fragments before parsing 50 XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant); 51 // 解析<include refid="Base_Column_List" /> 52 includeParser.applyIncludes(context.getNode()); 53 54 // Parse selectKey after includes and remove them. 55 processSelectKeyNodes(id, parameterTypeClass, langDriver); 56 57 // 这里很明显也是动态SQL 58 // org.apache.ibatis.scripting.xmltags.DynamicSqlSource 59 SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass); 60 // null 61 String resultSets = context.getStringAttribute("resultSets"); 62 // null 63 String keyProperty = context.getStringAttribute("keyProperty"); 64 // null 65 String keyColumn = context.getStringAttribute("keyColumn"); 66 // org.apache.ibatis.executor.keygen.NoKeyGenerator 67 KeyGenerator keyGenerator; 68 // updateByPrimaryKeySelective!selectKey 69 String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX; 70 // cn.vansky.schedule.time.menu.dao.MenuMapper.updateByPrimaryKeySelective!selectKey 71 keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true); 72 if (configuration.hasKeyGenerator(keyStatementId)) { 73 keyGenerator = configuration.getKeyGenerator(keyStatementId); 74 } else { 75 keyGenerator = context.getBooleanAttribute("useGeneratedKeys", 76 configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType)) 77 ? new Jdbc3KeyGenerator() : new NoKeyGenerator(); 78 } 79 80 builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, 81 fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, 82 resultSetTypeEnum, flushCache, useCache, resultOrdered, 83 keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets); 84 }
updateByPrimaryKeySelective最终的MappedStatement



下面就来看动态SqlSource的属性。

