MyBatis SpringBoot 杂记

最近接了个xxx代码.

不能说人家不好, 因为必进年月久了.能用这么长时间, 不就说明还不错么?! 我们现在每天写的, 能超出人家的么~~~ 呵呵

Java项目中, 把动态数据源切换的框架整合进来. 嗯...这个有好有不好吧.

按Service层使用AOP来切换主从. 但是企业应用非常复杂. 如果再加一个别的数据库连接进来, 框架就显得不够用.

而框架的修改权又掌握在某一个部门手中, 所以,只能类似补丁地融入. 所以有了这一上午走读Spring/MyBatis的机会. 也挺好.

SpringBoot的一个annotation @SpringBootApplication, 使用其 exclude, 把框架中的某一些 @Configuration 排除掉.

然后自己写子类继承这些原有的@Configuration类, 并且加上@Configuration.
然后就可以初始化了.
原有容器初始化的时候, 以@Bean的方式生产了SqlSessionFactory, 默认执行Mapper的时候就会使用.
所以,如果用新加进来的DataSource也生成@Bean SqlSessionFactory, 那么Mapper就会迷茫,因为默认是按类型查找SqlSessionFactory.
解决这个问题, 改框架动静太大. 简单一点, 把原来的@Bean SqlSessionFactory加上@Primary,为默认.

然后新数据库连接使用的时候,可以使用
@Autowired @Quirified("xxx") 来注入SqlSessionFactory, 然后在@PostConstruct里, 把SqlSessionTemplate获取到,
然后,可以使用 sqlSessionTemplate.getMapper(Xxx.class) 获取到 Mapper.class接口的代理实例.
在这里踩了一个坑, 就是 getMapper总是说没有注册. 于是追踪了一下Mapper的注册过程.
有几篇blog写得挺好. 但是太冗长
https://blog.csdn.net/u011043551/article/details/80607050
https://blog.csdn.net/tiantiandjava/article/details/80569451

关键点: SqlSessionFactoryBean 是建造者模式, 设置了一堆属性之后, 然后调用 getObject()的时候,
执行 
afterPropertiesSet();-->buildSqlSessionFactory(); --> 一大段.
这大段代码的基本逻辑有3点:
1.注册Class, 基本上是Example, DTO 等等.
2.注册plugins, 比如翻页插件就在这里注册
3.注册handler, 这一段没特别了解, 后面用到再看看
4.注册xml, xmlConfigBuilder.parse()
  这一段包含了这次的内容. 一大段.
    private void parseConfiguration(XNode root) {
    try {
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      loadCustomVfs(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      typeHandlerElement(root.evalNode("typeHandlers"));
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }
  基本是解析xml各个节点.
  其中mapperElement 这个方法里,
   把xml节点中mapper 进行解析.调用mapperParser.parse();(XMLMapperBuilder)  ---> bindMapperForNamespace();
    private void bindMapperForNamespace() {
        String namespace = builderAssistant.getCurrentNamespace();
        if (namespace != null) {
          Class<?> boundType = null;
          try {
            boundType = Resources.classForName(namespace);
          } catch (ClassNotFoundException e) {
            //ignore, bound type is not required
          }
          if (boundType != null) {
            if (!configuration.hasMapper(boundType)) {
              // Spring may not know the real resource name so we set a flag
              // to prevent loading again this resource from the mapper interface
              // look at MapperAnnotationBuilder#loadXmlResource
              configuration.addLoadedResource("namespace:" + namespace);
              configuration.addMapper(boundType);
            }
          }
        }
    }
  会把xml中namespace中写着的xxx.xxx.Mapper认为是类, 来加载注册!!! 藏得真是深.
  
  

猜你喜欢

转载自www.cnblogs.com/tekikesyo/p/10346555.html