-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
316 additions
and
4 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/dto/AnotherCarDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.hzz.spring.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class AnotherCarDTO { | ||
private int id; | ||
private String name; | ||
private FuelType fuelType; | ||
} |
2 changes: 1 addition & 1 deletion
2
...in/java/org/hzz/spring/entity/CarDto.java → .../main/java/org/hzz/spring/dto/CarDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.hzz.spring.entity; | ||
package org.hzz.spring.dto; | ||
|
||
import lombok.Data; | ||
|
||
|
13 changes: 13 additions & 0 deletions
13
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/dto/DivisionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.hzz.spring.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DivisionDTO { | ||
private int id; | ||
private String name; | ||
} |
17 changes: 17 additions & 0 deletions
17
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/dto/EmployeeDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hzz.spring.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Date; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class EmployeeDTO { | ||
private int employeeId; | ||
private String employeeName; | ||
private DivisionDTO division; | ||
private Date employeeStartDt; | ||
} |
5 changes: 5 additions & 0 deletions
5
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/dto/FuelType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.hzz.spring.dto; | ||
|
||
public enum FuelType { | ||
ELECTRIC, BIO_DIESEL | ||
} |
13 changes: 13 additions & 0 deletions
13
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/dto/TransactionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.hzz.spring.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class TransactionDTO { | ||
private String uuid; | ||
private Long totalInCents; | ||
} |
13 changes: 13 additions & 0 deletions
13
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/entity/AnotherCar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.hzz.spring.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class AnotherCar { | ||
protected int id; | ||
protected String name; | ||
} |
7 changes: 7 additions & 0 deletions
7
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/entity/BioDieselCar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.hzz.spring.entity; | ||
|
||
public class BioDieselCar extends AnotherCar{ | ||
public BioDieselCar(int id, String name) { | ||
super(id, name); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/entity/Division.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.hzz.spring.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Division { | ||
private int id; | ||
private String name; | ||
} |
7 changes: 7 additions & 0 deletions
7
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/entity/ElectricCar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.hzz.spring.entity; | ||
|
||
public class ElectricCar extends AnotherCar{ | ||
public ElectricCar(int id, String name) { | ||
super(id, name); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/entity/Employee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.hzz.spring.entity; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
|
||
@Data | ||
public class Employee { | ||
private int id; | ||
private String name; | ||
private Division division; | ||
private String startDt; | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/entity/Transaction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.hzz.spring.entity; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.UUID; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class Transaction { | ||
private Long id; | ||
private String uuid = UUID.randomUUID().toString(); | ||
private BigDecimal total; | ||
} |
34 changes: 34 additions & 0 deletions
34
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/mapper/AnotherCarMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.hzz.spring.mapper; | ||
|
||
import org.hzz.spring.dto.AnotherCarDTO; | ||
import org.hzz.spring.dto.FuelType; | ||
import org.hzz.spring.entity.AnotherCar; | ||
import org.hzz.spring.entity.BioDieselCar; | ||
import org.hzz.spring.entity.ElectricCar; | ||
import org.mapstruct.AfterMapping; | ||
import org.mapstruct.BeforeMapping; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingTarget; | ||
|
||
@Mapper(componentModel = "spring") | ||
public abstract class AnotherCarMapper { | ||
|
||
@BeforeMapping | ||
protected void enrichDTOWithFuelType(AnotherCar car, | ||
@MappingTarget AnotherCarDTO carDTO){ | ||
if (car instanceof ElectricCar) { | ||
carDTO.setFuelType(FuelType.ELECTRIC); | ||
} | ||
|
||
if (car instanceof BioDieselCar){ | ||
carDTO.setFuelType(FuelType.BIO_DIESEL); | ||
} | ||
} | ||
|
||
public abstract AnotherCarDTO carToCarDto(AnotherCar car); | ||
|
||
@AfterMapping | ||
protected void convertNameToUpperCase(@MappingTarget AnotherCarDTO carDTO){ | ||
carDTO.setName(carDTO.getName().toUpperCase()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/mapper/CarandCarDTOMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/mapper/EmployeeAndDTOMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.hzz.spring.mapper; | ||
|
||
import org.hzz.spring.dto.DivisionDTO; | ||
import org.hzz.spring.dto.EmployeeDTO; | ||
import org.hzz.spring.entity.Division; | ||
import org.hzz.spring.entity.Employee; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(componentModel = "spring") | ||
public abstract class EmployeeAndDTOMapper { | ||
@Mapping(target = "id", source = "dto.employeeId") | ||
@Mapping(target = "name", source = "dto.employeeName") | ||
@Mapping(target = "startDt", source = "dto.employeeStartDt", dateFormat = "dd-MM-yyyy HH:mm:ss") | ||
public abstract Employee employeeDTOToEmployee(EmployeeDTO dto); | ||
|
||
public abstract Division divisionDTOtoDivision(DivisionDTO dto); | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
javahelper/mapstruct-tutorial/src/main/java/org/hzz/spring/mapper/TransactionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.hzz.spring.mapper; | ||
|
||
import org.hzz.spring.dto.TransactionDTO; | ||
import org.hzz.spring.entity.Transaction; | ||
import org.mapstruct.Mapper; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
@Mapper(componentModel = "spring") | ||
public abstract class TransactionMapper { | ||
|
||
public TransactionDTO transactionToTransactionDTO(Transaction transaction) { | ||
if ( transaction == null ) { | ||
return null; | ||
} | ||
TransactionDTO transactionDTO = new TransactionDTO(); | ||
|
||
transactionDTO.setUuid( transaction.getUuid() ); | ||
transactionDTO.setTotalInCents(transaction.getTotal().multiply( | ||
new BigDecimal("100") | ||
).longValue()); | ||
|
||
return transactionDTO; | ||
} | ||
|
||
public abstract List<TransactionDTO> toTransactionDTO(Collection<Transaction> transactions); | ||
} |
30 changes: 30 additions & 0 deletions
30
javahelper/mapstruct-tutorial/src/test/java/org/hzz/mapper/AnotherCarMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.hzz.mapper; | ||
|
||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.hzz.spring.dto.AnotherCarDTO; | ||
import org.hzz.spring.entity.AnotherCar; | ||
import org.hzz.spring.entity.BioDieselCar; | ||
import org.hzz.spring.mapper.AnotherCarMapper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@Slf4j | ||
public class AnotherCarMapperTest { | ||
|
||
@Autowired | ||
private AnotherCarMapper anotherCarMapper; | ||
|
||
@Test | ||
public void test(){ | ||
AnotherCar car = new BioDieselCar(1001, "q10_car"); | ||
AnotherCarDTO anotherCarDTO = anotherCarMapper.carToCarDto(car); | ||
System.out.println(anotherCarDTO); | ||
// AnotherCarDTO(id=1001, name=Q10_CAR, fuelType=BIO_DIESEL) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
javahelper/mapstruct-tutorial/src/test/java/org/hzz/mapper/EmployAndDTOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.hzz.mapper; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.hzz.spring.dto.DivisionDTO; | ||
import org.hzz.spring.dto.EmployeeDTO; | ||
import org.hzz.spring.entity.Employee; | ||
import org.hzz.spring.mapper.EmployeeAndDTOMapper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.Date; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@Slf4j | ||
public class EmployAndDTOTest { | ||
@Autowired | ||
private EmployeeAndDTOMapper employeeAndDTOMapper; | ||
|
||
@Test | ||
public void testChildren(){ | ||
EmployeeDTO employeeDTO = new EmployeeDTO(); | ||
employeeDTO.setEmployeeId(1); | ||
employeeDTO.setEmployeeName("Q10Viking"); | ||
employeeDTO.setEmployeeStartDt(new Date()); | ||
employeeDTO.setDivision(new DivisionDTO(1,"IT")); | ||
|
||
Employee employee = employeeAndDTOMapper.employeeDTOToEmployee(employeeDTO); | ||
log.info(employee.toString()); | ||
// org.hzz.mapper.EmployAndDTOTest : Employee(id=1, name=Q10Viking, division=Division(id=1, name=IT), startDt=02-06-2023 22:02:46) | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
javahelper/mapstruct-tutorial/src/test/java/org/hzz/mapper/TransactionMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.hzz.mapper; | ||
|
||
import org.hzz.spring.dto.TransactionDTO; | ||
import org.hzz.spring.entity.Transaction; | ||
import org.hzz.spring.mapper.TransactionMapper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
public class TransactionMapperTest { | ||
|
||
@Autowired | ||
private TransactionMapper transactionMapper; | ||
|
||
@Test | ||
public void test(){ | ||
Transaction transaction = new Transaction(); | ||
transaction.setId(1l); | ||
transaction.setTotal(new BigDecimal(2)); | ||
|
||
TransactionDTO transactionDTO = transactionMapper.transactionToTransactionDTO(transaction); | ||
System.out.println(transactionDTO); | ||
// TransactionDTO(uuid=fde2ef85-8269-4808-9eb6-e1d27de2376f, totalInCents=200) | ||
} | ||
} |