客户服务实施
package com.application.service.impl;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerRepository custRepo;
@Autowired
private CustomerMapper mapper;
@Autowired
private HMergeMapper mergeMapper;
@Override
public CustomerViewData addCustomer(CustomerData custData) {
Customer cr = this.mapper.customerDataToCustomer(custData);
cr = this.custRepo.save(cr);
return this.mapper.fromCustToCustViewData(cr);
}
}
客户服务界面
package com.application.service;
@Component
public interface CustomerService {
CustomerViewData addCustomer(CustomerData custData);
}
客户地图
package com.application.mapper;
@Mapper(componentModel = "spring")
public interface CustomerMapper {
CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);
CustomerViewData fromCustToCustViewData(Customer entity);
}
主类
package com.application;
@EnableSwagger2
@SpringBootApplication
@ComponentScan(basePackages = { "com.application", "com.application.mapper" })
@EnableJpaRepositories(basePackages = {"com.application.repository"})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
错误 -
应用程序无法启动
说明:
com.application.service.impl.CustomerServiceImpl 中的字段映射器需要一个无法找到的“com.application.mapper.CustomerMapper”类型的 bean。
操作:
考虑在您的配置中定义一个“com.application.mapper.CustomerMapper”类型的 bean。
Note - Package structure is Main Class in com.appliaction,
Mapper class in com.application.mapper,
CustomerService class in com.application.service,
CustomerServiceImpl class in com.application.service.impl
答案 0 :(得分:1)
删除 CustomerMapper.class 中的 CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);
。这对我来说似乎是递归地狱。
你在CustomerMapper.class
里面,用this.
做你想做的事
在 CustomerMapper.class
之外只需使用自动装配来使用映射器类。
您已经定义了 componentModel = "spring"
,因此它将绑定到 spring 上下文。不要将映射器的手动检索与 spring 上下文混合在一起。
可能这会导致无法创建实例,然后 spring 抛出错误。
答案 1 :(得分:0)
这是固定的,问题是 Maven 插件。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>