涓氬姟浠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);
    

杩愯缁撴灉
鎶€鏈浘鐗? src=

涓婅竟鐨勪唬鐮佹垨璁告殏鏃剁湅璧锋潵杩樻槸姣旇緝绠€娲佺殑锛屼絾鏄垜浠渶瑕佹敞鎰忕殑涓€鐐瑰氨鏄钩鏃朵笟鍔″紑鍙戜腑鐨勫璞″睘鎬ц繙涓嶆槸涓婅堪浠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>

鎶€鏈浘鐗? src=

鏍稿績鏄繖涓€鍙?锛?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>

鎶€鏈浘鐗? src=

瀹夎 MapStruct 鎻掍欢

浣跨敤 MapStruct锛岃繕鏈変竴涓己鐐瑰氨鏄紝褰撳睘鎬ф敼鍚嶇殑鏃跺€欙紝鍥犱负鍦?Mapper 涓婃敞瑙d腑閰嶇疆鐨勫悕瀛楁槸鍦ㄥ瓧绗︿覆閲岄潰锛屽洜姝や笉浼氳嚜鍔ㄥ悓姝ョ殑銆傛墍浠?MapStruct 鎻愪緵浜嗕竴涓彃浠舵潵瑙e喅杩欎釜闂锛屽悓鏃惰繕鎻愪緵浠g爜鑷姩鎻愮ず銆佺偣鍑昏烦杞埌瀹炵幇绛夊姛鑳姐€?/p>

鍏充簬鎻掍欢鐨勬洿澶氫俊鎭紝鍙傝 MapStruct support for IntelliJ IDEA

鎬荤粨

MapSturct 鏄竴涓敓鎴愮被鍨嬪畨鍏紝 楂樻€ц兘涓旀棤渚濊禆鐨?JavaBean 鏄犲皠浠g爜鐨勬敞瑙e鐞嗗櫒锛坅nnotation processor锛夈€?/p>

浣滀负涓€涓敞瑙e鐞嗗櫒锛?閫氳繃MapStruct 鐢熸垚鐨勪唬鐮佸叿鏈夋€庝箞鏍风殑浼樺娍鍛紵鎶撲竴涓嬮噸鐐癸細

  1. 娉ㄨВ澶勭悊鍣?/li>
  2. 鍙互鐢熸垚 JavaBean 涔嬮棿鐨勬槧灏勪唬鐮?/li>
  3. 绫诲瀷瀹夊叏锛?楂樻€ц兘锛?鏃犱緷璧栨€?/li>

楂樻€ц兘

杩欐槸鐩稿鍙嶅皠鏉ヨ鐨勶紝 鍙嶅皠闇€瑕佸幓璇诲彇瀛楄妭鐮佺殑鍐呭锛?鑺遍攢浼氭瘮杈冨ぇ銆?鑰岄€氳繃 MapStruct 鏉ョ敓鎴愮殑浠g爜锛?鍏剁被浼间簬浜烘墜鍐欙紝浠g爜鎵ц閫熷害涓婂彲浠ュ緱鍒颁繚璇併€傦紙鍓嶉潰渚嬪瓙涓敓鎴愮殑浠g爜鍙互鍦ㄧ紪璇戝悗鐪嬪埌锛屽湪椤圭洰鐨?target/generated-sources/annotations 鐩綍閲屽彲浠ョ湅鍒板叿浣撲唬鐮侊級銆?/p>

鏄撲簬 debug

鍦ㄦ垜浠敓鎴愮殑浠g爜涓紝 鎴戜滑鍙互杞绘槗鐨勮繘琛?debug銆備絾鏄鏋滄槸浣跨敤鍙嶅皠瀹炵幇浠g爜鐨勬椂鍊欙紝 涓€鏃﹀嚭鐜颁簡闂锛?寰堝鏃跺€欐槸姣旇緝闅炬壘鍒板師鍥犮€?/p>

浠g爜鐙珛

鐢熸垚鐨勪唬鐮佹槸瀵圭珛鐨勶紝 娌℃湁杩愯鏃剁殑渚濊禆


鏈枃棣栧彂浜庡井淇″叕浼楀彿 銆愮▼搴忕尶鏉傝揣閾恒€戯紝鍏虫敞鍏紬鍙凤紝鑾峰彇鏇村绮惧僵鏂囩珷锛?/p>

鎶€鏈浘鐗? src=鏈嶅姟鍣ㄨ櫄鎷熷寲鎶€鏈柟妗堬紙涓嬶級

2.闈㈠悜瀵硅薄涓夊ぇ鐗瑰緛

Js涓璓roxy瀵硅薄

[瓒i椈] 浠g爜椤哄簭涔熶細褰卞搷 LuaJIT 鐨勬墽琛屾晥鐜?/a>