[英]Auto Scanning mappers in mybatis-spring
Our project structure is as follows: 我们的项目结构如下:
- basePkg
|- pkg1
|- dao1
|- mybatis-mappers...
|- classes...
|- pkg2
|- dao2
|- mybatis-mappers...
|- classes...
Right now, whenever we add a mybatis mapper to any of the dao packages, we have to add a new entry like the following to spring context.xml 现在,无论何时将mybatis映射器添加到任何dao包中,我们都必须在spring context.xml中添加如下所示的新条目。
<bean id="testDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="basePkg.pkg1.dao1.testDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
It's getting cumbersome to keep adding a new entry for every new mapper. 不断为每个新的映射器添加新条目变得越来越麻烦。 An alternative I found was to use <mybatis:scan \\>
. 我发现的另一种方法是使用<mybatis:scan \\>
。 I'm facing two issues with this approach: 我使用这种方法面临两个问题:
basPkg.dao*
package. 这意味着对于每个basPkg.dao*
包,我都必须具有单独的扫描元素。 Instead, is there a way to use a single scan element and specify all the dao
subpackages using a regular expression? 相反,有没有办法使用单个扫描元素并使用正则表达式指定所有dao
子包? I don't have to put this: 我不必这样:
<bean id="testDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="basePkg.pkg1.dao1.testDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
libraries used: 使用的库:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
And making sure 并确保
if the XML files are in src\\main\\java, then something like below is required: 如果XML文件位于src \\ main \\ java中,则需要如下所示:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-resource</id>
<phase>generate-resources</phase>
<goals>
<goal>add-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Hope the above helps. 希望以上内容对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.