涓氬姟浠g爜鐨勬晳鏄熲€斺€擩ava 瀵硅薄杞崲妗嗘灦 MapStruct 濡欑敤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓氬姟浠g爜鐨勬晳鏄熲€斺€擩ava 瀵硅薄杞崲妗嗘灦 MapStruct 濡欑敤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/system' title='system'>system inf 璁剧疆 浠婂ぉ email https ble 鍘熷洜
涓氬姟鍦烘櫙
鍋囪鐜板湪鏈夎繖涔堜釜鍦烘櫙锛屼粠鏁版嵁搴撴煡璇㈠嚭鏉ヤ簡涓€涓?user 瀵硅薄锛堝寘鍚?id锛岀敤鎴峰悕锛屽瘑鐮侊紝鎵嬫満鍙凤紝閭锛岃鑹茶繖浜涘瓧娈碉級鍜屼竴涓搴旂殑瑙掕壊瀵硅薄 role锛堝寘鍚?id锛岃鑹插悕锛岃鑹叉弿杩拌繖浜涘瓧娈碉級锛岀幇鍦ㄥ湪 controller
闇€瑕佺敤鍒?user 瀵硅薄鐨?id锛岀敤鎴峰悕锛屽拰瑙掕壊瀵硅薄鐨勮鑹插悕涓変釜灞炴€с€備竴绉嶆柟寮忔槸鐩存帴鎶婁袱涓璞′紶閫掑埌 controller
灞傦紝浣嗘槸杩欐牱浼氬鍑哄緢澶氭病鐢ㄧ殑灞炴€с€傛洿閫氱敤鐨勬柟寮忔槸闇€瑕佺敤鍒扮殑灞炴€у皝瑁呮垚涓€涓被(DTO)锛岄€氳繃浼犺緭杩欎釜绫荤殑瀹炰緥鏉ュ畬鎴愭暟鎹紶杈撱€?/p>
@AllArgsConstructor
@Data
public class User
private Long id;
private String username;
private String password;
private String phoneNum;
private String email;
private Role role;
Role.java
@AllArgsConstructor
@Data
public class Role
private Long id;
private String roleName;
private String description;
UserRoleDto.java
@Data
public class UserRoleDto
/**
* 鐢ㄦ埛id
*/
private Long userId;
/**
* 鐢ㄦ埛鍚? */
private String name;
/**
* 瑙掕壊鍚? */
private String roleName;
MainTest.java
娴嬭瘯绫伙紝妯℃嫙灏?user 瀵硅薄杞崲鎴?UserRoleDto 瀵硅薄
public class MainTest
User user = null;
/**
* 妯℃嫙浠庢暟鎹簱涓煡鍑?user 瀵硅薄
*/
@Before
public void before()
Role role = new Role(2L, "administrator", "瓒呯骇绠$悊鍛?quot;);
user = new User(1L, "zhangsan", "12345", "17677778888", "123@qq.com", role);
/**
* 妯℃嫙鎶?user 瀵硅薄杞崲鎴?UserRoleDto 瀵硅薄
*/
@Test
public void test1()
UserRoleDto userRoleDto = new UserRoleDto();
userRoleDto.setUserId(user.getId());
userRoleDto.setName(user.getUsername());
userRoleDto.setRoleName(user.getRole().getRoleName());
System.out.println(userRoleDto);
Role.java
@AllArgsConstructor
@Data
public class Role
private Long id;
private String roleName;
private String description;
UserRoleDto.java
@Data
public class UserRoleDto
/**
* 鐢ㄦ埛id
*/
private Long userId;
/**
* 鐢ㄦ埛鍚? */
private String name;
/**
* 瑙掕壊鍚? */
private String roleName;
MainTest.java
娴嬭瘯绫伙紝妯℃嫙灏?user 瀵硅薄杞崲鎴?UserRoleDto 瀵硅薄
public class MainTest
User user = null;
/**
* 妯℃嫙浠庢暟鎹簱涓煡鍑?user 瀵硅薄
*/
@Before
public void before()
Role role = new Role(2L, "administrator", "瓒呯骇绠$悊鍛?quot;);
user = new User(1L, "zhangsan", "12345", "17677778888", "123@qq.com", role);
/**
* 妯℃嫙鎶?user 瀵硅薄杞崲鎴?UserRoleDto 瀵硅薄
*/
@Test
public void test1()
UserRoleDto userRoleDto = new UserRoleDto();
userRoleDto.setUserId(user.getId());
userRoleDto.setName(user.getUsername());
userRoleDto.setRoleName(user.getRole().getRoleName());
System.out.println(userRoleDto);
杩愯缁撴灉
涓婅竟鐨勪唬鐮佹垨璁告殏鏃剁湅璧锋潵杩樻槸姣旇緝绠€娲佺殑锛屼絾鏄垜浠渶瑕佹敞鎰忕殑涓€鐐瑰氨鏄钩鏃朵笟鍔″紑鍙戜腑鐨勫璞″睘鎬ц繙涓嶆槸涓婅堪浠g爜涓畝绠€鍗曞崟鐨勫嚑涓瓧娈碉紝鏈夊彲鑳戒細鏈夋暟鍗佷釜瀛楁锛屽悓鐞嗕篃浼氭暟鍗佷釜瀵硅薄闇€瑕佽浆鎹紝鎴戜滑濡傛灉杩樻槸閫氳繃 getter銆乻etter 鐨勬柟寮忔妸涓€涓璞″睘鎬у€煎鍒跺埌鍙︿竴涓璞′腑鍘昏繕鏄潪甯搁夯鐑︾殑锛屼笉杩囦笉鐢ㄦ媴蹇冿紝浠婂ぉ瑕佷粙缁嶇粰澶у鐨?MapStruct 灏辨槸鐢ㄤ簬瑙e喅杩欑闂鐨勩€?/p>
瀹炵幇鏂瑰紡涔嬩娇鐢?MapStruct
杩欓噷鎴戜滑娌跨敤涓婅堪浠g爜涓殑鍩烘湰瀵硅薄 User.java
銆?code>Role.java銆?code>UserRoleDto.java銆?br />
鐒跺悗鏂板缓涓€涓?UserRoleMapper.java
锛岃繖涓潵鐢ㄦ潵瀹氫箟 User.java
銆?code>Role.java鍜?code>UserRoleDto.java涔嬮棿灞炴€у搴旇鍒欍€?/p>
鍦ㄨ繖涔嬪墠鎴戜滑闇€瑕佸紩鍏?MapStruct 鐨?pom 寮曠敤锛?/p>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.0.Final</version>
</dependency>
UserRoleMapper.java
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
* @Mapper 瀹氫箟杩欐槸涓€涓狹apStruct瀵硅薄灞炴€ц浆鎹㈡帴鍙o紝鍦ㄨ繖涓被閲岄潰瑙勫畾杞崲瑙勫垯
* 鍦ㄩ」鐩瀯寤烘椂锛屼細鑷姩鐢熸垚鏀规帴鍙g殑瀹炵幇绫伙紝杩欎釜瀹炵幇绫诲皢瀹炵幇瀵硅薄灞炴€у€煎鍒? */
@Mapper
public interface UserRoleMapper
/**
* 鑾峰彇璇ョ被鑷姩鐢熸垚鐨勫疄鐜扮被鐨勫疄渚? * 鎺ュ彛涓殑灞炴€ч兘鏄?public static final 鐨?
* 鏂规硶閮芥槸public abstract 鐨? */
UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);
/**
* 杩欎釜鏂规硶灏辨槸鐢ㄤ簬瀹炵幇瀵硅薄灞炴€у鍒剁殑鏂规硶
*
* @Mapping 鐢ㄦ潵瀹氫箟灞炴€у鍒惰鍒?
* source 鎸囧畾婧愬璞″睘鎬?
* target 鎸囧畾鐩爣瀵硅薄灞炴€? *
* @param user 杩欎釜鍙傛暟灏辨槸婧愬璞★紝涔熷氨鏄渶瑕佽澶嶅埗鐨勫璞? * @return 杩斿洖鐨勬槸鐩爣瀵硅薄锛屽氨鏄渶缁堢殑缁撴灉瀵硅薄
*/
@Mappings(
@Mapping(source = "id", target = "userId"),
@Mapping(source = "username", target = "name"),
@Mapping(source = "role.roleName", target = "roleName")
)
UserRoleDto toUserRoleDto(User user);
娴嬭瘯涓€涓嬬粨鏋?/p>
MainTest.java
/**
* 妯℃嫙閫氳繃MapStruct鎶妘ser瀵硅薄杞崲鎴怳serRoleDto瀵硅薄
*/
@Test
public void test2()
UserRoleDto userRoleDto = UserRoleMapper.INSTANCES.toUserRoleDto(user);
System.out.println(userRoleDto);
鍛冿紝寰堟槑鏄撅紝杩愯绔熺劧鎶ラ敊浜嗭紝鍏蜂綋寮傚父濡備笅锛?/p>
鏍稿績鏄繖涓€鍙?锛?code>java.lang.ClassNotFoundException: Cannot find implementation for top.zhoudl.mapstruct.UserRoleMapper ,涔熷氨鏄娌℃湁鎵惧埌 UserRoleMapper 绫荤殑瀹炵幇绫汇€?/p>
閫氳繃鏌ラ槄涓€浜涜祫鏂欏彲寰楋細
MapStruct 鏄竴涓彲浠ュ鐞嗘敞瑙g殑Java缂栬瘧鍣ㄦ彃浠讹紝鍙互鍦ㄥ懡浠よ涓娇鐢紝涔熷彲浠ュ湪 IDE 涓娇鐢ㄣ€侻apStruc t鏈変竴浜涢粯璁ら厤缃紝浣嗘槸涔熶负鐢ㄦ埛鎻愪緵浜嗚嚜宸辫繘琛岄厤缃殑閫斿緞銆傜己鐐瑰氨鏄繖鐜╂剰鍦ㄤ娇鐢ㄥ伐鍏疯嚜甯︾殑缂栬瘧鍣ㄦ椂涓嶄細鐢熸垚瀹炵幇绫伙紝闇€瑕侀€氳繃 maven 鐨勬柟寮忔潵杩涜缂栬瘧锛岀劧鍚庢墠浼氱敓鎴愬疄鐜扮被銆?/p>
鎵€浠ユ垜浠渶瑕佸鍔犱竴涓紪璇戞彃浠跺埌 pom 鏂囦欢涓細
<!-- 寮曞叆 processor -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
<scope>provided</scope>
</dependency>
<!--涓?Maven compile plugin 璁剧疆 annotation processor -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
鐒跺悗鎴戜滑杩愯绋嬪簭灏卞彲浠ュ緱鍒拌嚜宸辨兂瑕佺殑缁撴灉浜?/p>
瀹夎 MapStruct 鎻掍欢
浣跨敤 MapStruct锛岃繕鏈変竴涓己鐐瑰氨鏄紝褰撳睘鎬ф敼鍚嶇殑鏃跺€欙紝鍥犱负鍦?Mapper 涓婃敞瑙d腑閰嶇疆鐨勫悕瀛楁槸鍦ㄥ瓧绗︿覆閲岄潰锛屽洜姝や笉浼氳嚜鍔ㄥ悓姝ョ殑銆傛墍浠?MapStruct 鎻愪緵浜嗕竴涓彃浠舵潵瑙e喅杩欎釜闂锛屽悓鏃惰繕鎻愪緵浠g爜鑷姩鎻愮ず銆佺偣鍑昏烦杞埌瀹炵幇绛夊姛鑳姐€?/p>
鍏充簬鎻掍欢鐨勬洿澶氫俊鎭紝鍙傝 MapStruct support for IntelliJ IDEA
鎬荤粨
MapSturct
鏄竴涓敓鎴愮被鍨嬪畨鍏紝 楂樻€ц兘涓旀棤渚濊禆鐨?JavaBean 鏄犲皠浠g爜鐨勬敞瑙e鐞嗗櫒锛坅nnotation processor锛夈€?/p>
浣滀负涓€涓敞瑙e鐞嗗櫒锛?閫氳繃MapStruct
鐢熸垚鐨勪唬鐮佸叿鏈夋€庝箞鏍风殑浼樺娍鍛紵鎶撲竴涓嬮噸鐐癸細
- 娉ㄨВ澶勭悊鍣?/li>
- 鍙互鐢熸垚
JavaBean
涔嬮棿鐨勬槧灏勪唬鐮?/li> - 绫诲瀷瀹夊叏锛?楂樻€ц兘锛?鏃犱緷璧栨€?/li>
楂樻€ц兘
杩欐槸鐩稿鍙嶅皠鏉ヨ鐨勶紝 鍙嶅皠闇€瑕佸幓璇诲彇瀛楄妭鐮佺殑鍐呭锛?鑺遍攢浼氭瘮杈冨ぇ銆?鑰岄€氳繃 MapStruct
鏉ョ敓鎴愮殑浠g爜锛?鍏剁被浼间簬浜烘墜鍐欙紝浠g爜鎵ц閫熷害涓婂彲浠ュ緱鍒颁繚璇併€傦紙鍓嶉潰渚嬪瓙涓敓鎴愮殑浠g爜鍙互鍦ㄧ紪璇戝悗鐪嬪埌锛屽湪椤圭洰鐨?target/generated-sources/annotations
鐩綍閲屽彲浠ョ湅鍒板叿浣撲唬鐮侊級銆?/p>
鏄撲簬 debug
鍦ㄦ垜浠敓鎴愮殑浠g爜涓紝 鎴戜滑鍙互杞绘槗鐨勮繘琛?debug銆備絾鏄鏋滄槸浣跨敤鍙嶅皠瀹炵幇浠g爜鐨勬椂鍊欙紝 涓€鏃﹀嚭鐜颁簡闂锛?寰堝鏃跺€欐槸姣旇緝闅炬壘鍒板師鍥犮€?/p>
浠g爜鐙珛
鐢熸垚鐨勪唬鐮佹槸瀵圭珛鐨勶紝 娌℃湁杩愯鏃剁殑渚濊禆
鏈枃棣栧彂浜庡井淇″叕浼楀彿 銆愮▼搴忕尶鏉傝揣閾恒€戯紝鍏虫敞鍏紬鍙凤紝鑾峰彇鏇村绮惧僵鏂囩珷锛?/p>
鏈嶅姟鍣ㄨ櫄鎷熷寲鎶€鏈柟妗堬紙涓嬶級