Last active
February 11, 2019 11:21
-
-
Save sjyun/b1055d7f2ea29682be2b37ff96f3703f to your computer and use it in GitHub Desktop.
mapstruct
This file contains hidden or 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
buildscript { | |
ext { | |
springBootVersion = '2.1.2.RELEASE' | |
javaLanguageLevel = '1.8' | |
generatedMapperSourcesDir = "${buildDir}/generated-src/mapstruct/main" | |
} | |
repositories { | |
jcenter() | |
maven { | |
url "https://plugins.gradle.org/m2/" | |
} | |
} | |
dependencies { | |
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | |
classpath "net.ltgt.gradle:gradle-apt-plugin:0.21" | |
} | |
} | |
apply plugin: 'net.ltgt.apt-idea' | |
apply plugin: 'net.ltgt.apt-eclipse' | |
sourceSets.main { | |
ext.originalJavaSrcDirs = java.srcDirs | |
java.srcDir "${generatedMapperSourcesDir}" | |
} | |
tasks.withType(JavaCompile) { | |
options.compilerArgs = [ | |
'-Amapstruct.suppressGeneratorTimestamp=true' | |
] | |
} | |
configurations { | |
mapstruct | |
} | |
task generateMainMapperClasses(type: JavaCompile) { | |
ext.aptDumpDir = file( "${buildDir}/tmp/apt/mapstruct" ) | |
destinationDir = aptDumpDir | |
classpath = compileJava.classpath + configurations.mapstruct | |
source = sourceSets.main.originalJavaSrcDirs | |
ext.sourceDestDir = file ( "$generatedMapperSourcesDir" ) | |
options.define( | |
compilerArgs: [ | |
"-nowarn", | |
"-proc:only", | |
"-encoding", "UTF-8", | |
"-processor", "org.mapstruct.ap.MappingProcessor", | |
"-s", sourceDestDir.absolutePath, | |
"-source", rootProject.javaLanguageLevel, | |
"-target", rootProject.javaLanguageLevel, | |
] | |
); | |
inputs.dir source | |
outputs.dir generatedMapperSourcesDir; | |
doFirst { | |
sourceDestDir.mkdirs() | |
} | |
doLast { | |
aptDumpDir.delete() | |
} | |
} | |
compileJava.dependsOn generateMainMapperClasses | |
implementation group: 'org.mapstruct', name: 'mapstruct', version: '1.3.0.Beta2' | |
mapstruct "org.mapstruct:mapstruct-processor:1.3.0.Beta2" |
This file contains hidden or 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
package com.test.sb.mapdto; | |
import java.util.Date; | |
public class Employee { | |
private int id; | |
private String name; | |
private Date startDt; | |
public Date getStartDt() { | |
return startDt; | |
} | |
public void setStartDt(Date startDt) { | |
this.startDt = startDt; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
} | |
//EMploydto | |
package com.test.sb.mapdto; | |
public class EmployeeDto { | |
private int employeeId; | |
private String employeeName; | |
private String employeeStartDt; | |
public String getEmployeeStartDt() { | |
return employeeStartDt; | |
} | |
public void setEmployeeStartDt(String employeeStartDt) { | |
this.employeeStartDt = employeeStartDt; | |
} | |
public int getEmployeeId() { | |
return employeeId; | |
} | |
public void setEmployeeId(int employeeId) { | |
this.employeeId = employeeId; | |
} | |
public String getEmployeeName() { | |
return employeeName; | |
} | |
public void setEmployeeName(String employeeName) { | |
this.employeeName = employeeName; | |
} | |
} | |
This file contains hidden or 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
@Mapper | |
public interface EmployeeMapper { | |
@Mappings({ | |
@Mapping(target="employeeId", source="entity.id"), | |
@Mapping(target="employeeName", source="entity.name"), | |
@Mapping(target = "employeeStartDt", source = "entity.startDt", dateFormat = "dd-MM-yyyy HH:mm:ss") | |
}) | |
EmployeeDto employeeToEmployeeDTO(Employee entity); | |
@Mappings({ | |
@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") | |
}) | |
Employee employeeDTOtoEmployee(EmployeeDto dto); | |
} |
This file contains hidden or 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
package com.test.sb.dtoT; | |
import com.test.sb.mapdto.Employee; | |
import com.test.sb.mapdto.EmployeeDto; | |
import com.test.sb.mapdto.EmployeeMapper; | |
import org.junit.Test; | |
import org.mapstruct.factory.Mappers; | |
import static org.junit.Assert.assertEquals; | |
public class MapperTest { | |
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class); | |
private static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss"; | |
@Test | |
public void givenEmployeeDTOwithDiffNametoEmployee_whenMaps_thenCorrect() { | |
EmployeeDto dto = new EmployeeDto(); | |
dto.setEmployeeId(1); | |
dto.setEmployeeName("John"); | |
Employee entity = mapper.employeeDTOtoEmployee(dto); | |
assertEquals(dto.getEmployeeId(), entity.getId()); | |
assertEquals(dto.getEmployeeName(), entity.getName()); | |
} | |
@Test | |
public void givenEmployeewithDiffNametoEmployeeDTO_whenMaps_thenCorrect() { | |
Employee entity = new Employee(); | |
entity.setId(1); | |
entity.setName("John"); | |
EmployeeDto dto = mapper.employeeToEmployeeDTO(entity); | |
assertEquals(dto.getEmployeeId(), entity.getId()); | |
assertEquals(dto.getEmployeeName(), entity.getName()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment