简体   繁体   English

在mybatis-spring中自动扫描映射器

[英]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: 我使用这种方法面临两个问题:

  • I don't want to give the basePkg directly as it would entail Spring scanning the whole project structure for mappers which would affect performance. 我不想直接提供basePkg,因为它需要Spring扫描整个项目结构的映射器,这会影响性能。 This would mean that I would have to have a separate scan element for each 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子包?
  • We use two DataSources (MySQL and RedShift). 我们使用两个数据源(MySQL和RedShift)。 How do I account for this while addressing the first issue listed above? 解决上面列出的第一个问题时,我该如何解决?

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 并确保

  1. TestMapper.java interface will have TestMapper.xml TestMapper.java接口将具有TestMapper.xml
  2. or just TestMapper.java with annotations 或者只是带有注释的TestMapper.java

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM