mybatis的mapper.xml 打包问题

今天打开项目,发现查询失效了,莫名奇妙,排查了好长时间,终于找到问题所在,mapper文件没有打包(那为啥我前几天能跑起来?),解决如下:
一般情况下,我们用到的资源文件(各种xml,properites,xsd文件等)都放在src/main/resources下面,利用maven打包时,maven能把这些资源文件打包到相应的jar或者war里。
有时候,比如mybatis的mapper.xml文件,我们习惯把它和Mapper.java放一起,都在src/main/java下面,这样利用maven打包时,就需要修改pom.xml文件,来把mapper.xml文件一起打包进jar或者war里了,否则,这些文件不会被打包的。(maven认为src/main/java只是java的源代码路径)。
在网上找了一些方法,选择一种即可实现。
方法1,其中*/这样的写法,是为了保证各级子目录下的资源文件被打包。

<build>
    <finalName>testfinalName>
    
        resource>
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.propertiesinclude>
                <include>**/*.xmlinclude>
                <include>**/*.tldinclude>
            includes>
            <filtering>falsefiltering>
        resource>
    resources>
build>

方法2,利用build-helper-maven-plugin插件

<build>
    ...
    plugins>
        ...
        
        <plugin>
            <groupId>org.codehaus.mojogroupId>
            <artifactId>build-helper-maven-pluginartifactId>
            <version>1.8version>
            <executions>
                <execution>
                    <id>add-resourceid>
                    <phase>generate-resourcesphase>
                    <goals>
                        <goal>add-resourcegoal>
                    goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>src/main/javadirectory>
                                <includes>
                                    <include>**/*.xmlinclude>
                                includes>
                            resource>
                        resources>
                    configuration>
                execution>
            executions>
        plugin>   
        ...
    plugins>     
    ...
build>

方法3,利用maven-resources-plugin插件

<build>
    ...
    plugins>
        ...
        
        <plugin>
            <artifactId>maven-resources-pluginartifactId>
            <version>2.5version>
            <executions>
                <execution>
                    <id>copy-xmlsid>
                    <phase>process-sourcesphase>
                    <goals>
                        <goal>copy-resourcesgoal>
                    goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/classesoutputDirectory>
                        <resources>
                            <resource>
                                <directory>${basedir}/src/main/javadirectory>
                                <includes>
                                    <include>**/*.xmlinclude>
                                includes>
                            resource>
                        resources>
                    configuration>
                execution>
            executions>
        plugin>   
        ...
    plugins>     
    ...
build>

你可能感兴趣的:(java,mybatis)