资讯专栏INFORMATION COLUMN

[闹着玩-1] mybatis源码查看

Towers / 1922人阅读

摘要:配置测试描述测试类技术部查询表至此初步配置完成执行过程获取解析成接收方法方法方法属性定位方法数据绑定到具体方法提交传输获取配置解析标签对象获取过程源码使用

MyBatis 配置

</>复制代码

  1. 4.0.0
  2. com.huifer
  3. mybatisBook
  4. 1.0-SNAPSHOT
  5. UTF-8
  6. UTF-8
  7. 1.8
  8. org.mybatis
  9. mybatis
  10. 3.5.0
  11. mysql
  12. mysql-connector-java
  13. 8.0.13
  14. org.projectlombok
  15. lombok
  16. 1.18.4
  17. true
  18. org.apache.maven.plugins
  19. maven-compiler-plugin
  20. 1.8
  21. 1.8

mybatis-config.xml

</>复制代码

测试

DeptMapper.xml

</>复制代码

  1. INSERT INTO dept (dname, loc)
  2. VALUES (#{dname} , #{loc});

Dept.java

</>复制代码

  1. package com.huifer.mybatis;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. /**
  6. * 描述:
  7. *
  8. * @author huifer
  9. * @date 2019-02-21
  10. */
  11. @Data
  12. @AllArgsConstructor
  13. @NoArgsConstructor
  14. public class Dept {
  15. private Long id;
  16. private String dname;
  17. private String loc;
  18. }

测试类

</>复制代码

  1. public class Demo {
  2. public static void main(String[] args) throws Exception {
  3. Dept dept = new Dept();
  4. dept.setDname("技术部");
  5. dept.setLoc("oc");
  6. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
  7. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
  8. SqlSession session = factory.openSession();
  9. session.insert("insertDept", dept);
  10. session.commit();
  11. session.close();
  12. }
  13. }

查询表

至此初步配置完成

执行过程

</>复制代码

  1. graph TD
  2. start[获取mybatis.config.xml] --> conditionA[SqlSessionFactory]
  3. conditionA --> |xml 解析成 org.apache.ibatis.session.Configuration | conditionB[接收SqlSessionFactory]
  4. conditionB --> |org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource| conditionC[ SqlSession]
  5. conditionC --> |org.apache.ibatis.session.defaults.DefaultSqlSession insert方法 update方法| conditionD[insert方法]
  6. conditionD --> | org.apache.ibatis.mapping.MappedStatement sqlSource属性 | conditionE[mapper 定位]
  7. conditionE --> | org.apache.ibatis.executor.SimpleExecutor doUpdate方法 stmt|conditionF[数据绑定到具体sql]
  8. conditionF --> |org.apache.ibatis.executor.statement.PreparedStatementHandler update方法 提交| conditionG[传输sql]
  9. conditionG -->|commit| stop

session

获取mybatils-config.xml 配置 解析xml标签

org.apache.ibatis.session.defaults.DefaultSqlSessionFactory

</>复制代码

  1. private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
  2. Transaction tx = null;
  3. DefaultSqlSession var8;
  4. try {
  5. Environment environment = this.configuration.getEnvironment();
  6. TransactionFactory transactionFactory = this.getTransactionFactoryFromEnvironment(environment);
  7. tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
  8. Executor executor = this.configuration.newExecutor(tx, execType);
  9. var8 = new DefaultSqlSession(this.configuration, executor, autoCommit);
  10. } catch (Exception var12) {
  11. this.closeTransaction(tx);
  12. throw ExceptionFactory.wrapException("Error opening session. Cause: " + var12, var12);
  13. } finally {
  14. ErrorContext.instance().reset();
  15. }
  16. return var8;
  17. }

insert

org.apache.ibatis.session.defaults.DefaultSqlSession

</>复制代码

  1. public int insert(String statement, Object parameter) {
  2. return this.update(statement, parameter);
  3. }
  4. public int update(String statement, Object parameter) {
  5. int var4;
  6. try {
  7. this.dirty = true;
  8. MappedStatement ms = this.configuration.getMappedStatement(statement);
  9. var4 = this.executor.update(ms, this.wrapCollection(parameter));
  10. } catch (Exception var8) {
  11. throw ExceptionFactory.wrapException("Error updating database. Cause: " + var8, var8);
  12. } finally {
  13. ErrorContext.instance().reset();
  14. }
  15. return var4;
  16. }

ms 对象

doUpdate

org.apache.ibatis.executor.SimpleExecutor

</>复制代码

  1. public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  2. Statement stmt = null;
  3. int var6;
  4. try {
  5. Configuration configuration = ms.getConfiguration();
  6. StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, (ResultHandler)null, (BoundSql)null);
  7. stmt = this.prepareStatement(handler, ms.getStatementLog());
  8. var6 = handler.update(stmt);
  9. } finally {
  10. this.closeStatement(stmt);
  11. }
  12. return var6;
  13. }

update

</>复制代码

  1. org.apache.ibatis.executor.statement.PreparedStatementHandler

</>复制代码

  1. public int update(Statement statement) throws SQLException {
  2. PreparedStatement ps = (PreparedStatement)statement;
  3. ps.execute();
  4. int rows = ps.getUpdateCount();
  5. Object parameterObject = this.boundSql.getParameterObject();
  6. KeyGenerator keyGenerator = this.mappedStatement.getKeyGenerator();
  7. keyGenerator.processAfter(this.executor, this.mappedStatement, ps, parameterObject);
  8. return rows;
  9. }
Mapper

</>复制代码

获取过程源码

org.apache.ibatis.builder.xml.XMLConfigBuilder 使用parseConfiguration 方法将 mybatis-config.xml 中的mappers 标签内容获取到 mapperElement具体执行获取内容 ,作用将mappers添加到configuration 中

</>复制代码

  1. private void parseConfiguration(XNode root) {
  2. try {
  3. this.propertiesElement(root.evalNode("properties"));
  4. Properties settings = this.settingsAsProperties(root.evalNode("settings"));
  5. this.loadCustomVfs(settings);
  6. this.loadCustomLogImpl(settings);
  7. this.typeAliasesElement(root.evalNode("typeAliases"));
  8. this.pluginElement(root.evalNode("plugins"));
  9. this.objectFactoryElement(root.evalNode("objectFactory"));
  10. this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
  11. this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
  12. this.settingsElement(settings);
  13. this.environmentsElement(root.evalNode("environments"));
  14. this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
  15. this.typeHandlerElement(root.evalNode("typeHandlers"));
  16. this.mapperElement(root.evalNode("mappers"));
  17. } catch (Exception var3) {
  18. throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
  19. }
  20. }
  21. private void mapperElement(XNode parent) throws Exception {
  22. if (parent != null) {
  23. Iterator var2 = parent.getChildren().iterator();
  24. while(true) {
  25. while(var2.hasNext()) {
  26. XNode child = (XNode)var2.next();
  27. String resource;
  28. if ("package".equals(child.getName())) {
  29. resource = child.getStringAttribute("name");
  30. this.configuration.addMappers(resource);
  31. } else {
  32. resource = child.getStringAttribute("resource");
  33. String url = child.getStringAttribute("url");
  34. String mapperClass = child.getStringAttribute("class");
  35. XMLMapperBuilder mapperParser;
  36. InputStream inputStream;
  37. if (resource != null && url == null && mapperClass == null) {
  38. ErrorContext.instance().resource(resource);
  39. inputStream = Resources.getResourceAsStream(resource);
  40. mapperParser = new XMLMapperBuilder(inputStream, this.configuration, resource, this.configuration.getSqlFragments());
  41. mapperParser.parse();
  42. } else if (resource == null && url != null && mapperClass == null) {
  43. ErrorContext.instance().resource(url);
  44. inputStream = Resources.getUrlAsStream(url);
  45. mapperParser = new XMLMapperBuilder(inputStream, this.configuration, url, this.configuration.getSqlFragments());
  46. mapperParser.parse();
  47. } else {
  48. if (resource != null || url != null || mapperClass == null) {
  49. throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one.");
  50. }
  51. Class mapperInterface = Resources.classForName(mapperClass);
  52. this.configuration.addMapper(mapperInterface);
  53. }
  54. }
  55. }
  56. return;
  57. }
  58. }
  59. }

当前Configuration 下的mappedStatements 属性

org.apache.ibatis.binding.MapperRegistry addMapper 方法,作用将解析到的接口对象放到configuration 中 ,一个接口只能注册一次

</>复制代码

  1. public void addMapper(Class type) {
  2. if (type.isInterface()) {
  3. if (this.hasMapper(type)) {
  4. throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
  5. }
  6. boolean loadCompleted = false;
  7. try {
  8. this.knownMappers.put(type, new MapperProxyFactory(type));
  9. MapperAnnotationBuilder parser = new MapperAnnotationBuilder(this.config, type);
  10. parser.parse();
  11. loadCompleted = true;
  12. } finally {
  13. if (!loadCompleted) {
  14. this.knownMappers.remove(type);
  15. }
  16. }
  17. }
  18. }

org.apache.ibatis.builder.annotation.MapperAnnotationBuilder parse方法

loadXmlResource 来确认加载具体的xml文件

</>复制代码

  1. public void parse() {
  2. String resource = this.type.toString();
  3. if (!this.configuration.isResourceLoaded(resource)) {
  4. this.loadXmlResource();
  5. this.configuration.addLoadedResource(resource);
  6. this.assistant.setCurrentNamespace(this.type.getName());
  7. this.parseCache();
  8. this.parseCacheRef();
  9. Method[] methods = this.type.getMethods();
  10. Method[] var3 = methods;
  11. int var4 = methods.length;
  12. for(int var5 = 0; var5 < var4; ++var5) {
  13. Method method = var3[var5];
  14. try {
  15. if (!method.isBridge()) {
  16. this.parseStatement(method);
  17. }
  18. } catch (IncompleteElementException var8) {
  19. this.configuration.addIncompleteMethod(new MethodResolver(this, method));
  20. }
  21. }
  22. }
  23. this.parsePendingMethods();
  24. }
  25. private void loadXmlResource() {
  26. if (!this.configuration.isResourceLoaded("namespace:" + this.type.getName())) {
  27. String xmlResource = this.type.getName().replace(".", "/") + ".xml";
  28. InputStream inputStream = this.type.getResourceAsStream("/" + xmlResource);
  29. if (inputStream == null) {
  30. try {
  31. inputStream = Resources.getResourceAsStream(this.type.getClassLoader(), xmlResource);
  32. } catch (IOException var4) {
  33. }
  34. }
  35. if (inputStream != null) {
  36. XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, this.assistant.getConfiguration(), xmlResource, this.configuration.getSqlFragments(), this.type.getName());
  37. xmlParser.parse();
  38. }
  39. }
  40. }

根据 loadXmlResource 中下面这行得知 , PojoMapper.xml 要和 PojoMapperInterface 放在一个路径下

</>复制代码

  1. String xmlResource = this.type.getName().replace(".", "/") + ".xml";

具体sql

org.apache.ibatis.builder.xml.XMLStatementBuilder parseStatementNode 方法解析sql语句

</>复制代码

</>复制代码

  1. public void parseStatementNode() {
  2. String id = this.context.getStringAttribute("id");
  3. String databaseId = this.context.getStringAttribute("databaseId");
  4. if (this.databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) {
  5. Integer fetchSize = this.context.getIntAttribute("fetchSize");
  6. Integer timeout = this.context.getIntAttribute("timeout");
  7. String parameterMap = this.context.getStringAttribute("parameterMap");
  8. String parameterType = this.context.getStringAttribute("parameterType");
  9. Class parameterTypeClass = this.resolveClass(parameterType);
  10. String resultMap = this.context.getStringAttribute("resultMap");
  11. String resultType = this.context.getStringAttribute("resultType");
  12. String lang = this.context.getStringAttribute("lang");
  13. LanguageDriver langDriver = this.getLanguageDriver(lang);
  14. Class resultTypeClass = this.resolveClass(resultType);
  15. String resultSetType = this.context.getStringAttribute("resultSetType");
  16. StatementType statementType = StatementType.valueOf(this.context.getStringAttribute("statementType", StatementType.PREPARED.toString()));
  17. ResultSetType resultSetTypeEnum = this.resolveResultSetType(resultSetType);
  18. String nodeName = this.context.getNode().getNodeName();
  19. SqlCommandType sqlCommandType = SqlCommandType.valueOf(nodeName.toUpperCase(Locale.ENGLISH));
  20. boolean isSelect = sqlCommandType == SqlCommandType.SELECT;
  21. boolean flushCache = this.context.getBooleanAttribute("flushCache", !isSelect);
  22. boolean useCache = this.context.getBooleanAttribute("useCache", isSelect);
  23. boolean resultOrdered = this.context.getBooleanAttribute("resultOrdered", false);
  24. XMLIncludeTransformer includeParser = new XMLIncludeTransformer(this.configuration, this.builderAssistant);
  25. includeParser.applyIncludes(this.context.getNode());
  26. this.processSelectKeyNodes(id, parameterTypeClass, langDriver);
  27. SqlSource sqlSource = langDriver.createSqlSource(this.configuration, this.context, parameterTypeClass);
  28. String resultSets = this.context.getStringAttribute("resultSets");
  29. String keyProperty = this.context.getStringAttribute("keyProperty");
  30. String keyColumn = this.context.getStringAttribute("keyColumn");
  31. String keyStatementId = id + "!selectKey";
  32. keyStatementId = this.builderAssistant.applyCurrentNamespace(keyStatementId, true);
  33. Object keyGenerator;
  34. if (this.configuration.hasKeyGenerator(keyStatementId)) {
  35. keyGenerator = this.configuration.getKeyGenerator(keyStatementId);
  36. } else {
  37. keyGenerator = this.context.getBooleanAttribute("useGeneratedKeys", this.configuration.isUseGeneratedKeys() && SqlCommandType.INSERT.equals(sqlCommandType)) ? Jdbc3KeyGenerator.INSTANCE : NoKeyGenerator.INSTANCE;
  38. }
  39. this.builderAssistant.addMappedStatement(id, sqlSource, statementType, sqlCommandType, fetchSize, timeout, parameterMap, parameterTypeClass, resultMap, resultTypeClass, resultSetTypeEnum, flushCache, useCache, resultOrdered, (KeyGenerator)keyGenerator, keyProperty, keyColumn, databaseId, langDriver, resultSets);
  40. }
  41. }

还原sql方法

</>复制代码

  1. SqlSource sqlSource = langDriver.createSqlSource(this.configuration, this.context, parameterTypeClass);

org.apache.ibatis.scripting.defaults.RawSqlSource RawSqlSource方法

</>复制代码

  1. public RawSqlSource(Configuration configuration, String sql, Class parameterType) {
  2. SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
  3. Class clazz = parameterType == null ? Object.class : parameterType;
  4. this.sqlSource = sqlSourceParser.parse(sql, clazz, new HashMap());
  5. }

org.apache.ibatis.builder.SqlSourceBuilder parse方法还原成sql语句

</>复制代码

  1. public SqlSource parse(String originalSql, Class parameterType, Map additionalParameters) {
  2. SqlSourceBuilder.ParameterMappingTokenHandler handler = new SqlSourceBuilder.ParameterMappingTokenHandler(this.configuration, parameterType, additionalParameters);
  3. GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
  4. String sql = parser.parse(originalSql);
  5. return new StaticSqlSource(this.configuration, sql, handler.getParameterMappings());
  6. }

最后看一下 sqlSource

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73404.html

相关文章

  • [着玩-2]spring-mvc 主要流程

    摘要:源码仓库本文仓库三层结构表现层模型业务层持久层工作流程用户前端控制器用户发送请求前端控制器后端控制器根据用户请求查询具体控制器后端控制器前端控制器处理后结果前端控制器视图视图渲染视图前端控制器返回视图前端控制器用户响应结 SpringMvc 【源码仓库】【本文仓库】 三层结构 表现层 MVC模型 业务层 service 持久层 dao 工作流程 用户->前端控制器:用户...

    fuchenxuan 评论0 收藏0
  • 一个写着玩的 bitcoin 客户端

    摘要:一个写着玩的客户端,代码不复杂,轻松了解比特币。项目地址起因看书确实是很好的学习比特币的方法,但是没有代码的帮助,理解比特币如何实现时,很是困难。后来发现一个用写的完整客户端,就决定用它来研究比特币源码了,帮助我理解比特币。 一个写着玩的 bitcoin 客户端,代码不复杂,轻松了解比特币。项目地址:https://github.com/jiangleo/b... 起因 看书确实是很好...

    DTeam 评论0 收藏0
  • 一个写着玩的 bitcoin 客户端

    摘要:一个写着玩的客户端,代码不复杂,轻松了解比特币。项目地址起因看书确实是很好的学习比特币的方法,但是没有代码的帮助,理解比特币如何实现时,很是困难。后来发现一个用写的完整客户端,就决定用它来研究比特币源码了,帮助我理解比特币。 一个写着玩的 bitcoin 客户端,代码不复杂,轻松了解比特币。项目地址:https://github.com/jiangleo/b... 起因 看书确实是很好...

    fuyi501 评论0 收藏0
  • 记一次 Mybatis 一级缓存清理无效引起的源码走读

    摘要:今天对象在学习时发现对象的方法并不能清理一级缓存同一下相同查询条件返回的结果还是旧值。测试代码如下上网搜索网上搜索找到了相同问题并没有人解答。例如查看官方文档实例有一个本地缓存在执行和时被清理。要明确地关闭它获取打算做更多的工作你可以调用。 今天对象在学习 Mybatis 时发现 org.apache.ibatis.session.SqlSession 对象的 clearCache()...

    voyagelab 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<