IDEA导入Spring boot 项目使用Maven 搭建多模块项目启动时Mybatis扫描不到Mapper.xml文件解决方式
前几天导入的公司以前写的项目是一个Spring Boot 使用Maven搭建的多模块项目导入IDEA在编译完成发现没有扫描对应的xml
1 先查看tomcat编译完成的 target 文件是否编译出对应的xml 文件
公司的这个项目使用的逆向工程生成的xml是放在代码中的不是正常的开发中放到resources 中,idea 中的maven 编译是默认不去扫描 代码中的xml 的 要让其扫描要在总pom.xml 文件中配置
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
重新让maven编译
再次查看 target 中的代码 发现已经生成了对应的 xml
2.查看是否mybatis 配置正确
然后启动项目 ,发现还是出现bingException 说明没有还是没有对应的xml 文件,纳闷都生成了对应的xml 了还是这样 ,然后我就是打开对应的 log 日志查看启动日志
再yml 中设置debug
logging:
level:
#xxx表示你的项目名称
com.xxx: DEBUG
org.apache.ibatis: DEBUG
org.mybatis.Spring: DEBUG
然后查看对应的加载信息发现没有扫描到对应的xml
1.查看是不是@MapperScan的问题
2.mapper-locations的问题
之前项目写的
mybatis:
mapper-locations: classpath:com/szqbl/lib/*/dao/${szqbl.db-type}/*.xml,com/szqbl/lib/*/*/dao/${szqbl.db-type}/*.xml,classpath:com/szqbl/*/dao/${szqbl.db-type}/*.xml,classpath:com/szqbl/*/*/dao/${szqbl.db-type}/*.xml,classpath:com/szqbl/*/*/*/dao/${szqbl.db-type}/*.xml
然后debug 后发现是
mapper-locations 的问题,这个项目是一个maven多模块项目导入了多个模块以jar的方式
classpath:只会到你的class路径中查找找文件。
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找。
然后修改成 :
mybatis:
mapper-locations: classpath*:com/szqbl/lib/*/dao/${szqbl.db-type}/*.xml,com/szqbl/lib/*/*/dao/${szqbl.db-type}/*.xml,classpath:com/szqbl/*/dao/${szqbl.db-type}/*.xml,classpath:com/szqbl/*/*/dao/${szqbl.db-type}/*.xml,classpath:com/szqbl/*/*/*/dao/${szqbl.db-type}/*.xml
最后启动项目发现扫描加载每个项目中的xml 了 成功跑起来了