MyBatis interface and corresponding mapper file location configuration detailedthe original link is: 71480954
Today encountered a problem is mybatis in the interface and corresponding mapper file location, and the operation will be different, in the internet for a long time finally found the method, here is a simple analysis:
We know that in a typical MAVEN project, the directory structure is: Src/main/java and Src/main/resources, the former is used to store Java source code, the latter is to store some resource files, such as configuration files.
The MyBatis interface and the corresponding mapper file are not necessarily placed under the same package, if
to put togetherThe purpose is to mybatis for automatic scanning, and note that at this time the Java interface name and mapper file name to the same, otherwise it will report an exception, because at this time MyBatis will automatically resolve the corresponding interface and the corresponding configuration file, so there is no need to configure the location of the mapper file.
1: The interface and files are placed in the same package as follows:
When MAVEN is packaged by default, only the source code is packaged for the Src/main/java directory, and no other files are packaged. So at this point, if the corresponding mapper file is placed in the Src/main/java directory, will not be packaged into the final Jar folder, and will not be exported to the target folder, because the unit test is performed at the time of the/target directory/ Test-classes code, so it will not be successful at the time of the test.
In order to implement packaging in the MAVEN default environment, the MyBatis interface and the mapper file are in the same package, and the same package can be created in the Src/main/resources directory by placing the interface file in a Src/main/java package. This is a convention better than the configuration, so that when MAVEN is packaged, the files under the same package as Src/main/java and Src/main/resources are merged into the same package.
In the default MAVEN packaging environment, do not put the interface files and mapper files all to Src/main/java, so that the mapper file will not be packaged in
In src/main/java
and src/main/resources
in the same package name, the same file name, the default packaging after the structure is as follows:
It was found that the package was under the same bag.
And when you put the interface and the mapper file src/main/java
in the same package, the following:
The following files are packaged:
There is no mapper file after the compilation is found, so there will be an error whether it is a test or a formal execution at this time!!
Change the MAVEN build configuration
If you do not want to put the interface and mapper files into the src/main/java
and src/main/resources
, but all to put src/main/java
, then you need to specify in the build Maven packaging needs to include XML files, the following configuration:
[Java]View PlainCopy
- <span style="FONT-SIZE:14PX;" ><build>
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
- </build></span>
This will also pack the mapper files into the/target folder when you package.
2: The interface and file are not in the same package
If the interface and the mapper file are not under the same package, the automatic scan cannot be resolved, and the interface and files need to be configured separately.
2.1 How XML is configured
2.1.1 Do not use spring
The configuration file using MyBatis is as follows:
[Java]View PlainCopy
- <span style="FONT-SIZE:14PX;" ><?xml version="1.0" encoding="UTF-8"?>
- <! DOCTYPE Configuration
- Public "-//mybatis.org//dtd Config 3.0//en"
- "HTTP://MYBATIS.ORG/DTD/MYBATIS-3-CONFIG.DTD" >
- <configuration>
- <mappers>
- <!--mapper mapping file under scan path--
- <mapper resource="Mappers/usermapper.xml"/>
- <!--the interface file under the scan package---
- < packagename="Edu.zju.bme.data.manage.mapper"/>
- </mappers>
- </configuration></span>
2.1.2 Using Spring
Use spring's configuration file as follows:
[Java]View PlainCopy
- <span style="FONT-SIZE:14PX;" ><beans xmlns="Http://www.springframework.org/schema/beans"
- xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mybatis="Http://mybatis.org/schema/mybatis-spring"
- Xsi:schemalocation= "
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd ">
- <!--Configure interface-stored packages to scan the Mapper interface--
- <mybatis:scan base-package="Edu.zju.bme.data.manage.mapper"/>
- <bean id="sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean" >
- <!--Configure the Mapper file location, scan the map file, you can use the Ant-style path format--
- <property name="mapperlocations" value="Classpath*:mappers/**/*.xml"/>
- // ...
- </bean>
- </beans></span>
MyBatis interface and corresponding mapper file location configuration detailed