MyBatis interface and corresponding mapper file location configuration detailed

Source: Internet
Author: User

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
  1. <span style="FONT-SIZE:14PX;" ><build>
  2. <resources>
  3. <resource>
  4. <directory>src/main/java</directory>
  5. <includes>
  6. <include>**/*.xml</include>
  7. </includes>
  8. <filtering>false</filtering>
  9. </resource>
  10. </resources>
  11. </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
  1. <span style="FONT-SIZE:14PX;" ><?xml version="1.0" encoding="UTF-8"?>
  2. <! DOCTYPE Configuration
  3. Public "-//mybatis.org//dtd Config 3.0//en"
  4. "HTTP://MYBATIS.ORG/DTD/MYBATIS-3-CONFIG.DTD" >
  5. <configuration>
  6. <mappers>
  7. <!--mapper mapping file under scan path--
  8. <mapper resource="Mappers/usermapper.xml"/>
  9. <!--the interface file under the scan package---
  10. < packagename="Edu.zju.bme.data.manage.mapper"/>
  11. </mappers>
  12. </configuration></span>
2.1.2 Using Spring
Use spring's configuration file as follows:

[Java]View PlainCopy
  1. <span style="FONT-SIZE:14PX;" ><beans xmlns="Http://www.springframework.org/schema/beans"
  2. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:mybatis="Http://mybatis.org/schema/mybatis-spring"
  4. Xsi:schemalocation= "
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd ">
  7. <!--Configure interface-stored packages to scan the Mapper interface--
  8. <mybatis:scan base-package="Edu.zju.bme.data.manage.mapper"/>
  9. <bean id="sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean" >
  10. <!--Configure the Mapper file location, scan the map file, you can use the Ant-style path format--
  11. <property name="mapperlocations" value="Classpath*:mappers/**/*.xml"/>
  12. // ...  
  13. </bean>
  14. </beans></span>

MyBatis interface and corresponding mapper file location configuration detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.