Java中Mapper同名的解決方式
問題描述

問題復(fù)現(xiàn)
- 準備一個demo命名,架構(gòu)如下圖所示:

- application.yml文件如下所示:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true # 控制臺輸出sql、下劃線轉(zhuǎn)駝峰
mapper-locations:
- classpath*:com/example/mapper/xml/*.xml
- classpath*:com/example/othermapper/xml/*.xml
spring:
datasource:
driver-class-name: org.postgresql.Driver
username: postgres
password: 123456
url: jdbc:postgresql://localhost:5432/star_chat
3.準備兩個同名Mapper,這里使用的是UserMapper
//@Repository("otherMapper")
public interface UserMapper extends BaseMapper<User> {
}
4.pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
- 使用的是mybaits-plus,按其規(guī)則完成配置
- 啟動Main
解決方案
在同名的Mapper中,使用注解@Repository("otherMapper"),為該Mapper起一個別名作為區(qū)分,在這個項目中,只需要標注一個就可以解決該問題,而在一些自定義的配置中可能會不生效,需要同名的Mapper都另起不同的別名加以區(qū)分才能正常注入
應(yīng)用場景
一個項目中將別的項目的代碼拷貝在該項目中,出現(xiàn)了Mapper同名的情況,這個情況下,不想重命名Mapper可以使用該方案
到此這篇關(guān)于Java中Mapper同名的解決方式的文章就介紹到這了,更多相關(guān)Java Mapper同名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java通過PropertyDescriptor反射調(diào)用set和get方法
這篇文章主要為大家詳細介紹了Java通過PropertyDescriptor反射調(diào)用set和get方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03

