AI智能
改变未来

Mybaitis 源码分析

前提文件:对应项目为mybatis-01
其中配置xml文件为

<configuration><environments default=\"development\"><environment id=\"development\"><transactionManager type=\"JDBC\"/><dataSource type=\"POOLED\"><property name=\"driver\" value=\"com.mysql.cj.jdbc.Driver\"/><property name=\"url\" value=\"jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;serverTimezone=GMT%2B8&amp;useUnicode=true&amp;characterEncoding=utf8\"/><property name=\"username\" value=\"root\"/><property name=\"password\" value=\"123456\"/></dataSource></environment></environments><mappers><mapper resource=\"com/wang/Dao/UserMapper.xml\"/></mappers></configuration>

Dao接口层Mapper.xml为:

<mapper namespace=\"com.wang.Dao.UserMapper\"><select id=\"getUserLike\">select * from mybatis.user where like #{value}</select><select id=\"getUserList\" resultType=\"com.wang.pojo.User\">select * from mybatis.user;</select><select id=\"getUserById\" resultType=\"com.wang.pojo.User\" parameterType=\"int\">select * from mybatis.user where id = #{id};</select><select id=\"getUserById2\" resultType=\"com.wang.pojo.User\" parameterType=\"map\">select * from mybatis.user where id = #{id} and name=#{name};</select><!--    对象中的属性可以截止取出来--><insert id=\"addUser\" parameterType=\"com.wang.pojo.User\" >insert into mybatis.user (id, name ,pwd) values (#{id}, #{name}, #{pwd});</insert><insert id=\"addUser2\" parameterType=\"map\" >insert into mybatis.user (id, name ,pwd) values (#{userid}, #{username}, #{userpwd});</insert><update id=\"updateUser\" parameterType=\"com.wang.pojo.User\">update mybatis.user set name=#{name },pwd=#{pwd} where id=#{id};</update><delete id=\"deleteUser\" parameterType=\"int\">delete from mybatis.user where id=@{id}</delete></mapper>

技术拆解
首先对mybatis进行技术拆解,可分为如下部分:

一、对数据源部分进行分析:该部分整体的解析步骤为:

mybatis是如一步一步何处理数据源的:org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.Reader)>org.apache.ibatis.builder.xml.XMLConfigBuilder>org.apache.ibatis.builder.xml.XMLConfigBuilder.parse>org.apache.ibatis.builder.xml.XMLConfigBuilder.environmentsElement>org.apache.ibatis.builder.xml.XMLConfigBuilder.dataSourceElement>org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory.getDataSource>org.apache.ibatis.session.Configuration.setEnvironment   #####这里是set总结:解析xml文件,将文件内容给到Java对象Configuration,通过setEnvironment给到的

第一步:

可以得到如下内容:

<configuration><environments default=\"development\"><environment id=\"development\"><transactionManager type=\"JDBC\"/><dataSource type=\"POOLED\"><property name=\"driver\" value=\"com.mysql.cj.jdbc.Driver\"/><property name=\"url\" value=\"jdbc:mysql://localhost:3306/mybatis?useSSL=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8\"/><property name=\"username\" value=\"root\"/><property name=\"password\" value=\"123456\"/></dataSource></environment></environments><mappers><mapper resource=\"com/wang/Dao/UserMapper.xml\"/></mappers></configuration>

第二步得到该内容:

<environments default=\"development\"><environment id=\"development\"><transactionManager type=\"JDBC\"/><dataSource type=\"POOLED\"><property name=\"driver\" value=\"com.mysql.cj.jdbc.Driver\"/><property name=\"url\" value=\"jdbc:mysql://localhost:3306/mybatis?useSSL=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8\"/><property name=\"username\" value=\"root\"/><property name=\"password\" value=\"123456\"/></dataSource></environment></environments>

第三步:得到该内容

<dataSource type=\"POOLED\"><property name=\"driver\" value=\"com.mysql.cj.jdbc.Driver\"/><property name=\"url\" value=\"jdbc:mysql://localhost:3306/mybatis?useSSL=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8\"/><property name=\"username\" value=\"root\"/><property name=\"password\" value=\"123456\"/></dataSource>

第四步得到该内容:

<transactionManager type=\"JDBC\"/><dataSource type=\"POOLED\"><property name=\"driver\" value=\"com.mysql.cj.jdbc.Driver\"/><property name=\"url\" value=\"jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;serverTimezone=GMT%2B8&amp;useUnicode=true&amp;characterEncoding=utf8\"/><property name=\"username\" value=\"root\"/><property name=\"password\" value=\"123456\"/></dataSource>

第五步得到该内容:

这个就和我们的配置文件一一对应的!!!!

二,执行语句

是如何执行sql语句:org.apache.ibatis.session.SqlSessionFactoryBuilder.build(java.io.Reader)>org.apache.ibatis.builder.xml.XMLConfigBuilder>org.apache.ibatis.builder.xml.XMLConfigBuilder.parse>org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement>org.apache.ibatis.builder.xml.XMLMapperBuilder.XMLMapperBuilder(java.io.InputStream, org.apache.ibatis.session.Configuration, java.lang.String, java.util.Map<java.lang.String,org.apache.ibatis.parsing.XNode>)>org.apache.ibatis.builder.xml.XMLMapperBuilder.parse>org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement>org.apache.ibatis.builder.xml.XMLMapperBuilder.buildStatementFromContext(java.util.List<org.apache.ibatis.parsing.XNode>)>org.apache.ibatis.builder.xml.XMLStatementBuilder.XMLStatementBuilder(org.apache.ibatis.session.Configuration, org.apache.ibatis.builder.MapperBuilderAssistant, org.apache.ibatis.parsing.XNode, java.lang.String)>org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode>org.apache.ibatis.session.Configuration.addMappedStatement  ####这里是add

第一步得到该内容:

<mappers><mapper resource=\"com/wang/Dao/UserMapper.xml\"/></mappers>

补充知识(源码分析得到):
mybatis解析mapper文件有几种方式:4种,以及优先级


第二步得到该内容:

<mapper namespace=\"com.wang.Dao.UserMapper\"><select id=\"getUserLike\">select * from mybatis.user where like #{value}</select><select resultType=\"com.wang.pojo.User\" id=\"getUserList\">select * from mybatis.user;</select><select resultType=\"com.wang.pojo.User\" parameterType=\"int\" id=\"getUserById\">select * from mybatis.user where id = #{id};</select><select resultType=\"com.wang.pojo.User\" parameterType=\"map\" id=\"getUserById2\">select * from mybatis.user where id = #{id} and name=#{name};</select><insert parameterType=\"com.wang.pojo.User\" id=\"addUser\">insert into mybatis.user (id, name ,pwd) values (#{id}, #{name}, #{pwd});</insert><insert parameterType=\"map\" id=\"addUser2\">insert into mybatis.user (id, name ,pwd) values (#{userid}, #{username}, #{userpwd});</insert><update parameterType=\"com.wang.pojo.User\" id=\"updateUser\">update mybatis.user set name=#{name },pwd=#{pwd} where id=#{id};</update><delete parameterType=\"int\" id=\"deleteUser\">delete from mybatis.user where id=@{id}</delete></mapper>

第三步得到该内容:

对应我的Mapper.xml文件中的标签!!
mapper标签中的子标签都在这里!!!


org.apache.ibatis.mapping.MappedStatement中的内容对应mapper.xml中的select内容

补充知识二:

org.apache.ibatis.session.Configuration#newExecutor(org.apache.ibatis.transaction.Transaction, org.apache.ibatis.session.ExecutorType)mybiatis的核心是执行器,执行器有3种org.apache.ibatis.session.ExecutorType下有三种SIMPLE, REUSE, BATCHSIMPLE是默认的执行器org.apache.ibatis.executor.Executor执行器接口mybatis一级缓存是否默认开启呢,默认开启。if (cacheEnabled) {executor = new CachingExecutor(executor);}protected boolean cacheEnabled = true;

补充1.
补充2.

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Mybaitis 源码分析