xml gridview控件增删改查_Mybatis之XML如何映射到方法
前言
上文Mybatis之方法如何映射到XML中介紹了Mybatis是如何將方法進行分拆出方法名映射到statementID,參數如何解析成xml中sql所需要的,以及返回類型的處理;本文將從XML端來看是如何同方法端進行映射的。
XML映射類
前兩篇文章中了解到通過Mapper類路徑+方法名映射xxMapper.xml中的namespace+statementID,而namespace+statementID塊其實在初始化的時候在Configuration中保存在MappedStatement中,所以我們在增刪改查的時候都會看到如下代碼:
MappedStatement ms = configuration.getMappedStatement(statement);在Configuration中獲取指定namespace+statementID的MappedStatement,而在Configuration是通過Map維護了對應關系;已最常見的Select語句塊為例,在XML中的配置的結構如下:
其他增刪改除了個別的幾個關鍵字比如:keyProperty,keyColumn等,其他和select標簽類似;再來看一下MappedStatement類中相關的屬性:
public final class MappedStatement { private String resource; private Configuration configuration; private String id; private Integer fetchSize; private Integer timeout; private StatementType statementType; private ResultSetType resultSetType; private SqlSource sqlSource; private Cache cache; private ParameterMap parameterMap; private List resultMaps; private boolean flushCacheRequired; private boolean useCache; private boolean resultOrdered; private SqlCommandType sqlCommandType; private KeyGenerator keyGenerator; private String[] keyProperties; private String[] keyColumns; private boolean hasNestedResultMaps; private String databaseId; private Log statementLog; private LanguageDriver lang; private String[] resultSets; ...省略...}select標簽里面的關鍵字基本可以在類MappedStatement中找到對應的屬性,關于每個屬性代表的含義可以參考官方文檔:https://mybatis.org/mybatis-3/sqlmap-xml.html;除了關鍵字還有sql語句,對應的是MappedStatement中的SqlSource,sql語句有動態和靜態的區別,對應的SqlSource也提供了相關的子類:StaticSqlSource和DynamicSqlSource,相關的sql解析類在XMLScriptBuilder中:
public SqlSource parseScriptNode() { MixedSqlNode rootSqlNode = parseDynamicTags(context); SqlSource sqlSource = null; if (isDynamic) { sqlSource = new DynamicSqlSource(configuration, rootSqlNode); } else { sqlSource = new RawSqlSource(configuration, rootSqlNode, parameterType); } return sqlSource; }具體哪種sql是動態的,哪種是靜態的,相關邏輯在parseDynamicTags中判斷的,此處大致說一下其中的原理:遇到${}和動態標簽如,,則為DynamicSqlSource,否則為StaticSqlSource也就是常見的#{};在解析動態sql的時候Mybatis為每個標簽專門提供了處理類NodeHandler,初始化信息如下:
private void initNodeHandlerMap() { nodeHandlerMap.put("trim總結
以上是生活随笔為你收集整理的xml gridview控件增删改查_Mybatis之XML如何映射到方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim循环下表复制_Vim求生手册,让你
- 下一篇: strcpy会覆盖原来的吗_幽默你真的会