首页 > 解决方案 > Mapstruct 实现

问题描述

我正在使用 mapstruct 将我的模型映射到我的 DTO。我想按全名搜索记录。我不明白为什么会出现以下错误:

Error creating bean with name 'customerController'
Error creating bean with name 'customerServiceImpl'
Error creating bean with name 'customerRepository'
No property name found for type Customer!

这是我的项目

public interface CustomerMapper {

    CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);
    @Mapping(source = "lastName", target = "lastName")
    CustomerDTO customerToCustomerDTO(Customer customer);
}
@Data
public class CustomerDTO {
    private String firstName;
    private String lastName;
}
@Data
@Entity
@Getter
@Setter
public class Customer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String firstName;
    private String name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomerListDTO {
    List<CustomerDTO> categories;
}
@Controller
@RequestMapping("api/v1/customers")

public class CustomerController {
    private final CustomerService customerService;


    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @GetMapping("{name}")
    public ResponseEntity<CustomerDTO> getCustomerByName(@PathVariable String name) {
        return new ResponseEntity<>(
                customerService.getCustomerByName(name), HttpStatus.OK
        );
    }
public interface CustomerRepository extends JpaRepository<Customer, Long> {
    Customer findByName(String x);
}
public interface CustomerService {
    CustomerDTO getCustomerByName(String name);
}
@AllArgsConstructor
@Service
public class CustomerServiceImpl implements CustomerService {

    CustomerMapper customerMapper;
    CustomerRepository customerRepository;

    @Override
    public CustomerDTO getCustomerByName(String lastName) {
        return customerMapper.customerToCustomerDTO(customerRepository.findByName(lastName));
    }
}

这是一个潜在的解决方法:将在 CustomerMapper 中映射以下内容,但对我来说感觉不对。

@Mapping(source = "name", target = "lastName")
@Mapping(source = "firstName", target = "firstName")

在文档中,据说您可以将模型中的任何字段映射到 DTO,我认为我的代码可能有问题。我尝试在 repo、service、controller 中实现的方式。

编辑:

也许解决方案是在存储库中使用 DTO?

更新:

@Override
    public CustomerDTO getCustomerByName(String lastName) {
        return customerRepository.findByName(lastName).map(customerMapper::customerToCustomerDTO);
    }

.map 不能使用。

要使用 .map 我应该使用这样的代码

    .findAll()
    .stream()
    .map(customerMapper::customerToCustomerDTO)
    .collect(Collectors.toList());

但是,我使用的是 findByName 方法,它无法访问 .map。

我该如何解决这个问题?

编辑

这就是我认为我的客户应该是什么样子

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CustomerDTO {
    private String id;
    private String firstName;
    private String lastName;
}

标签: springmapstruct

解决方案


“找不到类型客户的属性名称!”

在您的表客户中,您有一个名为“名称”的列?

下面我对您的代码进行了一些更改,但是如果您需要按名称查找您的存储库,则需要找到正确的搜索。当您使用 findByName 时,仅返回名称等于传入参数的名称的行。示例: findByName("Scilla") 仅返回列名等于“Scilla”的行,如果列名具有“scilla”(较低)或“Scilla abc”等值,则查询未返回此条目。

值为“Scilla”的方法 findByName 生成此查询:

select * from customer where name = 'Scilla';

代码更改

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomerRepository extends JpaRepository<Customer, Long> {
    
    Customer findByLastName(String lastName);
    
    List<Customer> findByLastNameContainingIgnoreCase(String name);

    List<Customer> findByLastNameContaining(String name);

}
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface CustomerMapper {

    CustomerMapper INSTANCE = Mappers.getMapper(CustomerMapper.class);

    CustomerDTO customerToCustomerDTO(Customer customer);

    Customer toDomain(CustomerDTO customerDTO);
    
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Entity
@NoArgsConstructor
public class Customer {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String firstName;
    
    private String lastName;
    
}
import lombok.Data;

@Data
public class CustomerDTO {

    private String firstName;
    private String lastName;

}
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;

@RestController
@RequestMapping("api/v1/customers")
public class CustomerController {

    private final CustomerService customerService;


    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @GetMapping("{name}")
    public ResponseEntity<CustomerDTO> getCustomerByName(@PathVariable String name) {
        return new ResponseEntity<>(
                customerService.getCustomerByName(name), HttpStatus.OK
        );
    }
    
    @PostMapping
    public ResponseEntity<CustomerDTO> getCustomerByName(@RequestBody CustomerDTO customerDTO ) {
        return new ResponseEntity<>(
                customerService.save(customerDTO), HttpStatus.OK
        );
    }
    
}

重要的

下面放 Spring Data 查询和翻译查询。

List<Customer> findByLastNameContainingIgnoreCase(String name)
select * from customer where last_name ilike = '%name%';

pom.xml 定义

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
        
        <org.mapstruct.version>1.4.1.Final</org.mapstruct.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.4.1.Final</version>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <forceJavacCompilerUse>true</forceJavacCompilerUse>
                    <release>11</release>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

推荐阅读