MybatisMapperRefreshTest.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.extension.test;
  17. import org.apache.ibatis.session.SqlSession;
  18. import org.apache.ibatis.session.SqlSessionFactory;
  19. import org.springframework.core.io.ClassPathResource;
  20. import org.springframework.core.io.Resource;
  21. import com.baomidou.mybatisplus.core.pagination.Pagination;
  22. import com.baomidou.mybatisplus.core.toolkit.SystemClock;
  23. import com.baomidou.mybatisplus.extension.spring.MybatisMapperRefresh;
  24. import com.baomidou.mybatisplus.extension.test.mysql.mapper.UserMapper;
  25. import com.baomidou.mybatisplus.extension.test.mysql.mapper.UserMapper;
  26. /**
  27. * <p>
  28. * 切莫用于生产环境(后果自负)<br>
  29. * Mybatis 映射文件热加载(发生变动后自动重新加载).<br>
  30. * 方便开发时使用,不用每次修改xml文件后都要去重启应用.<br>
  31. * </p>
  32. *
  33. * @author nieqiurong
  34. * @Date 2016-08-25
  35. */
  36. public class MybatisMapperRefreshTest extends CrudTest {
  37. /**
  38. * 测试 Mybatis XML 修改自动刷新
  39. */
  40. public static void main(String[] args) throws Exception {
  41. Resource[] resource = new ClassPathResource[]{new ClassPathResource("mysql/UserMapper.xml")};
  42. SqlSessionFactory sessionFactory = new CrudTest().sqlSessionFactory();
  43. new MybatisMapperRefresh(resource, sessionFactory, 0, 5, true);
  44. boolean isReturn = false;
  45. SqlSession session = null;
  46. while (!isReturn) {
  47. try {
  48. session = sessionFactory.openSession();
  49. UserMapper userMapper = session.getMapper(UserMapper.class);
  50. userMapper.selectListRow(new Pagination(1, 10));
  51. resource[0].getFile().setLastModified(SystemClock.now());
  52. session.commit();
  53. session.close();
  54. Thread.sleep(5000);
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. } finally {
  58. if (session != null) {
  59. session.close();
  60. }
  61. Thread.sleep(5000);
  62. }
  63. }
  64. System.exit(0);
  65. }
  66. }