解决SpringBoot项目运行后出现Property 'mapperLocations' was not specified无法识别xml文件的问题

LonelyMan 1260 2021-07-26

起因

IDEA启动SpringBoot工程时显示Property 'mapperLocations' was not specified.,用AutoGenerator生成的mapper对应的xml文件都没扫描到

解决方案

若将生成得到的xml文件夹和里面的文件放在默认的src/main/java内的mapper文件夹内,编译运行后观察target目录会发现xml与里面的xml文件都不见了,因为默认编译后只会保留src/main/java内的.java文件,其他的文件都会被忽略,其他需要使用到的非.java文件需要放置在src/main/resources目录下

MybatisPlusProperties.java

private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"};

上面的代码说明MybatisPlus默认扫描xml文件的位置在/mapper

所以只需要将所有的xml文件放置在src/main/resources/mapper目录下就可以正常识别

另一种方式是在工程的pom文件中添加以下内容

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>/**/xml/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

这样可以指定src/main/java目录内的某些内容为resources,放置被忽略

之后再在application.yml文件中指定mapper-locations就可以正常识别

mapper-locations: classpath:/**/xml/*.xml

参考

https://blog.csdn.net/hngyymq/article/details/107201247