MybatisConfiguration.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright (c) 2011-2020, hubin (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus;
  17. import org.apache.ibatis.binding.MapperRegistry;
  18. import org.apache.ibatis.logging.Log;
  19. import org.apache.ibatis.logging.LogFactory;
  20. import org.apache.ibatis.mapping.MappedStatement;
  21. import org.apache.ibatis.session.Configuration;
  22. import org.apache.ibatis.session.SqlSession;
  23. import com.baomidou.mybatisplus.entity.GlobalConfiguration;
  24. /**
  25. * <p>
  26. * replace default Configuration class
  27. * </p>
  28. * <p>
  29. * Caratacus 2016/9/25 replace mapperRegistry
  30. * </p>
  31. *
  32. * @author hubin
  33. * @Date 2016-01-23
  34. */
  35. public class MybatisConfiguration extends Configuration {
  36. private static final Log logger = LogFactory.getLog(MybatisConfiguration.class);
  37. /*
  38. * Mapper 注册
  39. */
  40. public final MybatisMapperRegistry mybatisMapperRegistry = new MybatisMapperRegistry(this);
  41. /**
  42. * 初始化调用
  43. */
  44. public MybatisConfiguration() {
  45. logger.debug("mybatis-plus init success.");
  46. }
  47. /**
  48. * <p>
  49. * MybatisPlus 加载 SQL 顺序:
  50. * </p>
  51. * 1、加载XML中的SQL<br>
  52. * 2、加载sqlProvider中的SQL<br>
  53. * 3、xmlSql 与 sqlProvider不能包含相同的SQL<br>
  54. * <br>
  55. * 调整后的SQL优先级:xmlSql > sqlProvider > curdSql <br>
  56. */
  57. @Override
  58. public void addMappedStatement(MappedStatement ms) {
  59. logger.debug("addMappedStatement: " + ms.getId());
  60. if (GlobalConfiguration.isRefresh(ms.getConfiguration())) {
  61. /*
  62. * 支持是否自动刷新 XML 变更内容,开发环境使用【 注:生产环境勿用!】
  63. */
  64. this.mappedStatements.remove(ms.getId());
  65. } else {
  66. if (this.mappedStatements.containsKey(ms.getId())) {
  67. /*
  68. * 说明已加载了xml中的节点; 忽略mapper中的SqlProvider数据
  69. */
  70. logger.error("mapper[" + ms.getId() + "] is ignored, because it's exists, maybe from xml file");
  71. return;
  72. }
  73. }
  74. super.addMappedStatement(ms);
  75. }
  76. @Override
  77. public void setDefaultScriptingLanguage(Class<?> driver) {
  78. if (driver == null) {
  79. /* 设置自定义 driver */
  80. driver = MybatisXMLLanguageDriver.class;
  81. }
  82. super.setDefaultScriptingLanguage(driver);
  83. }
  84. @Override
  85. public MapperRegistry getMapperRegistry() {
  86. return mybatisMapperRegistry;
  87. }
  88. @Override
  89. public <T> void addMapper(Class<T> type) {
  90. mybatisMapperRegistry.addMapper(type);
  91. }
  92. @Override
  93. public void addMappers(String packageName, Class<?> superType) {
  94. mybatisMapperRegistry.addMappers(packageName, superType);
  95. }
  96. @Override
  97. public void addMappers(String packageName) {
  98. mybatisMapperRegistry.addMappers(packageName);
  99. }
  100. @Override
  101. public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
  102. return mybatisMapperRegistry.getMapper(type, sqlSession);
  103. }
  104. @Override
  105. public boolean hasMapper(Class<?> type) {
  106. return mybatisMapperRegistry.hasMapper(type);
  107. }
  108. }