gpt4 book ai didi

java - MapStruct 实现在 Spring Boot Web 应用程序中不起作用

转载 作者:行者123 更新时间:2023-12-03 21:39:49 24 4
gpt4 key购买 nike

我是 Spring Boot 和 MapStruct 的新手工具。

早些时候,一个项目(由其他团队使用这些技术编写)没有启动。然后,我在 Mapper 抽象类中做了一些更改,但现在映射器对象在应用程序启动时变为 null。

映射器抽象类:

@Mapper(componentModel = "spring")
public abstract class UserAndEmployeeMapper {

public UserAndEmployeeMapper INSTANCE = Mappers.getMapper( UserAndEmployeeMapper.class );

@Mapping(source = "username", target = "name")
@Mapping(source = "ssn", target = "ssn", defaultValue = "xxxxxx" )
@Mapping(target = "salary", constant = "34.67")
@Mapping(target = "dob", dateFormat = "dd/MM/yyyy", constant = "10/12/2002")
public abstract Employee mapToEmployee(User user);

public abstract List<Employee> mapToEmployee(List<User> users);

@Mapping(source = "name", target = "username")
public abstract User mapToUser(Employee employee);

public abstract List<User> mapToUser(List<Employee> employees);

}

LoginServiceImpl 类
@Service("loginService")
public class LoginServiceImpl implements LoginService{

private static final AtomicLong counter = new AtomicLong();

@Autowired
private EmployeeDao employeeDao;

private UserAndEmployeeMapper userAndEmployeeMapper;
...

}

pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.jdk8.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</build>

在 LoginServiceImpl 中添加 @Autowired 后,应用程序未启动并显示以下错误日志
***************************
APPLICATION FAILED TO START
***************************

Description:

Field userAndEmployeeMapper in org.service.impl.LoginServiceImpl required a bean of type 'org.mapper.UserAndEmployeeMapper' that could not be found.


Action:

Consider defining a bean of type 'org.mapper.UserAndEmployeeMapper' in your configuration.

有什么建议么 ?

最佳答案

将抽象类作为接口(interface)对我有用。

public interface UserAndEmployeeMapper {

关于java - MapStruct 实现在 Spring Boot Web 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44458089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com