mapstruct mapper 和mybatis mapper 冲突扫描不上
时间: 2023-08-03 22:08:47 浏览: 176
如果同时使用 MapStruct 和 MyBatis,可能会遇到 Mapper 扫描冲突的问题。这是因为两者都使用了 `@Mapper` 注解,并且默认的 Mapper 扫描路径也是相同的。
解决这个问题的方法是,为 MapStruct 的 Mapper 和 MyBatis 的 Mapper 指定不同的扫描路径。可以在启动类上使用 `@MapperScan` 注解,分别指定 MapStruct 和 MyBatis 的 Mapper 所在的包路径,例如:
```
@MapperScan(basePackages = "com.example.mapstruct.mapper", annotationClass = Mapper.class)
@org.mybatis.spring.annotation.MapperScan(basePackages = "com.example.mybatis.mapper", annotationClass = Mapper.class)
@SpringBootApplication
public class Application {
// ...
}
```
上面的代码中,`@MapperScan` 注解中的 `basePackages` 属性分别指定了 MapStruct 和 MyBatis 的 Mapper 所在的包路径,`annotationClass` 属性指定了 Mapper 接口上所使用的注解类型。
这样就可以避免 Mapper 扫描冲突的问题了。
相关问题
mapstruct mapper 和mybatis mapper 冲突扫描不上 用yml配置解决
使用 YAML 配置文件也可以解决 MapStruct Mapper 和 MyBatis Mapper 扫描冲突的问题。可以在 `application.yml` 文件中分别指定 MapStruct 和 MyBatis 的 Mapper 所在的包路径,例如:
```
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.mybatis.entity
base-package: com.example.mybatis.mapper
mapstruct:
componentModel: spring
unmappedTargetPolicy: IGNORE
typeConversionPolicy: ERROR
typeConversionConfig: com.example.mapstruct.config.MapStructTypeConversionConfig
mappingConfig: com.example.mapstruct.config.MapStructMappingConfig
mapperPackages:
- com.example.mapstruct.mapper
```
上面的代码中,`mybatis` 节点下的 `base-package` 属性指定了 MyBatis 的 Mapper 所在的包路径,`mapstruct` 节点下的 `mapperPackages` 属性指定了 MapStruct 的 Mapper 所在的包路径。
需要注意的是,如果同时使用了 YAML 配置文件和 Java 配置类,以 YAML 配置文件为准。
tkmapper和mybatis plus哪一个用的人多
由于我是一个AI语言模型,没有具体的数据可以进行比较。但是从我的训练数据中,可以看到Mybatis Plus是一个非常流行的Java ORM框架,它具有简化CRUD操作、提供代码生成器、强大的Lambda查询、乐观锁、多租户等功能。而TkMapper是基于Mybatis的通用Mapper,它可以自动完成单表的CRUD操作,但是相对来说功能比Mybatis Plus要弱一些。两者各有优缺点,具体使用还需根据实际情况进行选择。