spring boot框架mybatis.mapper-locations配置問題詳解

一、作用

用於將配置路徑下的*.xml文件加載到mybatis中

二、如何配置

springboot或者spring項目經常會引用其它項目,把其它項目的Jar包加進來,因爲每個項目的包路徑不一樣,mapper.xml的路徑也不一樣,這個時候就需要引入多個路徑。

1. *.xml文件路徑在resources包下時,可根據路徑配置如下

方法一:只有一個路徑

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

方法二:有多個路徑

mybatis.mapper-locations= classpath:mapper/*.xml,classpath:mapper/user*.xml

方法三:通配符 ** 表示任意級的目錄

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

2. *.xml文件路徑在java包下時,不可使用mybatis.mapper-locations配置,可根據路徑配置如下

在pom.xml的<build>標籤中添加如下

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章