1. 程式人生 > >如何設置Spring Boot掃描mapper文件

如何設置Spring Boot掃描mapper文件

推薦 batis strong spa 模塊 ota nbsp ssp sta

一、掃描mapper接口文件:

1、推薦:

在Application.java啟動文件中,加註解:

@MapperScan("com.xxx.mapper")

2、

@Mapper
因為我的mapper是一個模塊,portal一個模塊.
mapper在com.xxx.mapper下
portal的groupid是com.xxx,這樣可以@Mapper直接掃描到.
但是
mybatis-generator生成的mapper並沒有@mapper,即使折騰出來了,雖然對運行效率不會有什麽影響,啟動就會變慢,浪費時間,所以不推薦。

二、掃描mapper.xml文件

1、application.properties

配置:mybatis.mapper-locations: classpath:mappers/*.xml

2、

<!-- 配置掃描包,加載mapper代理對象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.mapper"></property>
</bean>

@SpringBootApplication
@ImportResource(locations = "classpath:spring-dao.xml")
public class PortalApplication {

public static void main(String[] args) {
SpringApplication.run(PortalApplication.class, args);
}
}

如何設置Spring Boot掃描mapper文件