mybatis plus3.1.0 热加载mapper

今天又开始写业务代码了,每次修改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热加载插件/

 

posted @ 2019-08-12 20:17  香巴拉  阅读(1874)  评论(0编辑  收藏  举报