今天又開始寫業務代碼了,每次修改SQL都要重啟服務,實在是浪費時間。
想起之前研究過的《mybatis plus3.1.0 熱加載mapper》,一直沒有成功,今天靜下心來分析了問題,終於找到原因了。
上文中提到的MybatisPlusConfig,斷點分析到獲取資源文件未0個,不對勁。
resources = resourceResolver.getResources(mapperLocations)
於是我發現我的本地配置可能不是很好。改了一下:
mybatis-plus:
mapper-locations: classpath:/mybatis/mapper/*-mapper.xml,/mybatis/mapper/*/*Mapping.xml
改為如下
mybatis-plus:
mapper-locations: classpath*:mybatis/mapper/*/*.xml
后面加載到資源文件了,可是又遇到了另外的坑,文章中的MybatisMapperRefresh類,明明定義了集合類型,可還是有其他類型的對象在,然后就報類型轉換的錯誤。我想到了用迭代來做轉換,終於成功了。
Collection<MappedStatement> mappedStatements = configuration.getMappedStatements();
這里居然存在org.apache.ibatis.session.Configuration$StrictMap$Ambiguity,然后轉換到MappedStatement對象就報類型轉換的錯誤了。用下框紅字的辦法解決了。
private void cleanKeyGenerators(List<XNode> list, String namespace) { for (XNode context : list) { String id = context.getStringAttribute("id"); configuration.getKeyGeneratorNames().remove(id + SelectKeyGenerator.SELECT_KEY_SUFFIX); configuration.getKeyGeneratorNames().remove(namespace + "." + id + SelectKeyGenerator.SELECT_KEY_SUFFIX); Collection<MappedStatement> mappedStatements = configuration.getMappedStatements(); List<MappedStatement> objects = Lists.newArrayList(); Iterator<MappedStatement> it = mappedStatements.iterator(); while (it.hasNext()){ Object object=it.next(); if(object instanceof org.apache.ibatis.mapping.MappedStatement) { MappedStatement mappedStatement=(MappedStatement)object; if (mappedStatement.getId().equals(namespace + "." + id)) { objects.add(mappedStatement); } } } // for (MappedStatement mappedStatement : mappedStatements) { // if (mappedStatement.getId().equals(namespace + "." + id)) { // objects.add(mappedStatement); // } // } mappedStatements.removeAll(objects); } }
然后這樣起作用了。
之前還研究了一篇文章,說是用idea插件模式進行,不過啟動了之后,報錯了,於是也沒有深入。還是用代碼方式好一點。
不過倒是get了很多新姿勢。
https://githuboy.online/2019/05/11/基於JRebel開發的MybatisPlus熱加載插件/
