1. 程式人生 > >spring boot掃描mapper檔案

spring boot掃描mapper檔案

 

一個簡單的功能,百度查的都是XX,谷歌萬歲.

因為掃描不到自動生成的mapper就無法注入到service

方案一[email protected]

如果Mapper檔案所在的包和你的配置mapper的專案的pom定義的groupid相同的話.

因為我的mapper是一個模組,portal一個模組.

mapper在com.haitian.mapper下

portal的groupid是com.haitian,這樣可以直接掃描到.

但是這樣有兩個需要考慮的地方:

1.mybatis-generator生成的mapper並沒有@mapper

配置檔案不行,好像外掛可以

https://github.com/mybatis/generator/issues/184

還得折騰一會

2.如果第一個折騰出來了,預設會掃描com.haitian下的所有包來找@Mapper

雖然對執行效率不會有什麼影響,啟動就會變慢啊,浪費時間.

所以不推薦

方案二.

mybatis.mapper-locations=com.haitian.mapper/*.xml

不管用,有人說properties管用,我yml和propertis都試了,都不管用

方法三.載入xml檔案

<!-- 配置掃描包,載入mapper代理物件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.haitian.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的開發理念,不美麗.

方案四:

@MapperScan("com.haitian.mapper")

一行搞定.