MyBatis整合Spring的实现(16)

本章中分析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的属性。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang