1. 實(shí)體映射最強(qiáng)工具類(lèi):MapStruct 真香!

        共 38384字,需瀏覽 77分鐘

         ·

        2022-07-06 11:15

        關(guān)注我們,設(shè)為星標(biāo),每天7:40不見(jiàn)不散,架構(gòu)路上與您共享

        回復(fù)架構(gòu)師獲取資源


        大家好,我是你們的朋友架構(gòu)君,一個(gè)會(huì)寫(xiě)代碼吟詩(shī)的架構(gòu)師。

        'javajgs.com';


        來(lái)源:blog.csdn.net/qq122516902

        • 1.MapStruct是用來(lái)做什么的?
        • 2.使用MapStruct解決上述問(wèn)題
        • 3.添加默認(rèn)方法
        • 4.可以使用abstract class來(lái)代替接口


        • 5.可以使用多個(gè)參數(shù)
        • 5.直接使用參數(shù)作為屬性值
        • 6.更新對(duì)象屬性
        • 7.沒(méi)有g(shù)etter/setter也能賦值
        • 8.使用Spring依賴(lài)注入
        • 9.自定義類(lèi)型轉(zhuǎn)換

        首先來(lái)了解一下DTO,DTO簡(jiǎn)單的理解就是做數(shù)據(jù)傳輸對(duì)象的,類(lèi)似于VO,但是VO用于傳輸?shù)角岸?。(~~)

        1.MapStruct是用來(lái)做什么的?

        現(xiàn)在有這么個(gè)場(chǎng)景,從數(shù)據(jù)庫(kù)查詢(xún)出來(lái)了一個(gè)user對(duì)象(包含id,用戶(hù)名,密碼,手機(jī)號(hào),郵箱,角色這些字段)和一個(gè)對(duì)應(yīng)的角色對(duì)象role(包含id,角色名,角色描述這些字段),現(xiàn)在在controller需要用到user對(duì)象的id,用戶(hù)名,和角色對(duì)象的角色名三個(gè)屬性。

        一種方式是直接把兩個(gè)對(duì)象傳遞到controller層,但是這樣會(huì)多出很多沒(méi)用的屬性。更通用的方式是需要用到的屬性封裝成一個(gè)類(lèi)(DTO),通過(guò)傳輸這個(gè)類(lèi)的實(shí)例來(lái)完成數(shù)據(jù)傳輸。

        User.java

        @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,這個(gè)類(lèi)就是封裝的類(lèi)

        @Data  
        public class UserRoleDto {  
            /**  
             * 用戶(hù)id  
             */  
            private Long userId;  
            /**  
             * 用戶(hù)名  
             */  
            private String name;  
            /**  
             * 角色名  
             */  
            private String roleName;  
        }  

        測(cè)試類(lèi),模擬將user對(duì)象轉(zhuǎn)換成UserRoleDto對(duì)象

        public class MainTest {  
            User user = null;  
          
            /**  
             * 模擬從數(shù)據(jù)庫(kù)中查出user對(duì)象  
             */  
            @Before  
            public void before() {  
               Role role  = new Role(2L, "administrator""超級(jí)管理員");  
               user  = new User(1L, "zhangsan""12345""17677778888""[email protected]", role);  
            }  
          
            /**  
             * 模擬把user對(duì)象轉(zhuǎn)換成UserRoleDto對(duì)象  
             */  
            @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);  
            }  
        }  

        從上面代碼可以看出,通過(guò)getter、setter的方式把一個(gè)對(duì)象屬性值復(fù)制到另一個(gè)對(duì)象中去還是很麻煩的,尤其是當(dāng)屬性過(guò)多的時(shí)候。而MapStruct就是用于解決這種問(wèn)題的。

        2.使用MapStruct解決上述問(wèn)題

        這里我們沿用User.java、Role.java、UserRoleDto.java。

        新建一個(gè)UserRoleMapper.java,這個(gè)來(lái)用來(lái)定義User.java、Role.java和UserRoleDto.java之間屬性對(duì)應(yīng)規(guī)則:

        UserRoleMapper.java

        import org.mapstruct.Mapper;  
        import org.mapstruct.Mapping;  
        import org.mapstruct.Mappings;  
        import org.mapstruct.factory.Mappers;  
          
        /**  
         * @Mapper 定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則  
         *          在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制  
         */  
        @Mapper  
        public interface UserRoleMapper {  
          
            /**  
             * 獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例  
             * 接口中的屬性都是 public static final 的 方法都是public abstract的  
             */  
            UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);  
          
            /**  
             * 這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法  
             *  
             * @Mapping 用來(lái)定義屬性復(fù)制規(guī)則 source 指定源對(duì)象屬性 target指定目標(biāo)對(duì)象屬性  
             *  
             * @param user 這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象  
             * @return 返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象  
             */  
            @Mappings({  
                    @Mapping(source = "id", target = "userId"),  
                    @Mapping(source = "username", target = "name"),  
                    @Mapping(source = "role.roleName", target = "roleName")  
            })  
            UserRoleDto toUserRoleDto(User user);  
          
        }  

        在測(cè)試類(lèi)中測(cè)試:

        通過(guò)上面的例子可以看出,使用MapStruct方便許多。

        3.添加默認(rèn)方法

        添加默認(rèn)方法是為了這個(gè)類(lèi)(接口)不只是為了做數(shù)據(jù)轉(zhuǎn)換用的,也可以做一些其他的事。

        import org.mapstruct.Mapper;  
        import org.mapstruct.Mapping;  
        import org.mapstruct.Mappings;  
        import org.mapstruct.factory.Mappers;  
          
        /**  
         * @Mapper 定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則  
         *          在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制  
         */  
        @Mapper  
        public interface UserRoleMapper {  
          
            /**  
             * 獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例  
             * 接口中的屬性都是 public static final 的 方法都是public abstract的  
             */  
            UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);  
          
            /**  
             * 這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法  
             *  
             * @Mapping 用來(lái)定義屬性復(fù)制規(guī)則 source 指定源對(duì)象屬性 target指定目標(biāo)對(duì)象屬性  
             *  
             * @param user 這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象  
             * @return 返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象  
             */  
            @Mappings({  
                    @Mapping(source = "id", target = "userId"),  
                    @Mapping(source = "username", target = "name"),  
                    @Mapping(source = "role.roleName", target = "roleName")  
            })  
            UserRoleDto toUserRoleDto(User user);  
          
            /**  
             * 提供默認(rèn)方法,方法自己定義,這個(gè)方法是我隨便寫(xiě)的,不是要按照這個(gè)格式來(lái)的  
             * @return  
             */  
            default UserRoleDto defaultConvert() {  
                UserRoleDto userRoleDto = new UserRoleDto();  
                userRoleDto.setUserId(0L);  
                userRoleDto.setName("None");  
                userRoleDto.setRoleName("None");  
                return userRoleDto;  
            }  
          
        }  

        測(cè)試代碼:

        @Test  
        public void test3() {  
            UserRoleMapper userRoleMapperInstances = UserRoleMapper.INSTANCES;  
            UserRoleDto userRoleDto = userRoleMapperInstances.defaultConvert();  
            System.out.println(userRoleDto);  
        }  

        4. 可以使用abstract class來(lái)代替接口

        mapper可以用接口來(lái)實(shí)現(xiàn),也可以完全由抽象來(lái)完全代替

        import org.mapstruct.Mapper;  
        import org.mapstruct.Mapping;  
        import org.mapstruct.Mappings;  
        import org.mapstruct.factory.Mappers;  
          
        /**  
         * @Mapper 定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則  
         *          在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制  
         */  
        @Mapper  
        public abstract class UserRoleMapper {  
          
            /**  
             * 獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例  
             * 接口中的屬性都是 public static final 的 方法都是public abstract的  
             */  
            public static final UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);  
          
            /**  
             * 這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法  
             *  
             * @Mapping 用來(lái)定義屬性復(fù)制規(guī)則 source 指定源對(duì)象屬性 target指定目標(biāo)對(duì)象屬性  
             *  
             * @param user 這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象  
             * @return 返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象  
             */  
            @Mappings({  
                    @Mapping(source = "id", target = "userId"),  
                    @Mapping(source = "username", target = "name"),  
                    @Mapping(source = "role.roleName", target = "roleName")  
            })  
            public abstract UserRoleDto toUserRoleDto(User user);  
          
            /**  
             * 提供默認(rèn)方法,方法自己定義,這個(gè)方法是我隨便寫(xiě)的,不是要按照這個(gè)格式來(lái)的  
             * @return  
             */  
            UserRoleDto defaultConvert() {  
                UserRoleDto userRoleDto = new UserRoleDto();  
                userRoleDto.setUserId(0L);  
                userRoleDto.setName("None");  
                userRoleDto.setRoleName("None");  
                return userRoleDto;  
            }  
          
        }  

        5.可以使用多個(gè)參數(shù)

        可以綁定多個(gè)對(duì)象的屬性值到目標(biāo)對(duì)象中:

        package com.mapstruct.demo;  
          
        import org.mapstruct.Mapper;  
        import org.mapstruct.Mapping;  
        import org.mapstruct.Mappings;  
        import org.mapstruct.factory.Mappers;  
          
        /**  
         * @Mapper 定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則  
         *          在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制  
         */  
        @Mapper  
        public interface UserRoleMapper {  
          
            /**  
             * 獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例  
             * 接口中的屬性都是 public static final 的 方法都是public abstract的  
             */  
            UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);  
          
            /**  
             * 這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法  
             *  
             * @Mapping 用來(lái)定義屬性復(fù)制規(guī)則 source 指定源對(duì)象屬性 target指定目標(biāo)對(duì)象屬性  
             *  
             * @param user 這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象  
             * @return 返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象  
             */  
            @Mappings({  
                    @Mapping(source = "id", target = "userId"),  
                    @Mapping(source = "username", target = "name"),  
                    @Mapping(source = "role.roleName", target = "roleName")  
            })  
            UserRoleDto toUserRoleDto(User user);  
          
            /**  
             * 多個(gè)參數(shù)中的值綁定   
             * @param user 源1  
             * @param role 源2  
             * @return 從源1、2中提取出的結(jié)果  
             */  
            @Mappings({  
                    @Mapping(source = "user.id", target = "userId"), // 把user中的id綁定到目標(biāo)對(duì)象的userId屬性中  
                    @Mapping(source = "user.username", target = "name"), // 把user中的username綁定到目標(biāo)對(duì)象的name屬性中  
                    @Mapping(source = "role.roleName", target = "roleName") // 把role對(duì)象的roleName屬性值綁定到目標(biāo)對(duì)象的roleName中  
            })  
            UserRoleDto toUserRoleDto(User user, Role role);  

        對(duì)比兩個(gè)方法~

        5.直接使用參數(shù)作為屬性值

        package com.mapstruct.demo;  
          
        import org.mapstruct.Mapper;  
        import org.mapstruct.Mapping;  
        import org.mapstruct.Mappings;  
        import org.mapstruct.factory.Mappers;  
          
        /**  
         * @Mapper 定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則  
         *          在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制  
         */  
        @Mapper  
        public interface UserRoleMapper {  
          
            /**  
             * 獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例  
             * 接口中的屬性都是 public static final 的 方法都是public abstract的  
             */  
            UserRoleMapper INSTANCES = Mappers.getMapper(UserRoleMapper.class);  
          
            /**  
             * 直接使用參數(shù)作為值  
             * @param user  
             * @param myRoleName  
             * @return  
             */  
            @Mappings({  
                    @Mapping(source = "user.id", target = "userId"), // 把user中的id綁定到目標(biāo)對(duì)象的userId屬性中  
                    @Mapping(source = "user.username", target = "name"), // 把user中的username綁定到目標(biāo)對(duì)象的name屬性中  
                    @Mapping(source = "myRoleName", target = "roleName") // 把role對(duì)象的roleName屬性值綁定到目標(biāo)對(duì)象的roleName中  
            })  
            UserRoleDto useParameter(User user, String myRoleName);  
          
        }  

        測(cè)試類(lèi):

        public class Test1 {  
            Role role = null;  
            User user = null;  
          
            @Before  
            public void before() {  
                role = new Role(2L, "administrator""超級(jí)管理員");  
                user = new User(1L, "zhangsan""12345""17677778888""[email protected]", role);  
            }  
            @Test  
            public void test1() {  
                UserRoleMapper instances = UserRoleMapper.INSTANCES;  
                UserRoleDto userRoleDto = instances.useParameter(user, "myUserRole");  
                System.out.println(userRoleDto);  
            }  
        }  

        6.更新對(duì)象屬性

        在之前的例子中UserRoleDto useParameter(User user, String myRoleName);都是通過(guò)類(lèi)似上面的方法來(lái)生成一個(gè)對(duì)象。而MapStruct提供了另外一種方式來(lái)更新一個(gè)對(duì)象中的屬性。@MappingTarget

        public interface UserRoleMapper1 {  
          
            UserRoleMapper1 INSTANCES = Mappers.getMapper(UserRoleMapper1.class);  
          
            @Mappings({  
                    @Mapping(source = "userId", target = "id"),  
                    @Mapping(source = "name", target = "username"),  
                    @Mapping(source = "roleName", target = "role.roleName")  
            })  
            void updateDto(UserRoleDto userRoleDto, @MappingTarget User user);  
          
          
            @Mappings({  
                    @Mapping(source = "id", target = "userId"),  
                    @Mapping(source = "username", target = "name"),  
                    @Mapping(source = "role.roleName", target = "roleName")  
            })  
            void update(User user, @MappingTarget UserRoleDto userRoleDto);  
          
        }  

        通過(guò)@MappingTarget來(lái)指定目標(biāo)類(lèi)是誰(shuí)(誰(shuí)的屬性需要被更新)。@Mapping還是用來(lái)定義屬性對(duì)應(yīng)規(guī)則。

        以此為例說(shuō)明:

        @Mappings({  
                    @Mapping(source = "id", target = "userId"),  
                    @Mapping(source = "username", target = "name"),  
                    @Mapping(source = "role.roleName", target = "roleName")  
            })  
            void update(User user, @MappingTarget UserRoleDto userRoleDto);  

        @MappingTarget標(biāo)注的類(lèi)UserRoleDto 為目標(biāo)類(lèi),user類(lèi)為源類(lèi),調(diào)用此方法,會(huì)把源類(lèi)中的屬性更新到目標(biāo)類(lèi)中。更新規(guī)則還是由@Mapping指定。

        7.沒(méi)有g(shù)etter/setter也能賦值

        對(duì)于沒(méi)有g(shù)etter/setter的屬性也能實(shí)現(xiàn)賦值操作

        public class Customer {  
          
            private Long id;  
            private String name;  
          
            //getters and setter omitted for brevity  
        }  
          
        public class CustomerDto {  
          
            public Long id;  
            public String customerName;  
        }  
          
        @Mapper  
        public interface CustomerMapper {  
          
            CustomerMapper INSTANCE = Mappers.getMapper( CustomerMapper.class );  
          
            @Mapping(source = "customerName", target = "name")  
            Customer toCustomer(CustomerDto customerDto);  
          
            @InheritInverseConfiguration  
            CustomerDto fromCustomer(Customer customer);  
        }  

        @Mapping(source = “customerName”, target = “name”)不是用來(lái)指定屬性映射的,如果兩個(gè)對(duì)象的屬性名相同是可以省略@Mapping的。

        MapStruct生成的實(shí)現(xiàn)類(lèi):

        @Generated(  
            value = "org.mapstruct.ap.MappingProcessor",  
            date = "2019-02-14T15:41:21+0800",  
            comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"  
        )  
        public class CustomerMapperImpl implements CustomerMapper {  
          
            @Override  
            public Customer toCustomer(CustomerDto customerDto) {  
                if ( customerDto == null ) {  
                    return null;  
                }  
          
                Customer customer = new Customer();  
          
                customer.setName( customerDto.customerName );  
                customer.setId( customerDto.id );  
          
                return customer;  
            }  
          
            @Override  
            public CustomerDto toCustomerDto(Customer customer) {  
                if ( customer == null ) {  
                    return null;  
                }  
          
                CustomerDto customerDto = new CustomerDto();  
          
                customerDto.customerName = customer.getName();  
                customerDto.id = customer.getId();  
          
                return customerDto;  
            }  
        }  

        @InheritInverseConfiguration在這里的作用就是實(shí)現(xiàn)customerDto.customerName = customer.getName();功能的。如果沒(méi)有這個(gè)注解,toCustomerDto這個(gè)方法則不會(huì)有customerName 和name兩個(gè)屬性的對(duì)應(yīng)關(guān)系的。

        8.使用Spring依賴(lài)注入

        @Data  
        @NoArgsConstructor  
        @AllArgsConstructor  
        public class Customer {  
            private Long id;  
            private String name;  
        }  
          
        @Data  
        public class CustomerDto {  
            private Long id;  
            private String customerName;  
        }  
          
        // 這里主要是這個(gè)componentModel 屬性,它的值就是當(dāng)前要使用的依賴(lài)注入的環(huán)境  
        @Mapper(componentModel = "spring")  
        public interface CustomerMapper {  
          
            @Mapping(source = "name", target = "customerName")  
            CustomerDto toCustomerDto(Customer customer);  
        }  

        @Mapper(componentModel = “spring”),表示把當(dāng)前Mapper類(lèi)納入spring容器??梢栽谄渌?lèi)中直接注入了:

        @SpringBootApplication  
        @RestController  
        public class DemoMapstructApplication {  
          
         // 注入Mapper  
            @Autowired  
            private CustomerMapper mapper;  
          
            public static void main(String[] args) {  
                SpringApplication.run(DemoMapstructApplication.class, args);  
            }  
          
            @GetMapping("/test")  
            public String test() {  
                Customer customer = new Customer(1L, "zhangsan");  
                CustomerDto customerDto = mapper.toCustomerDto(customer);  
                return customerDto.toString();  
            }  
          
        }  

        看一下由mapstruct自動(dòng)生成的類(lèi)文件,會(huì)發(fā)現(xiàn)標(biāo)記了@Component注解。

        @Generated(  
            value = "org.mapstruct.ap.MappingProcessor",  
            date = "2019-02-14T15:54:17+0800",  
            comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"  
        )  
        @Component  
        public class CustomerMapperImpl implements CustomerMapper {  
          
            @Override  
            public CustomerDto toCustomerDto(Customer customer) {  
                if ( customer == null ) {  
                    return null;  
                }  
          
                CustomerDto customerDto = new CustomerDto();  
          
                customerDto.setCustomerName( customer.getName() );  
                customerDto.setId( customer.getId() );  
          
                return customerDto;  
            }  
        }  

        9.自定義類(lèi)型轉(zhuǎn)換

        有時(shí)候,在對(duì)象轉(zhuǎn)換的時(shí)候可能會(huì)出現(xiàn)這樣一個(gè)問(wèn)題,就是源對(duì)象中的類(lèi)型是Boolean類(lèi)型,而目標(biāo)對(duì)象類(lèi)型是String類(lèi)型,這種情況可以通過(guò)@Mapper的uses屬性來(lái)實(shí)現(xiàn):

        @Data  
        @NoArgsConstructor  
        @AllArgsConstructor  
        public class Customer {  
            private Long id;  
            private String name;  
            private Boolean isDisable;  
        }  
          
        @Data  
        public class CustomerDto {  
            private Long id;  
            private String customerName;  
            private String disable;  
        }  

        定義轉(zhuǎn)換規(guī)則的類(lèi):

        public class BooleanStrFormat {  
            public String toStr(Boolean isDisable) {  
                if (isDisable) {  
                    return "Y";  
                } else {  
                    return "N";  
                }  
            }  
            public Boolean toBoolean(String str) {  
                if (str.equals("Y")) {  
                    return true;  
                } else {  
                    return false;  
                }  
            }  
        }  

        定義Mapper,@Mapper( uses = { BooleanStrFormat.class}),注意,這里的users屬性用于引用之前定義的轉(zhuǎn)換規(guī)則的類(lèi):

        @Mapper( uses = { BooleanStrFormat.class})  
        public interface CustomerMapper {  
          
            CustomerMapper INSTANCES = Mappers.getMapper(CustomerMapper.class);  
          
            @Mappings({  
                    @Mapping(source = "name", target = "customerName"),  
                    @Mapping(source = "isDisable", target = "disable")  
            })  
            CustomerDto toCustomerDto(Customer customer);  
        }  

        這樣子,Customer類(lèi)中的isDisable屬性的true就會(huì)轉(zhuǎn)變成CustomerDto中的disable屬性的yes。

        MapStruct自動(dòng)生成的類(lèi)中的代碼:

        @Generated(  
            value = "org.mapstruct.ap.MappingProcessor",  
            date = "2019-02-14T16:49:18+0800",  
            comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"  
        )  
        public class CustomerMapperImpl implements CustomerMapper {  
          
         // 引用 uses 中指定的類(lèi)  
            private final BooleanStrFormat booleanStrFormat = new BooleanStrFormat();  
          
            @Override  
            public CustomerDto toCustomerDto(Customer customer) {  
                if ( customer == null ) {  
                    return null;  
                }  
          
                CustomerDto customerDto = new CustomerDto();  
          // 轉(zhuǎn)換方式的使用  
                customerDto.setDisable( booleanStrFormat.toStr( customer.getIsDisable() ) );  
                customerDto.setCustomerName( customer.getName() );  
                customerDto.setId( customer.getId() );  
          
                return customerDto;  
            }  
        }  

        要注意的是,如果使用了例如像spring這樣的環(huán)境,Mapper引入uses類(lèi)實(shí)例的方式將是自動(dòng)注入,那么這個(gè)類(lèi)也應(yīng)該納入Spring容器:

        CustomerMapper.java指定使用spring

        @Mapper(componentModel = "spring", uses = { BooleanStrFormat.class})  
        public interface CustomerMapper {  
          
            CustomerMapper INSTANCES = Mappers.getMapper(CustomerMapper.class);  
          
            @Mappings({  
                    @Mapping(source = "name", target = "customerName"),  
                    @Mapping(source = "isDisable", target = "disable")  
            })  
            CustomerDto toCustomerDto(Customer customer);  
        }  

        轉(zhuǎn)換類(lèi)要加入Spring容器:

        @Component  
        public class BooleanStrFormat {  
            public String toStr(Boolean isDisable) {  
                if (isDisable) {  
                    return "Y";  
                } else {  
                    return "N";  
                }  
            }  
            public Boolean toBoolean(String str) {  
                if (str.equals("Y")) {  
                    return true;  
                } else {  
                    return false;  
                }  
            }  
        }  

        MapStruct自動(dòng)生成的類(lèi):

        @Generated(  
            value = "org.mapstruct.ap.MappingProcessor",  
            date = "2019-02-14T16:55:35+0800",  
            comments = "version: 1.3.0.Final, compiler: javac, environment: Java 1.8.0_181 (Oracle Corporation)"  
        )  
        @Component  
        public class CustomerMapperImpl implements CustomerMapper {  
          
         // 使用自動(dòng)注入的方式引入  
            @Autowired  
            private BooleanStrFormat booleanStrFormat;  
          
            @Override  
            public CustomerDto toCustomerDto(Customer customer) {  
                if ( customer == null ) {  
                    return null;  
                }  
          
                CustomerDto customerDto = new CustomerDto();  
          
                customerDto.setDisable( booleanStrFormat.toStr( customer.getIsDisable() ) );  
                customerDto.setCustomerName( customer.getName() );  
                customerDto.setId( customer.getId() );  
          
                return customerDto;  
            }  
        }


        到此文章就結(jié)束了。Java架構(gòu)師必看一個(gè)集公眾號(hào)、小程序、網(wǎng)站(3合1的文章平臺(tái),給您架構(gòu)路上一臂之力,javajgs.com)。如果今天的文章對(duì)你在進(jìn)階架構(gòu)師的路上有新的啟發(fā)和進(jìn)步,歡迎轉(zhuǎn)發(fā)給更多人。歡迎加入架構(gòu)師社區(qū)技術(shù)交流群,眾多大咖帶你進(jìn)階架構(gòu)師,在后臺(tái)回復(fù)“加群”即可入群。



        這些年小編給你分享過(guò)的干貨


        1.idea永久激活碼(親測(cè)可用)

        2.優(yōu)質(zhì)ERP系統(tǒng)帶進(jìn)銷(xiāo)存財(cái)務(wù)生產(chǎn)功能(附源碼)

        3.優(yōu)質(zhì)SpringBoot帶工作流管理項(xiàng)目(附源碼)

        4.最好用的OA系統(tǒng),拿來(lái)即用(附源碼)

        5.SBoot+Vue外賣(mài)系統(tǒng)前后端都有(附源碼

        6.SBoot+Vue可視化大屏拖拽項(xiàng)目(附源碼)


        轉(zhuǎn)發(fā)在看就是最大的支持??

        瀏覽 26
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 古代吻胸摸腿扒衣 | 黄色电影视频网址 | 国产精品色婷婷AV挤奶 | 久久一次| 国产人妻精品一区二区三区不卡 |