MybatisMapperRegistry.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright 2009-2015 the original author or authors.
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.baomidou.mybatisplus;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.apache.ibatis.binding.BindingException;
  22. import org.apache.ibatis.binding.MapperProxyFactory;
  23. import org.apache.ibatis.binding.MapperRegistry;
  24. import org.apache.ibatis.session.Configuration;
  25. import org.apache.ibatis.session.SqlSession;
  26. import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
  27. /**
  28. * <p>
  29. * 继承至MapperRegistry
  30. * </p>
  31. *
  32. * @author Caratacus hubin
  33. * @since 2017-04-19
  34. */
  35. public class MybatisMapperRegistry extends MapperRegistry {
  36. private final Map<Class<?>, MapperProxyFactory<?>> knownMappers = new HashMap<>();
  37. private final Configuration config;
  38. public MybatisMapperRegistry(Configuration config) {
  39. super(config);
  40. this.config = config;
  41. // TODO注入SqlRunner
  42. GlobalConfigUtils.getSqlInjector(config).injectSqlRunner(config);
  43. }
  44. @SuppressWarnings("unchecked")
  45. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  46. final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
  47. if (mapperProxyFactory == null) {
  48. throw new BindingException("Type " + type + " is not known to the MybatisPlusMapperRegistry.");
  49. }
  50. try {
  51. return mapperProxyFactory.newInstance(sqlSession);
  52. } catch (Exception e) {
  53. throw new BindingException("Error getting mapper instance. Cause: " + e, e);
  54. }
  55. }
  56. public <T> boolean hasMapper(Class<T> type) {
  57. return knownMappers.containsKey(type);
  58. }
  59. @Override
  60. public <T> void addMapper(Class<T> type) {
  61. if (type.isInterface()) {
  62. if (hasMapper(type)) {
  63. // TODO 如果之前注入 直接返回
  64. return;
  65. // throw new BindingException("Type " + type +
  66. // " is already known to the MybatisPlusMapperRegistry.");
  67. }
  68. boolean loadCompleted = false;
  69. try {
  70. knownMappers.put(type, new MapperProxyFactory<>(type));
  71. // It's important that the type is added before the parser is
  72. // run
  73. // otherwise the binding may automatically be attempted by the
  74. // mapper parser. If the type is already known, it won't try.
  75. // TODO 自定义无 XML 注入
  76. MybatisMapperAnnotationBuilder parser = new MybatisMapperAnnotationBuilder(config, type);
  77. parser.parse();
  78. loadCompleted = true;
  79. } finally {
  80. if (!loadCompleted) {
  81. knownMappers.remove(type);
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * @since 3.2.2
  88. */
  89. public Collection<Class<?>> getMappers() {
  90. return Collections.unmodifiableCollection(knownMappers.keySet());
  91. }
  92. }