Mybatis-SQL映射解析
Mybatis的映射器是我们用的最多的工具,映射器的作用就是把sql的配置映射成JavaBean。select,update,insert,delete对应在SQL映射文件为几个顶级元素,整理Mybatis官网中的配置的参数如下:
image-20210115161230461.png
在sql映射文件中把平时写的sql语句写成xml形式:
<!--select 形式-->
<select id="findUserById" parameterType="int"
resultType="com.kkb.mybatis.introduction.po.User"
flushCache="false" useCache="true" timeout="10" fetchSize="256" statementType="PREPARED" resultSetType="FORWARD_ONLY">
SELECT * FROM user WHERE id = #{id}
</select>
<!--insert 形式,selectKey为生成主键的元素-->
<insert id="insertAuthor">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}</insert>
Mybatis框架需要读取sql映射文件的配置内容转换成javaBean对象进行使用。
以Mybatis官网中的一个insert语句,对应框架中的结构
image-20210119163302414.png
//把select ,update,insert,delete转换成MappedStatement对象的过程
public void parseStatementNode() {
String id = context.getStringAttribute("id");
String databaseId = context.getStringAttribute("databaseId");
if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
return;
}
Integer fetchSize = context.getIntAttribute("fetchSize");
Integer timeout = context.getIntAttribute("timeout");
//弃用的参数
String parameterMap = context.getStringAttribute("parameterMap");
String parameterType = context.getStringAttribute("parameterType");
Class<?> parameterTypeClass = resolveClass(parameterType);
String resultMap = context.getStringAttribute("resultMap");
String resultType = context.getStringAttribute("resultType");
String lang = context.getStringAttribute("lang");
LanguageDriver langDriver = getLanguageDriver(lang);
Class<?> resultTypeClass = resolveClass(resultType);
String resultSetType = context.getStringAttribute("resultSetType");
StatementType statementType = StatementType.valueOf(context.getStringAttribute("statementType", StatementType.PREPARED.toString()));
ResultSetType resultSetTypeEnum = resolveResultSetType(resultSetType);
String nodeName = context.getNode().getNodeName();
SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
boolean flushCache = context.getBooleanAttribute("flushCache", !isSelect);
boolean useCache = context.getBooleanAttribute("useCache", isSelect);
boolean resultOrdered = context.getBooleanAttribute("resultOrdered", false);
// Include Fragments before parsing
XMLIncludeTransformer includeParser = new XMLIncludeTransformer(configuration, builderAssistant);
includeParser.applyIncludes(context.getNode());
// Parse selectKey after includes and remove them.
//selectKey元素也是一个MappedStatement对象,放入Configuration缓存中
// processSelectKeyNodes过程中的代码:封装成MappedStatement对象,再把MappedStatement 包装近SelectKeyGenerator对象
// MappedStatement keyStatement = configuration.getMappedStatement(id, false);
// configuration.addKeyGenerator(id, new SelectKeyGenerator(keyStatement, executeBefore));
processSelectKeyNodes(id, parameterTypeClass, langDriver);
// Parse the SQL (pre: <selectKey> and <include> were parsed and removed)
SqlSource sqlSource = langDriver.createSqlSource(configuration, context, parameterTypeClass);
String resultSets = context.getStringAttribute("resultSets");
String keyProperty = context.getStringAttribute("keyProperty");
String keyColumn = context.getStringAttribute("keyColumn");
KeyGenerator keyGenerator;
String keyStatementId = id + SelectKeyGenerator.SELECT_KEY_SUFFIX;
//selectKey的id
keyStatementId = builderAssistant.applyCurrentNamespace(keyStatementId, true);
//根据selectKey的id,从configuration获取keyGenerator对象
if (configuration.hasKeyGenerator(keyStatementId)) {
keyGenerator = configuration.getKeyGenerator(keyStatementId);
} else {
keyGenerator = context.getBooleanAttribute("useGeneratedKeys",
configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType))
? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
}
//创建MappedStatemenet对象,并放入Configuration的缓存中
builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType,
fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass,
resultSetTypeEnum, flushCache, useCache, resultOrdered,
keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
}
理解类与配置文件的对应关系,可以方便读懂mybatis中的解析过程
- MappedStatement:对应select,update,insert,delete元素,把每一次sql语句的执行封装成MappedStatement。
- SqlSource:一个sql元素中内容,包含了sql纯文本,需要的参数列表,参数的属性。
- ParameterMap:一个sql元素内容中的所有参数的对象,包含一个ParameterMapping的队列。
- ParameterMapping :一个sql元素内容中单个参数的对象。
- BoundSql:根据传入的执行参数,由sqlSource获取,在运行时,可以确定最终执行的语句。DefaultParameterHandler 的setParameters过程就是通过BoundSql 获取到的运行时参数,来替换parparedStatement中的占位符。完成参数的注入。
先理清sql映射文件的元素与框架中对应封装的类的关系,就抓住Mybatis的主要脉络,具体框架的映射处理过程就是代码执行的过程,这些细节需要再后续学习,研究。