MybatisSessionFactoryBuilder.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2011-2014, 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 java.io.InputStream;
  18. import java.io.Reader;
  19. import java.util.Properties;
  20. import org.apache.ibatis.exceptions.ExceptionFactory;
  21. import org.apache.ibatis.executor.ErrorContext;
  22. import org.apache.ibatis.session.SqlSessionFactory;
  23. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  24. import com.baomidou.mybatisplus.entity.GlobalConfiguration;
  25. import com.baomidou.mybatisplus.toolkit.GlobalConfigUtils;
  26. import com.baomidou.mybatisplus.toolkit.IOUtils;
  27. /**
  28. * <p>
  29. * replace default SqlSessionFactoryBuilder class
  30. * </p>
  31. *
  32. * @author hubin
  33. * @Date 2016-01-23
  34. */
  35. public class MybatisSessionFactoryBuilder extends SqlSessionFactoryBuilder {
  36. private GlobalConfiguration globalConfig = GlobalConfigUtils.defaults();
  37. @Override
  38. public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
  39. try {
  40. MybatisXMLConfigBuilder parser = new MybatisXMLConfigBuilder(reader, environment, properties);
  41. GlobalConfigUtils.setGlobalConfig(parser.getConfiguration(), this.globalConfig);
  42. return build(parser.parse());
  43. } catch (Exception e) {
  44. throw ExceptionFactory.wrapException("Error building SqlSession.", e);
  45. } finally {
  46. ErrorContext.instance().reset();
  47. IOUtils.closeQuietly(reader);
  48. }
  49. }
  50. @Override
  51. public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
  52. try {
  53. MybatisXMLConfigBuilder parser = new MybatisXMLConfigBuilder(inputStream, environment, properties);
  54. GlobalConfigUtils.setGlobalConfig(parser.getConfiguration(), this.globalConfig);
  55. return build(parser.parse());
  56. } catch (Exception e) {
  57. throw ExceptionFactory.wrapException("Error building SqlSession.", e);
  58. } finally {
  59. ErrorContext.instance().reset();
  60. IOUtils.closeQuietly(inputStream);
  61. }
  62. }
  63. // TODO 注入全局配置
  64. public void setGlobalConfig(GlobalConfiguration globalConfig) {
  65. this.globalConfig = globalConfig;
  66. }
  67. }