当前位置: 首页 > 知识库问答 >
问题:

Spring Boot看不到我的Mapstruct映射程序

吕鸿文
2023-03-14

我定义了映射器,但Spring Boot无法检测到它。我好几天都找不到问题。请帮忙。尝试了IDEA和NetBeans。试图从这个线程添加一些关于main类的注释。

UserMapper.java

package com.example.springmysqlelastic.mapper;

import com.example.springmysqlelastic.model.Food;
import com.example.springmysqlelastic.model.FoodModel;
import com.example.springmysqlelastic.model.User;
import com.example.springmysqlelastic.model.UserModel;
import com.example.springmysqlelastic.model.dto.FoodDTO;
import com.example.springmysqlelastic.model.dto.UserDTO;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;
//@Component
@Mapper(componentModel = "spring")
public interface UserMapper {

    UserDTO toUserDTO(User user);

    List<UserDTO> toUserDtos(List<User> users);

    User toUser(UserDTO userDTO);

    List<User> toUsers(List<UserDTO> userDTOS);

    UserModel toUserModel(User user);
/*
    FoodDTO toFoodDTO(Food food);

    List<FoodDTO> toFoodDtos(List<Food> foods);

    Food toFood(FoodDTO foodDTO);

    List<Food> toFoods(List<FoodDTO> foodDTOS);

    FoodModel toFoodModel(Food food);*/
}

用户服务

package com.example.springmysqlelastic.service.impl;

import com.example.springmysqlelastic.mapper.UserMapper;
import com.example.springmysqlelastic.model.User;
import com.example.springmysqlelastic.model.dto.UserDTO;
import com.example.springmysqlelastic.repo.IUserDAO;
import com.example.springmysqlelastic.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserService {

    private IUserDAO userDAO;
    private UserMapper userMapper;

    @Autowired
    public UserService(IUserDAO userDAO, UserMapper userMapper) {
        this.userDAO = userDAO;
        this.userMapper = userMapper;
    }
    
    @Override
    public UserDTO save(UserDTO userDTO) {
        User user = this.userDAO.save(this.userMapper.toUser(userDTO));
        return this.userMapper.toUserDTO(user);
    }

    @Override
    public UserDTO findById(Long id) {
        return this.userMapper.toUserDTO(this.userDAO.findById(id).orElse(null));
    }

    @Override
    public List<UserDTO> findAll() {
        return this.userMapper.toUserDtos(this.userDAO.findAll());
    }
}

UserController.java

package com.example.springmysqlelastic.rest;

import com.example.springmysqlelastic.model.dto.UserDTO;
import com.example.springmysqlelastic.service.IUserService;
import com.example.springmysqlelastic.utils.PathResources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user") //PathResources.USER
public class UserController {

    private final IUserService userService;

    @Autowired
    public UserController(IUserService userService) {
        this.userService = userService;
    }

    @PostMapping("/save") //PathResources.SAVE
    public ResponseEntity<UserDTO> saveUser(@RequestBody UserDTO userDTO) {
        return new ResponseEntity<>(this.userService.save(userDTO), HttpStatus.OK);
    }

    @GetMapping("/find-one/{id}") //PathResources.FIND_ONE + "/{" + PathResources.ID + "}"
    public ResponseEntity<UserDTO> findById(@PathVariable Long id) {
        return new ResponseEntity<>(this.userService.findById(id), HttpStatus.OK);
    }

    @GetMapping("/find-all") //PathResources.FIND_ALL
    public ResponseEntity<List<UserDTO>> findById() {
        return new ResponseEntity<>(this.userService.findAll(), HttpStatus.OK);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-mysql-elastic</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-mysql-elastic</name>
    <description>Demo project for Mysql and ElasticSearch Synchronization in Spring</description>

    <properties>
        <java.version>1.8</java.version>
        <mapstruct.version>1.4.0.Beta3</mapstruct.version>
        <org.json.version>20190722</org.json.version>
        <swagger.version>2.9.2</swagger.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
                <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>${org.json.version}</version>
        </dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson 
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.73</version>
</dependency>-->

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <!--<version>1.18.12</version>-->
            <type>jar</type>
        </dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.6</version>
</dependency>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>2.0.2</version>
            <type>jar</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.example.springmysqlelastic.SpringMysqlElasticApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

主类

package com.example.springmysqlelastic;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
//@SpringBootApplication(scanBasePackages={"com.example.springmysqlelastic"})
@EnableElasticsearchRepositories("com.example.springmysqlelastic.repo.elastic")
@EnableScheduling
//@ComponentScan(scanBasePackages = {"com.example.springmysqlelastic"})
//@EntityScan("com.example.springmysqlelastic.model")
//@EnableJpaRepositories("com.example.springmysqlelastic")
//@ComponentScan(basePackages = {"com.example.springmysqlelastic"})
//@EnableAutoConfiguration

public class SpringMysqlElasticApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringMysqlElasticApplication.class, args);
    }

}

共有1个答案

赵雅懿
2023-03-14

在文档中指出,除了主依赖项之外,还需要添加annotationProcessor

Gradle的示例:

dependencies {
    implementation 'org.mapstruct:mapstruct:1.4.2.Final'
    annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final'
}
 类似资料:
  • 目标对象 我希望每个都是的映射,带有,mapstruct生成如下所示: 但相反,我得到了一个编译错误: 源参数中不存在名为“Roleids”的属性。你是说“空”吗?

  • 我有两种将实体映射到域的方法。 当我试图定义实体列表到域的映射方法时,我发现了用于映射集合元素的模糊映射方法。 有没有一种方法可以定义用于映射对象集合的方法

  • 例如,我有以下接口映射器: 在代码中,您可以看到映射和一些默认方法,其中包含其他映射。如何在Mapstruct映射中使用这些方法,以便Mapstruct使用这些方法在字段中填充值?

  • 我尝试使用MapStruct编写映射器类,如下所示: 目前它显示了“未知属性”“customer.customerid”和“usertypes.usertype.userid”等错误。有人能帮我用MapStruct映射所有这些元素吗? 问题2:我们如何绘制跟踪图?1)customerId usertypes->user->userid 2)pdtPrice offers->OffersType->

  • 下面的例子中,我有一个单独的域层和一个单独的持久层。我使用Mapstruct进行映射,当从域映射到实体或从实体映射到域时,会出现堆栈溢出,因为双向引用总是被调用- 用于映射的类非常基本

  • 我想将泛型类型<code>Y</code>的对象映射到另一个泛型类型为<code>X</code>的对象。在mapstruct中有这样的功能吗?或者我必须为通用映射编写自定义映射器吗?当我编译上面的代码时,会出现编译错误。