<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                **1. 兩個存在字段類型不一致的實體類** ```java @Data public class Flight { private String flightId; private String flightName; private String createTime; } @Data public class FlightDto { private String flightDtoId; private String flightDtoName; private LocalDateTime createTime; } ``` **2. 映射接口** ```java //imports將表達式用到的相關類導入到當前類中 @Mapper(imports = {DateUtils.class, UUID.class}) public interface FlightMapper { FlightMapper INSTANCE = Mappers.getMapper(FlightMapper.class); //flightDtoId取值為表達式expression的返回值 @Mapping(target = "flightDtoId", expression = "java(UUID.randomUUID().toString())") //當flightName為null時,flightDtoName取值為defaultExpression的返回值 @Mapping(source = "flightName", target = "flightDtoName", defaultExpression = "java(UUID.randomUUID().toString())") //當類型不一致時可以使用expression表達式進行轉換 @Mapping(target = "createTime", expression = "java(DateUtils.strToLocalDateTime(source.getCreateTime()))") FlightDto toDto(Flight source); //如果不使用imports屬性,則需要在表達式中寫全名 //@Mapping(, expression="java(java.util.UUID.randomUUID().toString())") } ``` 我定義的`DateUtils.strToLocalDateTime(...)`方法如下: ```java public class DateUtils { public static LocalDateTime strToLocalDateTime(String str) { DateTimeFormatter df = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss"); return LocalDateTime.parse(str, df); } } ``` **3. 測試** ```java @Test public void testFlightMapper() { Flight flight = new Flight(); flight.setFlightId("10001"); flight.setFlightName(null ); flight.setCreateTime("2021-10-21 10:38:29"); FlightDto flightDto = FlightMapper.INSTANCE.toDto(flight); //Flight:Flight(flightId=10001, flightName=null, createTime=2021-10-21 10:38:29) System.out.println("Flight:" + flight); //FlightDto:FlightDto(flightDtoId=48c367e7-..., flightDtoName=2bc3454e-..., createTime=2021-10-21T10:38:29) System.out.println("FlightDto:" + flightDto); } ``` **4. 查看映射接口被Mapstruct編譯后的代碼** ```java public class FlightMapperImpl implements FlightMapper { public FlightMapperImpl() { } public FlightDto toDto(Flight source) { if (source == null) { return null; } else { FlightDto flightDto = new FlightDto(); if (source.getFlightName() != null) { flightDto.setFlightDtoName(source.getFlightName()); } else { flightDto.setFlightDtoName(UUID.randomUUID().toString()); } flightDto.setFlightDtoId(UUID.randomUUID().toString()); flightDto.setCreateTime(DateUtils.strToLocalDateTime(source.getCreateTime())); return flightDto; } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看